Author Topic: Please Add Example of Toggle Buttons.  (Read 5126 times)

rocki

  • Guest
Please Add Example of Toggle Buttons.
« on: June 26, 2013, 11:45:45 PM »
Hi Tasharen,

Would it be possible to update the examples and include some variations of  Toggle Buttons.  By Toggle Buttons, I mean if there are a set of 3 buttons, clicking on one would disable the other two. 

The reason for this request is that Toggle Buttons are a very often-used basic staple of GUI interaction.  It  would be really nice us who are getting started with NGUI to jump in with a working Toggle example.  At the moment, it's quite a lot of searching through the forum and trying to get something working.  It could take hours just to get a simple set of  toggle button to work. 

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Please Add Example of Toggle Buttons.
« Reply #1 on: June 27, 2013, 12:18:05 AM »
Look into the Checkbox tutorial. Checkbox's name is misleading. It should be just named "toggle". If you give a group of checkboxes the same root object, they will deactivate each other when clicked on.

TokyoDan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Please Add Example of Toggle Buttons.
« Reply #2 on: March 18, 2014, 06:28:00 AM »
The Checkbox example is very confusing and doesn't address what I want to do. It activates/deactivates colored box Sprites depending on what checkbox you click.

I just want to use toggles to set UserPefs depending on what I check.

I have three buttons setup as toggles and I add the OnSelectedMapToggleChange() function to each one as an EventDelegate. I didn't attach UIToggledComponents or UIToggledObjects because I don't want to enable/disable any components or objects, I simply want to make settings in PlayerPrefs.

https://www.dropbox.com/s/uyact5j014mkcqq/ToggleProblem.png

Another thing I think is strange is executing EventDelegate.Add(selectedMapToggle.onChange, OnSelectedMapToggleChange);  causes the code in that function to be executed before I even ever tap on a button.

  1. public class TOToptions2HUD : MonoBehaviour {
  2.  
  3.         public UIToggle[] selectedMapToggle;
  4.        
  5.  
  6.         void OnEnable() {
  7.                
  8.                 // add OnSelectedMapToggleChange delegate to each toggle button
  9.                 for(int i=0; i<selectedMapToggle.Length; i++)
  10.                 {
  11.                         EventDelegate.Add(selectedMapToggle[i].onChange, OnSelectedMapToggleChange);
  12.                 }
  13.                
  14.                
  15.                 // turn off the checkmark on each toggle button
  16.                 for(int i=0; i<selectedMapToggle.Length; i++)
  17.                 {
  18.                         selectedMapToggle[i].value = false;
  19.                 }
  20.                
  21.                 // Turn on the checkmark on the toggle button according to which one was set in PlayerPrefs
  22.  
  23.                 int ind = PlayerPrefs.GetInt("SelectedMapIndex");               // get the index of map that is selected               
  24.                 selectedMapToggle[ind].value = true;
  25.  
  26.     }
  27.        
  28.         // remove OnSelectedMapToggleChange delegate from each toggle button
  29.         void OnDisable()
  30.         {
  31.                
  32.                 for(int i=0; i<selectedMapToggle.Length; i++)
  33.                 {
  34.                         EventDelegate.Remove(selectedMapToggle[i].onChange, OnSelectedMapToggleChange);
  35.                 }
  36.  
  37.     }
  38.  
  39.        
  40.         void OnSelectedMapToggleChange()
  41.         {                      
  42.                 switch(UIToggle.current.name)
  43.                 {
  44.                 case "Button-UM1":
  45.                         PlayerPrefs.SetInt("SelectedMapIndex", 0);
  46.                         PlayerPrefs.Save();                                                    
  47.                         break;
  48.                 case "Button-UM2":
  49.                         PlayerPrefs.SetInt("SelectedMapIndex", 1);
  50.                         PlayerPrefs.Save();                                                    
  51.                         break;
  52.                 case "Button-UM3":
  53.                         PlayerPrefs.SetInt("SelectedMapIndex", 2);
  54.                         PlayerPrefs.Save();                                                    
  55.                         break;
  56.                 }
  57.         }
  58. }

P.S. I thought I could possibly simplify thing by attaching a UICheckBox script to each button instead of using UIToggle but I can't find UICheckBox script. Has it been discontinued?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Please Add Example of Toggle Buttons.
« Reply #3 on: March 18, 2014, 03:27:26 PM »
You are adding delegate listeners, then you change the state, which in turn triggers those delegate listeners... furthermore when the toggle starts up it calls the delegate after setting the initial state. In either case, the delegate will always execute at least once on start. This is done for consistency purposes so that the associated data always matches the toggle's state.

Inside your callback function you are also not checking the state. You just check the name. Keep in mind that callback will be called both when the state is 'true' as well as when it's 'false'.

You need to check UIToggle.current.value before setting any player prefs, or going further for that matter. In your case, if it's false, just exit early.

TokyoDan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Please Add Example of Toggle Buttons.
« Reply #4 on: March 18, 2014, 07:34:30 PM »
I got it fixed. Thanks again.