August 6, 2019
Controlling Coroutines
More Advanced Coroutines in Unity
Youtube – Unity3D – 2 Ways to Start & Stop Coroutines (the good & bad ways)
By: Unity3d College
Youtube – Unity3D – Using Delegates / Actions as Callbacks in Coroutines
By: Unity3d College
1st Tutorial
This first tutorial is one I’ve looked at before, and it makes more sense now when I want more accurate control of a coroutine in Unity. Basically you can create an IEnumerator variable that can just hold the coroutine you want to run. This same variable can be checked for being null as a way to determine if it is already running, which can be helpful to ensure only one instance of the coroutine is running at a time.
This also suggests a different approach than normal for creating a more accurate timer in a Unity coroutine. Instead of counting a timer up or down with Time.DeltaTime, they set a float value to Time.time when the coroutine is started, and then check if difference of the current time (Time.time) and that set start time is greater than your intended timer value.
2nd Tutorial
This was a bit more advanced, and I need to look more into Unity Actions to fully understand how this works, but I think I got the general concept. It appears the basic premise is using an IEnumerator which takes on a method (methods) as a delegate input to ensure that a certain step is completed before running the input delegate (method(s)).
For example here, they wanted to replace an image but they wanted to ensure that the www object was obtained first to use for that method. Since a delegate was used, they also showed the flexibility of how the same foundation could support different methods.
What I am a bit confused by here, is how the ReplaceImage method that is set as the delegate in the LoadImage IEnumerator gets its input. It requires an input of Texture2D but I don’t see if or how that is set. I believe this may be my lack of knowledge on either Actions or Delegates in Unity, so I’ll need to look into that.