Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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. }