Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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

    Karrzun

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