Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ronronmx · May 27, 2011 at 06:57 PM · guigui-button

GUI.Toolbar - Custom on/off button states

I have a toolbar with an array of custom images for each button, so far so good. Where I hit the wall is when I tried to make each button have its own "Active" state...

What's I'm doing now is using to arrays of images, the first array being my "Normal" state, the second array being the "Active" states.

It's kind of a hack, since I have to switch array members depending on which button was pressed, and go back the the original image everytime another button is pressed.

Is there a better way to have custom "Active" state for each toolbal button, or should I use individual buttons instead of a toolbar?

Thx in advance!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jahroy · May 27, 2011 at 07:12 PM

Look into the use of GUISkin and custom GUIStyles.

First, create a GUISkin. This can be done by right-clicking in the project pane.

A GUISkin is basically an array of GUIStyles. There are styles for all the basic GUI controls, plus a place where you can create your own custom styles.

Custom Styles can be added to the array named customStyles, which can be found at the bottom of your GUI Skin in the Inspector (left-click your GUISkin and look in the Inspector). You can add as many custom styles as you like. Each style has 8 different states: normal, hover, active, and focused for both the on and off state. Each state can have its own background image.

I would recommend adding a style for each button and naming the style in a way that it can accessed in your script. One way to do this would be to have an array of button objects that you iterate over to draw your buttons. It sounds like you're going to want toggles since you want two different states, so each button object might have a boolean to track its state and a name that can be used to look up its style. You might also want to store its position just for fun:

  class ActionButton
  {
    var styleName   :  String;   // name of custom style in GUI Skin
    var isSelected  :  boolean;  // is the toggle button on or off
    var label       :  String;
    var position    :  Rect;     // where to draw this button on the screen 
  }
 
 
  /* an array of buttons that you can populate in the inspector */
 
  var buttonList  :  ActionButton [];
 
 
  function OnGUI ()
  {
 
    for ( var i = 0; i < buttonList.length; i ++ ) {

      var butt = buttonList[i];
 
      butt.isSelected = 
        GUI.Toggle(butt.position, butt.isSelected, butt.label, butt.styleName);
 
      if ( butt.isSelected ) {
        print("This butt is turned on: " + butt.label);
        resetOtherButtons(i);
      }
 
    }

  }

  function resetOtherButtons ( ignoreThisNumber ) 
  {
    for ( var i = 0; i < buttonList.length; i ++ ) {

      if ( i != ignoreThisNumber ) {
        buttonList[i].isSelected = false;
      }
    }
  }
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 HHameline · May 27, 2011 at 07:19 PM

I think I had the exact same issue. Let me show you my code and and you can decide whether or not it helps you.

     void OnGUI()
 {
     if (GUI.Toggle(new Rect(70, 165, 217, 133), boolC, "", "cat1"))
     {
         boolP = false;
         boolM = false;
         boolZ = false;
         category = 0;
     }

     if (GUI.Toggle(new Rect(70, 305, 217, 133), boolP, "", "cat2"))
     {
         boolC = false;
         boolM = false;
         boolZ = false;
         category = 1;
     }

     if (GUI.Toggle(new Rect(70, 445, 217, 133), boolM, "", "cat3"))
     {
         boolC = false;
         boolP = false;
         boolZ = false;
         category = 2;
     }

     if (GUI.Toggle(new Rect(70, 585, 217, 133), boolZ, "", "cat4"))
     {
         boolC = false;
         boolP = false;
         boolM = false;
         category = 3;
     }
 }


The "cat#" are individual custom style sin my GUISkin. You can't actually give each button in a toolbar a unique active/pressed/hover texture unfortunately it's all for one and one for all. What I did was created four custom toggles that act like buttons and behave like a toolbar by setting the others to false when one is pressed and using the category int and a switch(category) for any logic I might need to do based on which button was pressed. If you think this might help and have any questions just comment on this post and I'll reply as soon as possible

Hope that helps. Hans

Comment
Add comment · Show 6 · 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 HHameline · May 27, 2011 at 07:20 PM 0
Share

Hah jahroy guess i should have refreshed before posting :P, both have the same idea.

avatar image jahroy · May 27, 2011 at 07:42 PM 1
Share

You could make my code accomplish the same thing by adding a function named resetOtherButtons() and calling it whenever a toggle button is turned on. The code in the new function would simply iterate over the array of buttons and set isSelected to false for each button other than the selected one. I just edited my answer to include the reset function...

avatar image ronronmx · May 27, 2011 at 08:38 PM 0
Share

Thanks a lot to both of you for the help, both solutions would work perfectly fine! What I ended up doing (for now), is creating a customStyle with one "On Normal" texture applied, which changes the background behind the button. The only drawback doing it this way is that I can't change the image of the button itself (for example adding a glow to the icon), but at least the background changes and accomplishes what I wanted. Later when I decide whether or not to change the appearance of the icon itself, I will use your suggestions to make it happen.

Thanks again!

avatar image jahroy · May 27, 2011 at 08:48 PM 0
Share

Why not make your icon part of your background image? We typically use 6 different images for each button: normal, hover, active, on-normal, on-hover, on-active. Each button gets its own style and looks quite nice. In the code you pass an empty string to the GUI.Toggle() function so that the background images applied to the style do all the work.

avatar image ronronmx · May 27, 2011 at 11:20 PM 0
Share

Yes I thought about doing it this way but I really wanted to use a gui.toolbar! I thought there had to be an easy way to accomplish this using a toolbar, but apparently not. $$anonymous$$aybe I will go back to using separate buttons, or toggles, ins$$anonymous$$d of using a toolbar?!?

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a Hover function for a button or toolbar from code. 2 Answers

Setting Scroll View Width GUILayout 1 Answer

How can I set up a series of OnGUI Toggle buttons in a for loop? 1 Answer

How to change a GUI button texture through scripting 1 Answer

Situational GUI popup question. 3 Answers


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