- Home /
GUI Grid click
I have a script witch is
var selGridInt : int = 0; var selStrings : String[] = ["Empty", "Empty", "Empty", "Empty"];
function OnGUI () {
if (Input.GetKey("tab"))
selGridInt = GUI.SelectionGrid (Rect (1000, 10, 300, 100), selGridInt, selStrings, 2);
}
and I want it to be able to enable mesh renderer on a sword called "Broadsword" when i click one of them. Any help would be greatly appreciated. Thanks.
Answer by Paulo-Henrique025 · Aug 14, 2012 at 07:15 PM
void OnGUI()
{
int valueYouWant;
int storeIndex;
selGridInt = GUI.SelectionGrid (Rect (1000, 10, 300, 100), selGridInt, selStrings, 2);
if(storeIndex != selGridInt)
{
storeIndex = selGridInt;
if(selGridInt == valueYouWant)
{
//DoWhatYouWant
}
}
}
If your selGrindInt has changed check to see if it is the item you want and perform something
Edit: The complete working copy
using UnityEngine;
using System.Collections;
public class DrawGrid : MonoBehaviour {
public int selGridInt = -1; // The indexer of the grid
public string[] selStrings; // Itens of the grid
private int storeIndex = -1; // Store the actual value to check if there was some change in the grid
public int valueYouWant; // The value you are aiming for, if the grid index match this something must happen!
void Start()
{
selGridInt = -1; // Start the grid with nothing selected
}
void OnGUI()
{
selGridInt = GUILayout.SelectionGrid(selGridInt, selStrings, 2); // The grid using GUYLayout so you dont have to define a Rect, if you want to use Rect just use GUI.SelectionGrid
if(storeIndex != selGridInt) // If the grid index and the value we were holding are diferent it means there was some change in the grid
{
storeIndex = selGridInt; // The store now receives the grid index, so the next time OnGUI is called the if statement just above will no be true and this line will no be executed, this way the code is executed only once
if(selGridInt == valueYouWant) // If the grid index is the value you want do something
{
Debug.Log("lol");
}
}
}
}
Just keep in mind that the SelectableGrid does not behave like a button. If you are looking for a button like behavior just use button. If you think drawing lots of buttons seems hard and boring try to use GUILayout or even a for/foreach routine to draw them for you.
its C#, you can identify by the the return type beside OnGUI, void means that no value is returned, in JS just function would work.
Looking more closer I've notice that part of my code was pasted from yours, which is JS. The correct call of the function would be: GUI.SelectionGrid (new Rect (1000, 10, 300, 100), selGridInt, selStrings, 2); The new was missing.
Sorry I'm a bit confused about the second part of your comment. "The correct call of the function would be: GUI.SelectionGrid (new Rect (1000, 10, 300, 100), selGridInt, selStrings, 2)"
sorry I'm kinda new to scripts. What does that mean.
While using C# you need to use a keyword called "new" to create an object, you cant create them out of nothing like in JS. Thats why the keyword "new" was missing before the type Rect, the code would never work in a .cs script. I'll edit the answer with the correct code.