Author Topic: Progression slider sending a percentage for UILabel Text  (Read 3737 times)

IIIHAPPYIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Progression slider sending a percentage for UILabel Text
« on: March 14, 2013, 10:42:42 AM »
I have a progression widget setup that I would like to have send it's value to a UILabel to input into the Text field. I have research the UIEvenReceiver and Listener but still have not figured out exactly how all that works. Can I get little more of example here?


To add on here is what I have so far:

  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UISlider))]
  4. public class LevelPercentage : MonoBehaviour
  5. {
  6.     UISlider mSlider;
  7.         UILabel mLabel;
  8.  
  9.     void Awake ()
  10.     {
  11.         mSlider = GetComponent<UISlider>();
  12.         mSlider.sliderValue = mLabel;
  13.         mSlider.eventReceiver = gameObject;
  14.     }
  15.  
  16.     void OnSliderChange (float val)
  17.     {
  18.         mLabel = val;
  19.     }
  20. }
  21.  


Now I know that I am getting an error converting. I am not sure if I am going in the right direction or not.
« Last Edit: March 14, 2013, 11:43:27 AM by IIIHAPPYIII »

IIIHAPPYIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: Progression slider sending a percentage for UILabel Text
« Reply #1 on: March 14, 2013, 01:37:10 PM »
So I've kind of got it to work using this:

  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UISlider))]
  4. public class LevelPercentage : MonoBehaviour
  5. {
  6.     UISlider mSlider;
  7.         UILabel mLabel;
  8.  
  9.     void Awake ()
  10.     {
  11.         mSlider = GetComponent<UISlider>();
  12.         mSlider.eventReceiver = gameObject;
  13.                 mLabel = GetComponent<UILabel>();
  14.  
  15.     }
  16.  
  17.     void OnSliderChange (float val)
  18.     {
  19.         mLabel.text = val.ToString ();
  20.     }
  21. }
  22.  
  23.  
  24.  


However, I don't know how to get it to point to the existing UISlider on my Progress Widget. As it is not, when attached to the UILabel it creates the UISlider because I have it set to require component. However, I want to ditch the require component and set it to point towards the existing UISlider. I have tried this:

  1. using UnityEngine;
  2.  
  3. public class LevelPercentage : MonoBehaviour
  4. {
  5.     [SerializeField] UISlider mSlider = null;
  6.         UILabel mLabel;
  7.  
  8.     void Awake ()
  9.     {
  10.         mSlider = GetComponent<UISlider>();
  11.         mSlider.eventReceiver = gameObject;
  12.                 mLabel = GetComponent<UILabel>();
  13.  
  14.     }
  15.  
  16.     void OnSliderChange (float val)
  17.     {
  18.         mLabel.text = val.ToString ();
  19.     }
  20. }
  21.  
  22.  
  23.  

I thought this would point towards the OnSlider FunctionName in the UISlider but it isn't. I am sure there is something simple I am missing here but I just can't see it. Any help?

IIIHAPPYIII

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: Progression slider sending a percentage for UILabel Text
« Reply #2 on: March 14, 2013, 04:16:33 PM »
Alright got it to work:

  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UISlider))]
  4. public class LevelPercentage : MonoBehaviour
  5. {
  6.     UISlider mSlider;
  7.         UILabel mLabel;
  8.  
  9.     void Awake ()
  10.     {
  11.         mSlider = GetComponent<UISlider>();
  12.         mSlider.eventReceiver = gameObject;
  13.                 mLabel = GetComponent<UILabel>();
  14.  
  15.     }
  16.  
  17.     void OnSliderChange (float val)
  18.     {
  19.                 val = val * 100;
  20.         mLabel.text = val.ToString ("0.\\%");
  21.     }
  22. }
  23.  
  24.  
  25.  

If doing something wrong in that let me know, but it did work when attached to the label. Removed my original UISlider component and let it make its own.