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
1
Question by cosmogonies.net · Feb 22, 2015 at 04:23 PM · guitogglecallbackgrouptwice

Unity new GUI, how to nicely trigger grouped toggleButton callback

Hi, I have issues when connecting a function into On Value Changed callback :

I have a Panel, with a "Toggle Group" component, which contains two toggleButtons, that work exclusively.

In each toggleButton, I can plug a function with (only?) one argument, and what I did is put an int to notify an index of the user choice (bad design?).

But I see that the function is triggered twice, one for the old active toggleButton (for disabling event I imagine), and next for the new ticked one... It is bad for me, because all my logic further is executed twice.

Worse, if I put a gameObject as argument of the callback function, I will not be able to put an int for index, so I will not be able to know the user choice... (.IsOn is true for Both callback executions)

So, how can I nicely trigger (the same) function from a grouped toggleButton, and know what gameObject is ticked (and only once per user click)

Thank you !

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

Answer by cosmogonies.net · Feb 22, 2015 at 06:54 PM

Anyway, I managed to do it in a tricky way :

 // From public properties, MyPanel is the panel which contains all the grouped toggleButton:
 // Each toggleButtons are connected to the function "OnToggle_Language", with a unique index for each as int parameter
 
 public void OnToggle_Language(int _LanguageIndex)
 {
     GameObject EventSource = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
 
     Toggle[] Toggles = MyPanel.GetComponentsInChildren<Toggle>() as Toggle[];
     for (int i = 0; i < Toggles.Length; i++) 
     {
         if( Toggles[i].gameObject ==  EventSource)
             if(Toggles[i].isOn)
                 if(i==_LanguageIndex)
                     Debug.Log (i + "is trigerred");
     }
 }

Now, the log is only triggered once, but I'm quite sure that this is a bad workaround and there's a cleaner way...

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 AminAghajoon · Jul 27, 2015 at 04:34 AM

Maybe the better way is this(attach this to toggle group object) :

  public void OnAnyToggleValueChanged()
  {
         var toggleGroup = GetComponent<ToggleGroup>();
         if (toggleGroup.AnyTogglesOn()) {
             var activeToggle = toggleGroup.ActiveToggles().First();
             // Do something with active toggle
         }
  }


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

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

GUI window popup button 1 Answer

Little help with GUI Toggle needed 1 Answer

EditorGUI elements require minimum size of 24px? 0 Answers

GuiTexture toggle on keypress. 2 Answers

Drawing GUI rectangle uses negative y coordinate 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