Search Unity

Slider Events / Check value against range of values

Discussion in 'Immediate Mode GUI (IMGUI)' started by Karrzun, May 17, 2018.

  1. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    129
    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):
    1.  
    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;
    7.  
    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

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    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);
    3.  
    4. if (oldIndex != index) {
    5.  
    6.     oldIndex = index;
    7.     ChangeImage(images[index]);
    8. }
     
  3. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    129
    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.