More Effective Coroutines are called slightly differently than Unity’s coroutines. The structure is exactly the same, so in most cases it’s just a matter of find and replace inside your code.
First, you need to include the two namespaces that MEC uses. MEC coroutines are defined in the MovementEffects namespace and they also rely on the System.Collections.Generic functionality rather than the System.Collections functionality that unity coroutines use. System.Collections is hardly ever used for anything except Unity’s coroutines, so an easy way to switch is to do a “Find and Replace,” to change System.Collections to System.Collections.Generic and then just change the lines that have errors. So make sure these two using statements are at the top of every C# script that is using MEC coroutines:
using System.Collections.Generic; using MEC;
Next, replace every instance of StartCoroutine, so this
StartCoroutine(_CheckForWin());
..is replaced with RunCoroutine. (You have to pick the execution loop when you define the process, and it defaults to “Segment.Update”.)
Timing.RunCoroutine(_CheckForWin()); // To run in the Update segment. Timing.RunCoroutine(_CheckForWin(), Segment.FixedUpdate); // To run in the FixedUpdate segment. Timing.RunCoroutine(_CheckForWin(), Segment.LateUpdate); // To run in the LateUpdate segment. Timing.RunCoroutine(_CheckForWin(), Segment.SlowUpdate); // To run in the SlowUpdate segment.
The process’ header will then need to be changed as well. It turns from this:
IEnumerator _CheckForWin() { ... }
To this:
IEnumerator<float> _CheckForWin() { ... }
It is a very good idea to get in the habit of always putting an underscore before all coroutine functions. The reason for this is that coroutines (both Unity and MEC ones) have an annoying tenancy to pretend to run correctly but to actually not execute at all if you try to run the function without using RunCoroutine. The “_” before the function helps you to remember to always use “Timing.RunCoroutine(_CheckForWin());” rather than trying to call it using the line “_CheckForWin();”.
Whenever you want to wait for the next frame you can use “yield return Timing.WaitForOneFrame;” or just “yield return 0;”. Both of those statements evaluate to the same thing, so you can pick the one you like better, but I suggest using WaitForOneFrame since it will make your code more easily understandable to anyone who is not familiar with coroutines and/or Unity. So this,
IEnumerator _CheckForWin() { while (_cubesHit < TotalCubes) { WinText.text = "Have not won yet."; yield return null; } WinText.text = "You win!"; }
Would turn into this:
IEnumerator<float> _CheckForWin() { while (_cubesHit < TotalCubes) { WinText.text = "Have not won yet."; yield return Timing.WaitForOneFrame; } WinText.text = "You win!"; }
If you want to pause for some number of seconds rather than just one frame then you can use Timing.WaitForSeconds. So this,
IEnumerator _CheckForWin() { while (_cubesHit < TotalCubes) { WinText.text = "Have not won yet."; yield return new WaitForSeconds(0.1f); } WinText.text = "You win!"; }
Turns into this:
IEnumerator<float> _CheckForWin() { while (_cubesHit < TotalCubes) { WinText.text = "Have not won yet."; yield return Timing.WaitForSeconds(0.1f); } WinText.text = "You win!"; }
Unity’s default coroutines allow you to use yield return as a shorthand syntax to create one coroutine and wait for it to finish before continuing:
IEnumerator _EnableHappyFace() { yield return _CheckForWin(); HappyFaceObject.SetActive(true); }
The above function starts the _CheckForWin coroutine function, and the _EnableHappyFace coroutine function remains paused until _CheckForWin actually finishes, then it enables the HappyFaceObject. The yield return statement above is equivalent to “yield return StartCoroutine(_CheckForWin());”, but Unity calls StartCoroutine behind the scenes for you.
MEC Pro has a shorthand version that calls RunCoroutine automatically as well, but in MEC Free you have to use the longhand version:
IEnumerator<float> _EnableHappyFace() { // This line can replace Unity's default coroutines in both MEC Free and MEC Pro yield return Timing.WaitUntilDone(Timing.RunCoroutine(_CheckForWin()); // This shorthand version only works in MEC Pro yield return Timing.WaitUntilDone(_CheckForWin()); HappyFaceObject.SetActive(true); }