Author Topic: UISlider Change ?  (Read 5090 times)

Tester1234

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
UISlider Change ?
« on: January 04, 2014, 10:59:25 AM »
I have been looking other topics, searching web, this UISlider seems to changed alot in time. I just followed many example, all of them was failure.  :-\ , so I deciced to ask.

All I need to get the change in slider;

I made a test script, attached to a slider, but nothing happens when the slider changes. Can someone guide me ? Regards.
  1.         private UISlider uiSlider;
  2.  
  3.         void Awake(){
  4.                 uiSlider = this.gameObject.GetComponent<UISlider>() as UISlider;
  5.         }
  6.  
  7.         void OnValueChange (float val){
  8.         Debug.Log("Slider Test: " + val.ToString());
  9.     }
  10.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider Change ?
« Reply #1 on: January 04, 2014, 12:30:44 PM »
Doing it via code:
  1. public class Test : MonoBehaviour
  2. {
  3.     void Awake()
  4.     {
  5.         UISlider slider = GetComponent<UISlider>();
  6.         EventDelegate.Add(slider.onChange, MyFunc);
  7.     }
  8.  
  9.     void MyFunc ()
  10.     {
  11.         Debug.Log(UISlider.current.value);
  12.     }
  13. }
Doing it via inspector is easier. As long as the function is "public" and is of type "void (void)", it will show up in the inspector for the On Change section on the slider when you drag & drop the game object there.
  1. public class Test : MonoBehaviour
  2. {
  3.     public void MyFunc ()
  4.     {
  5.         Debug.Log(UISlider.current.value);
  6.     }
  7. }
http://www.tasharen.com/forum/index.php?topic=6738.0