user6066644
user6066644

Reputation:

Function call on slider release?

I use a slider to load historical data into my visualization application. The slider represents a period in time relative to now. So you can slide backwards in time and it will load data.

Currently, however, the data will load whenever the value of the slider changes which means if I want to see 5 hours ago? I have to load everything in between...

The code I am using is listed below:

    history = LoadData.Historical();

    data = GameObject.Find("DataManager").GetComponent<LoadData>();

    slider.maxValue = history.Length - 1;
    slider.value = slider.maxValue;
    slider.onValueChanged.AddListener(delegate { ValueChange(); });

where ValueChange() contains something like this:

    LoadData.Candles(data.bounds, history[(int)slider.value]);

Is there a way that I can only load the data when the value has been changed AND when I am not holding the slider?

Note: I have tried a coroutine method to achieve this, but it only ended in tears and infinite loops. :/

Edit:

I drafted this solution, but it seems OnMouseUp does not work directly on Sliders... Any ideas?

void Start()
{

    slider = gameObject.GetComponent<Slider>();

    history = LoadData.Historical();

    data = GameObject.Find("DataManager").GetComponent<LoadData>();

    slider.maxValue = history.Length - 1;
    slider.value = slider.maxValue;
    slider.onValueChanged.AddListener(delegate { ValueChange(); });

    LoadData.Candles(data.bounds, history[(int)slider.maxValue]);

    valueChanged = false;
}

void OnMouseUp()
{
    Debug.Log("UP");
    if (valueChanged)
    {
        LoadData.DestroyCandles();
        LoadData.Candles(data.bounds, history[(int)slider.value]);
    }
    valueChanged = false;
}

void ValueChange()
{
    valueChanged = true;
    Debug.Log(valueChanged);
}

Edit2:

enter image description here

Upvotes: 0

Views: 4186

Answers (1)

yes
yes

Reputation: 967

You could use the event system to capture the MouseUp event like below. It should do the trick, but you will need to account for if the value changed or not yourself (just store the old value and check if its not equal)

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;

    public class SliderOnPointerUpExample : MonoBehaviour, IPointerUpHandler {

        Slider slider;
        float oldValue;

        void Start() {
            slider = GetComponent<Slider>();
            oldValue = slider.value;
        }

        public void OnPointerUp(PointerEventData eventData) {
            if(slider.value != oldValue) {
                Debug.Log("Slider value changed from " + oldValue + " to " + slider.value);
                oldValue = slider.value;
            }
        }
    }

idk if its the best solution, a solution nevertheless (but probably not the worst either) :D

Upvotes: 3

Related Questions