Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user5200 · Jul 02, 2012 at 01:54 AM · guibuttontogglegui.button

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;
  }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image torrente · Jul 02, 2012 at 02:32 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user5200 · Jul 10, 2012 at 02:30 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats wrong with my GUI.Toggle? 2 Answers

(Unity 4.6) Button animations 1 Answer

multiple GUI.Buttons at the same time 1 Answer

Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers

Make more buttons appear, on button click. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges