- Home /
fps calculation with while loop in coroutine
Hi, ım trying to call 24times per 1 second but something is wrong.My test code like this;
int frame;
bool stop;
void Start ()
{
StartCoroutine (Test ());
}
IEnumerator StopTest ()
{
yield return new WaitForSeconds (10f);
stop = true;
}
IEnumerator Test ()
{
StartCoroutine (StopTest ());
while (!stop) {
frame++;
yield return new WaitForSeconds (1f / 24f);
}
Debug.Log (frame / 24f);
}
ı should see always "10" on console but my results are like this;
first try 7.583333 second 7.25 7.625...
i dont know what is wrong
Answer by hexagonius · May 23, 2018 at 05:42 PM
The documentation reveals the answer for your problem:
https://docs.unity3d.com/ScriptReference/WaitForSeconds.html
Answer by kyle-misner-kriel · May 23, 2018 at 06:19 PM
Unity can't wait for exactly 1f / 24f of a second. Every frame you'll have slightly different delays which are slightly above (or much more then) 0.0416666667.
Getting your value to reach nearly 10 will require writing some time regulation code, which is less simple.
i was try with system.timers, but i'm getting this error;
Internal_GetWidth can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. UnityEngine.Texture2D:GetPixels()
i need to read texture 24 times in one second for video recording $$anonymous$$y last code like this;
void StartRecord ()
{
myTimer.Elapsed += new ElapsedEventHandler (AddFrames);
myTimer.Interval = 41.6;
myTimer.Start ();
StartCoroutine (StopVideoRecording ());
}
void AddFrames (object sender, System.EventArgs e)
{
Color[] pix_depth = texture.GetPixels ();
system.timers it works well but now i have diffrent problem. Do you have any advice?
Your answer
Follow this Question
Related Questions
yield return new WaitForSeconds () not working 1 Answer
Problem with coroutine 2 Answers
Need Help with a Null Reference 1 Answer