Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Slider Events / Check value against range of values

Discussion in 'Extensions & OnGUI' started by Karrzun, May 17, 2018.

  1. Karrzun


    Oct 26, 2017
    Hi everyone!

    I'm currently working on a game/program where the user can dynamically set the time of day on his own. That means he or she can change it whenever they want to whatever value (within a given range) they want. This is done by dragging a UI Slider but to minimize the impact of the element on the screen I want to collapse the slider and instead show an image which gives Information about the current value of the slider (e.g. a bright sun for noon, a moon for night, etc) when it isn't needed anymore. Then, if the user wants to change the value again, he or she can hover over the image and by that expand the slider again.
    Up until now, that shouldn't be a problem. But I'm using the exact float values of the slider for my time of day, whereas the images to show the current value are of course linked to ranges of values. Using the onValueChanged Event I could just get the current slider value and (using the normalized value here) run it through a bunch of if statements for each different image I'm using. Something like this:

    Code (csharp):
    2. float v = Slider.value;
    3. if (v < 0.25f) return dawn;
    4. if (v < 0.50f) return noon;
    5. if (v < 0.75f) return dusk;
    6. return night;
    However, to me that seems like a kind of dirty way to do it. Instead I'd like to use something like an onValueChanged event, that only triggers if I enter a new range of values. For example, if I move the slider down from 0.70 I do adjust the lightlevel but I don't want to check my image for updates until I fall below 0.50.
    Are there any events to achieve this?

    Thank you in advance!

    // Edit:
    Also, I'm an idiot because I obviously can't post into the right subforum. This thread belongs into the new UI section "Unity UI & Text Mesh Pro". Maybe a mod could move it? Thank you.
    Last edited: May 17, 2018
  2. CDF


    Sep 14, 2013
    convert the value into an array index, check the old index vs new if you want to dispatch an event, or handle updates. Something like this perhaps:

    Code (CSharp):
    1. var images = [dawn, noon, dusk, night]
    2. int index = Mathf.Max(0, Mathf.CeilToInt(Slider.value * images.Length) - 1);
    4. if (oldIndex != index) {
    6.     oldIndex = index;
    7.     ChangeImage(images[index]);
    8. }
  3. Karrzun


    Oct 26, 2017
    I appreciate the effort but, if I understand you correctly, that still forces me to check for an image update in every onValueChanged event - which is what I wanted to avoid.