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 chris gough · Apr 12, 2012 at 01:33 PM · guibuttoninventorytoggle

change the style of a gui by a key press

i am creating an inventory script that i want the user to press the numbers keys to interact with the bar along the bottom, but i cant get the images to change on key press only when they are clicked. i have tried using the toolbar but i get back errors, if someone knows a way in which to set a the toggle to active that would really help.

thanks in advance

function OnGUI() { var curIndex = 0; GUI.skin = customSkin; GUI.BeginGroup (new Rect (400, 500, 400, 120));

     for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {
         //iterate through all the items in the dictionary.
          var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);
          var customStyle : GUIStyle;
          var btnToggled : boolean;
          var itemIndex : int;
          itemIndex = curIndex;
          customStyle = "icon_"+item.Key;
          GUI.skin = customSkin;
          btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+")", customStyle);
          //Create a rect for the button.  It is offset more for each item in the array.
          if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) {
                curItem = item;
               //if the user clicks the button, select it as the current item.
          }
          if (curItem.Key == item.Key){
          btnToggled = true;
          }else{
          
          }
          curIndex++;
          //increase the offset counter.
     }

     GUI.EndGroup();

}

Comment
Add comment · Show 2
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 IgnoranceIsBliss · Apr 13, 2012 at 05:28 AM 0
Share

A little confused... in your code, customStyle is a GUIStyle object.

But then you say...

customStyle = "icon_"+item.$$anonymous$$ey;

That line sets it as if it was a string. Wouldn't it be better to say

customStyle = customSkin.GetStyle("icon_"+item.$$anonymous$$ey);

This gets the named style from your custom skin and then uses it to draw the toggle. As it is, you are using a BLAN$$anonymous$$ custom style, as far as I can tell (and you are doing it before setting the GUI.skin property, too).

avatar image chris gough · Apr 13, 2012 at 12:54 PM 0
Share

Thanks I've changed my script on what you said, but it still didn't solve the issue that I have which is, getting the custom style to change to active when the appropriate key was pressed. so when I press 1, 2, 3 or 4 the image will change to the active state of the style

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI.Button is acting funky. 2 Answers

Is there a way of using just basic gui.Buttons as toggles? 1 Answer

GUI Button which looks like a Toggle 0 Answers

GUI Button Position - Can it float to the top-left? 3 Answers

Need help with toggle button 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