Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to difference when a slider value is changing manually or automatically?

I’m programming a video player in Unity, and I’m using a Slider as the bar to set the seek of the video.

For this I have created the SeekVideo function that uses the value of the slider to assign it to the video and call it from OnValueChange.

My problem is that when changing the value of the slider also in the "Update" method, the SeekVideo function is called in each frame.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t know if there is any way to differentiate with a flag, for example, if the function has been called manually or automatically.

This is my code:

Unity Editor

public void SeekVideo() {
    Debug.Log("This function is being called every frame");
    float value = slider.value;
    long miliseconds = (long)(GetVideoDuration(videoIndex) * value);
    Seek(0, miliseconds);
}

public void Update() {
    if (isVideoPlayerReady && slider != null) {
        slider.value = (float)GetVideoPosition(videoIndex) / GetVideoDuration(videoIndex);
    }
}

I don’t know if it’s possible, because right now the code makes every frame reset the video seed and it consumes a lot of resources. Any suggestion?

>Solution :

Instead of setting

slider.value = ...;

which will trigger the callback event you can use SetValueWithoutNotify

slider.SetValueWithoutNotify(...);

which as the name says omits the callback

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading