- Home /
Accessing a particular GUI.Button in code after it has been created
I am working on a project (iOS) that requires me to have access to a particular button. I am creating several buttons using a FOR loop and using an array as its origin. Each array element has several variables that I use within the IF declaration of the button so I can give buttons functionality.
What I want to do are two things:
1) To be able to tap a particular button and change its background without changing the background of all the other buttons. I thought of using a toggle button, but it doesn't work for what I am trying to accomplish. I tried adding a change of GUIStyle to the IF statement but it changes all other buttons with it.
2) I need to implement a Play functionality that basically "steps through" every button, each button changing background when it's doing something and executing its function when it's called.
Is there a way I can "tag" the buttons for me to access them individually later on in code? Is there a way to assign a name to a button to access it that way? Can you think of a way to achieve these things in a simpler way?
Thank you very much for the help!
I wish I could upvote. Exactly same Q what I want to search!
Answer by robertbu · Apr 16, 2014 at 12:33 AM
I'm not sure of your exact requirements, and I'm not an expert on the GUI system since I use EZGUI for my interface word. But you can use an index to color some specific button. Here is an exmple script. Attach to an empty game object. You can change the value of indexToColor to change which button (zero based index) gets a color.
#pragma strict
private var indexToColor = -1;
private var rect = Rect(0,0,100,50);
private var dist = 60;
private var strings = ["One", "Two", "Three", "Four", "Five"];
private var color = Color.red;
function OnGUI () {
for (var i = 0; i < strings.Length; i++) {
var r = rect;
r.y = i * dist;
var saveColor : Color;
if (i == indexToColor) {
saveColor = GUI.color;
GUI.color = color;
}
if (GUI.Button(r, strings[i])) {
Debug.Log("The "+i+"th button was pressed");
indexToColor = i;
}
if (i == indexToColor) {
GUI.color = saveColor;
}
}
}
Hi Robert, thank you for your answer! This definitely proves to be extremely helpful in what I'm trying to accomplish, although when I try out the script it always seems to color the "Two" button red regardless of the indexToColor value and I'm not sure why. Any idea why that could be?
You were probably trying to change the value in the script. Since 'indexToColor' is a public variable, any code initialization of public variables is only used at the time the script is attached. After that only the value in the inspector matters.
I change the example code. I made 'indexToColor' private, and now it changes the color of the button last clicked.
You're absolutely right. I wrongly assumed it was a private variable. Thank you!
I have one more question. I'm trying to do exactly what you just did but with GUIStyles ins$$anonymous$$d of colors so I can use images ins$$anonymous$$d of colors. It seems to me that it should be somewhat similar to the code, but the only thing I'm having trouble with is getting the equivalent to the GUI.color in the non-button IF statements. I'm assu$$anonymous$$g I just have to add what the current GUIStyle is, but I'm not quite sure how I can dynamically get the current GUIStyle. Any suggestions? (Sorry if this is obvious or too ambiguous, but I'm somewhat of a beginner still and often overlook things).
Since I use EZGUI, I don't have a deep understand of Unity's GUI, so there may be better ways to use GUIStyles. Below is a bit of code that uses GUIStyles. I don't use the current style. Ins$$anonymous$$d there are two new styles: 'styleNormal' and 'stylePressed'. You are responsible for initializing the textures and the font in the Inspector. Just open a style and drag and drop the textures into the appropriate slots. For my test, I also change the font and font alignment as well as the textures. Here is the new code:
#pragma strict
private var indexToColor = -1;
private var rect = Rect(0,0,100,50);
private var dist = 60;
private var strings = ["One", "Two", "Three", "Four", "Five"];
private var color = Color.red;
public var styleNormal : GUIStyle;
public var stylePressed : GUIStyle;
function OnGUI () {
for (var i = 0; i < strings.Length; i++) {
var r = rect;
r.y = i * dist;
var saveColor : Color;
var style = (i == indexToColor) ? stylePressed : styleNormal;
if (GUI.Button(r, strings[i], style)) {
Debug.Log("The "+i+"th button was pressed");
indexToColor = i;
}
}
}
Your answer
Follow this Question
Related Questions
OnGUI/iPhone - cannot touch TWO+ buttons at the same time...? 3 Answers
Optimizing OnGUI - Too many gui elements? 2 Answers
Remove Button Border 3 Answers