What is the best way to loop with very short and accurate interval?
We want to create a special video player in Unity and we need to draw the video frame according to its FPS, like 30, 50, 60 or even 120. The video frames are decoded from native plugin and their corresponding textures are returned to Unity. So we should draw and update the texture in Unity infinitely with a short and accurate loop interval such as 33.3ms for 30FPS, 16.7ms for 60FPS, 8.3ms for 120FPS, and so on. In order to achieve this, which of the following methods should we use?
FixedUpdate()
with variableTime.fixedDeltaTime
Coroutines with
yield return new WaitForSecondsRealTime()
InvokeRepeating()
Some other methods that we don't know yet.
We cannot use Thread
s with Thread.Sleep()
because we should call some Unity APIs to do the rendering that are limited to be called on Unity's main thread.
We have tested FixedUpdate
and Coroutines but it seems their intervals are limited by the frequency of Update()
, which is related to the device's screen refresh rate. For example, if we tested it on an OnePlus 7 Pro, since the refresh rate is 90Hz, the Update()
is called per 11ms approximately. Then if the video's FPS is 30, since 33 can be divided by 11, it's OK. But when we switch to a 60FPS video, the rendering interval cannot be 16.7ms, but 22ms instead, which makes the video looks laggy.
So which is the most accurate and stable way to loop with such a short and variable interval?
Your answer
Follow this Question
Related Questions
What to do for many comparison operations in foreach loop? 0 Answers
Unity Freezing after for loop 1 Answer
Execute coroutine in Update() 8 Answers
Coroutine is called twice less time than update 1 Answer
Round Loop Problem?... 1 Answer