So I’ve had a quick look around and I’ve seen some posts about not using loops in IEnumerators, however after reading the information available on the scripting reference there’s nothing that I can see about them being particularly resource heavy or incorrect, so I figured I’d ask here.
This is the script I’m using for testing:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class iEnumeratorTest : MonoBehaviour
{
public bool isRecording;
public float recordingTime;
public float timeTaken;
public float tickTime;
void Update()
{
if (isRecording)
{
recordingTime += Time.deltaTime;
}
else
{
if (recordingTime != 0f)
{
Debug.Log("That took: " + recordingTime);
recordingTime = 0f;
}
}
}
public void ButtonClick()
{
StartCoroutine(Tick());
}
IEnumerator Tick()
{
isRecording = true;
for (float f = timeTaken; f >= 0; f -= tickTime)
{
yield return new WaitForSeconds(tickTime);
}
isRecording = false;
}
}
Update() returns values roughly 10-15% higher than the time it should take to wait. For example, when tickTime was set to 0.1 and timeTaken set to 5, results returned varied between 5.15234 and 5.657412. The results have been similar when using both while and for loops and the actual time is closer to the desired time when increasing tickTime and decreasing timeTaken. This wouldn’t be such a big deal however as the wait times are intended to increase dramatically towards the end 10-15% is a great deal of time to be wasted.