- Home /
Toggling multiple button states
Hello all,
I have been doing some UI research for my game and found many useful things, but I'm stumped with this problem. First, I found a great way to make buttons that are just textures from here:
http://answers.unity3d.com/questions/26784/how-can-i-have-just-a-texture-as-a-button.html
And I am combining that with GUI.toggle documentation found here:
http://docs.unity3d.com/Documentation/ScriptReference/GUI.Toggle.html
Now, what I want to do is to toggle multiple button "states" so to speak. Let's say I have buttons A, B and C. Pressing A or B will turn on a "state", lets call them SpeedOn and StrengthOn. These states correspond to different textures which, if they are on, should apply the appropriate texture to button C when it is clicked. Hence, if i click on SpeedOn button (button A), then click on button C, the "SpeedOn" texture will appear on button C. Instead, if I have clicked on the "StrengthOn" button (button B), the texture associated with StrengthOn should appear on button C.
There are more than 2 different states so it won't be a simple matter like toggling a button on or off. There are at least 4 right now, but the rest are omitted for simplicity.
I hope I have explained that clearly enough. I have made some changes due to feedback and suggestions. But I am still unsure of what to put in button C's if statement. Here is the code I have so far:
These turn on the states (buttons A and B):
if (GUI.Button(new Rect(5, 20, 52, 52), spd))
{
speedon = true;
}
if (GUI.Button(new Rect(5, 72, 52, 52), str))
{
strengthon = true;
}
This is the code for when you click button C. Before receiving a state, it is simply an empty button:
CheckChosen();
if (GUI.Button(new Rect(203, 79, 20, 20), " "))
{
//What should go in here?
}
Finally, this function checks which state is on, so it can choose the appropriate texture to put on button C:
void CheckChosen()
{
if (speedon)
texturetoapply = spd;
if (strengthon)
texturetoapply = str;
if (viton)
texturetoapply = vit;
}
If the states go in a particular order, you can change your bool to perhaps an int and do something like you have above, but set it to increase the value by 1 per each click. You will have to also add in a bit of code to roll it over to the first setting when it reaches the top value. Doing this will give you the ability to add in as many options as you would need. Your coding looks solid, but please let me know if you need an example of how to do this.
Answer by user5200 · Aug 10, 2012 at 03:57 AM
Hi everyone,
I have figured out the answer to my own question after much fiddling around, so I thought I would share. Here is what I came up with:
TURN ON THE STATE:
if (GUI.Button(new Rect(5, 20, 52, 52), spd))
{
textureinsidemouse = spd;
speedon = true;
clickedon = "spd";
helpmsg = true;
}
if (GUI.Button(new Rect(314, 126, 22, 22), texturestoapply[0], mystyle))
{
if (Event.current.button == 1 && texturestoapply[0] != null)
{
Storepotential(new Vector2(1,1), 0);
removemsg = true;
}
if (clickedon == null)
return;
if (clickedon == "upwards" || clickedon == "downwards" || clickedon == "leftwards" || clickedon == "rightwards")
{
Changewave(clickedon, 1, 1);
updateAdjacentnodes(1,1);
Clearallmouseshit();
return;
}
texturestoapply[0] = textureinsidemouse;
gridcontent[1,1] = clickedon;
checkAdjacentnodes(1, 1);
Chainreaction();
}
There's a lot of extra stuff in there now, but basically, I made a Texture2D array and used it in the button content. At first, it is blank because there is no texture in texturestoapply[0]. Once I click on the button that turns on the state, the texture is "inside the mouse" so to speak. It will transfer to any blank button that has texturestoapply[] as content.
So, in the above example, spd is the name of the texture I want. When i click on the first button, it becomes textureinsidemouse. Then finally texturestoapply[0] becomes textureinsidemouse. Since the button is set to display texturestoapply[0], it will end up displaying the original spd texture. What a journey!
Answer by whydoidoit · Jul 02, 2012 at 07:43 AM
You're having the classic paradigm thinking problem with OnGUI :)
OnGUI gets called many times per frame with different events. The one that clicks the button isn't the one that draws it which comes later. Put your code from your CheckChosen code outside the if body to decide on a texture and put it in a variable. Use that variable as the texture in the GUI.Button in the if which shows the third button. Set the right true or false in the if for that button.
Hey $$anonymous$$ike, you have been answering a lot of my questions. Thanks for being patient with a program$$anonymous$$g noob. Can't hit that upvote button enough! Anyway, I'm trying to understand your suggestion here. I have moved CheckChosen out of the if body and made a new variable to hold the texture I want to appear on the currently empty C button. But I'm not sure what you mean by your last sentence. I have made edits to the OP based on your suggestions.