Author Topic: basic functionallity of UIToggle, is there a "onToggle" state ?  (Read 5693 times)

Vaupell2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
  • Can't activate my main profile so had to make a 2.
    • View Profile
    • Evisystems
Trying to do a simple options menu where it's possible to turn music and sound on/off seperatly.

and NGUI:UIToggle is just giving me grief :(

I have tryed many different setups, and the tutorials only show states of sprites which i donot care about!
I want to access scripting when there is a change.

IE, a player "unchecks" the checkmark and it saves playerprefs music off..

Look at this image (screenshot of my problem) - link to large screenshot - http://i.imgur.com/vK3S4ZB.png



Im trying to save playerprefs when checked/unchecked.

  1. public class musicToggle : MonoBehaviour {
  2.  
  3.         public void Update ()
  4.         {
  5.                 if(UIToggle.current.name=="musicCheck" == true ){
  6.                         Debug.Log (UIToggle.current.name);
  7.                         PlayerPrefs.SetInt("music", 1);
  8.                 }
  9.  
  10.         }
  11. }
  12.  


Any advice ?

Vaupell2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
  • Can't activate my main profile so had to make a 2.
    • View Profile
    • Evisystems
Re: basic functionallity of UIToggle, is there a "onToggle" state ?
« Reply #1 on: December 11, 2013, 04:55:12 PM »
still trying to just get the current state of the buttom :( 

  1.  
  2.         using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class optionsToMain : MonoBehaviour {
  6.  
  7.         void OnToggleChanged()
  8.         {
  9.                 PlayerPrefs.SetInt ("lyd", UIToggle.current.value);
  10.  
  11.  
  12.                 Debug.Log ("Sound toggle");
  13.         }
  14. }
  15.  

Didnt work either.

getting  "The best overloaded method match for 'UnityEngine.PlayerPefs.SetInt(string, int)' has invalid arguments.
Well i think the toggle is returning a bool not 1 or 0.. so it must be true or false ..

gonna try to "CONVERT" the bool to int then, because you can't save a bool.. :(


Edit, now tried this,,  no errors, but also no functionality..


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class optionsToMain : MonoBehaviour {
  5.  
  6.         public bool tempValue;
  7.         //public int soundStatus;
  8.  
  9.         void OnToggleChanged()
  10.         {
  11.                 tempValue = UIToggle.current.value;
  12.                 if(tempValue){
  13.                         PlayerPrefs.SetInt ("lyd", 1);
  14.                 } else {
  15.                         PlayerPrefs.SetInt ("lyd", 0);
  16.                 }
  17.  
  18.  
  19.  
  20.         //      soundStatus = UIToggle.GetActiveToggle(tempSound, 1);
  21.         //      PlayerPrefs.SetInt (gameSound, soundStatus);
  22.                 Debug.Log ("Sound toggle");
  23.         }
  24. }
The script is ofcourse attached to the actual toggle button which has the UIToggle script on it.
« Last Edit: December 11, 2013, 05:56:53 PM by Vaupell2 »

npritchard

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: basic functionallity of UIToggle, is there a "onToggle" state ?
« Reply #2 on: December 11, 2013, 07:48:06 PM »
Hook into the event using EventDelegate:

http://www.tasharen.com/ngui/docs/class_event_delegate.html

  1.  
  2. public class OptionsToMain : MonoBehaviour {
  3.   public UIToggle soundToggle;
  4.  
  5.   void Start() {
  6.     EventDelegate.Add(soundToggle.onChange, OnSoundToggleChange);
  7.   }
  8.  
  9.   void OnDestroy() {
  10.     EventDelegate.Remove(soundToggle.onChange, OnSoundToggleChange);
  11.   }
  12.  
  13.   void OnSoundToggleChange() {
  14.     if (soundToggle.value) {
  15.     } else {
  16.     }
  17.   }
  18.  
  19. }
  20.  

Hope that helps

Vaupell2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
  • Can't activate my main profile so had to make a 2.
    • View Profile
    • Evisystems
Re: basic functionallity of UIToggle, is there a "onToggle" state ?
« Reply #3 on: December 12, 2013, 02:52:35 PM »
Hook into the event using EventDelegate:

http://www.tasharen.com/ngui/docs/class_event_delegate.html

  1.  
  2. public class OptionsToMain : MonoBehaviour {
  3.   public UIToggle soundToggle;
  4.  
  5.   void Start() {
  6.     EventDelegate.Add(soundToggle.onChange, OnSoundToggleChange);
  7.   }
  8.  
  9.   void OnDestroy() {
  10.     EventDelegate.Remove(soundToggle.onChange, OnSoundToggleChange);
  11.   }
  12.  
  13.   void OnSoundToggleChange() {
  14.     if (soundToggle.value) {
  15.     } else {
  16.     }
  17.   }
  18.  
  19. }
  20.  

Hope that helps

Got a "buy me a drink" link  ?

Because i want to buy you a drink!  IT WORKS!!

Thank you!

Siddharth3322

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: basic functionallity of UIToggle, is there a "onToggle" state ?
« Reply #4 on: February 06, 2014, 01:23:11 AM »
Thanks for this discussion.