Search code examples
c#unit-testingunity-game-engineunity-webgl

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.

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