public IEnumerator TestExecuteRepeated1() { var counter = 0; { MonoBehaviour myMonoBehaviour = CreateSomeMonoBehaviour(); myMonoBehaviour.ExecuteRepeated(() => { counter++; return(counter < 3); // stop repeated execution once 3 is reached }, delayInSecBetweenIterations: 0.3f, delayInSecBeforeFirstExecution: .2f); } { // while the repeated task is running check over time if the counter increases: yield return(new WaitForSeconds(0.1f)); Assert.AreEqual(0, counter); yield return(new WaitForSeconds(0.2f)); Assert.AreEqual(1, counter); yield return(new WaitForSeconds(0.3f)); Assert.AreEqual(2, counter); yield return(new WaitForSeconds(0.3f)); Assert.AreEqual(3, counter); yield return(new WaitForSeconds(0.3f)); Assert.AreEqual(3, counter); } }
public IEnumerator ExampleUsage1() { MonoBehaviour myMonoBehaviour = CreateSomeMonoBehaviour(); // Execute a task after a defined time: myMonoBehaviour.ExecuteDelayed(() => { Log.d("I am executed after 0.6 seconds"); }, delayInSecBeforeExecution: 0.6f); // Execute a task multiple times: myMonoBehaviour.ExecuteRepeated(() => { Log.d("I am executed every 0.3 seconds until I return false"); return(true); }, delayInSecBetweenIterations: 0.3f, delayInSecBeforeFirstExecution: .2f); yield return(null); }
public IEnumerator TestExecuteRepeated2() { var counter = 0; { MonoBehaviour myMonoBehaviour = CreateSomeMonoBehaviour(); myMonoBehaviour.ExecuteRepeated(() => { counter++; return(true); // the function will never tell the loop to stop }, delayInSecBetweenIterations: 0.1f, repetitions: 3); // stop repeated execution once 3 is reached Assert.AreEqual(1, counter); // no delayInSecBeforeFirstExecution is set to task will instantly be executed } { // while the repeated task is running check over time if the counter increases: yield return(new WaitForSeconds(0.15f)); Assert.AreEqual(2, counter); yield return(new WaitForSeconds(0.1f)); Assert.AreEqual(3, counter); yield return(new WaitForSeconds(0.1f)); Assert.AreEqual(3, counter); // the counter should only increase 3 times we set repetitions: 3 } }