I have some questions about coroutines and how they’re used in the lecture “How coroutines work” and “Wait for Scene to Load”.
I tried to gather some information about IEnumerators, and also found out that IEnumerators are closely related to IEnumerables. Tell me if I’m right: IEnumerables are objects which can be “counted”, and in order to be counted they need a class which count them, the Enumerator indeed (a class which implements IEnumerator’s interface). So I’m assuming that there is no IEnumerator without a IEnumerables and viceversa.
How is this handled with coroutines? Is the object “coroutine”, in the Unity system, implementing the IEnumerable interface (I mean behind the scenes)? So is this why we need to return an IEnumerator when using a coroutine?
In the example of Sam, at a certain point the GetPringles() method calls “GetEnumerator”, which is a method needed in the implementation of the IEnumerable interface. I don’t see where it is implemented, tho. Maybe in the class (that we can’t see) which implements the HungryChild method? Which, I suppose, will also derive from the IEnumerable interface?
So, considering all these IEnumerators, IEnumerable and coroutines and yield return, how are all of these implemented in Unity? I’ve not found anything really “deep” in the web. Can you help me understand them better (and in a simple way, possibly )
In regards of the lecture about the loading scene, I’ve not fully understood the need to use coroutines to load the next scene. Do we want to wait for the scene to be loaded otherwise the “Portal” we are looking for would be searched in the same frame and, so, the returned portal would be in the same scene, so basically…the same portal?
If we return an IEnumerator object with our implemented coroutine, how can we satisfy this condition when returning something like “yield return SceneManager.LoadSceneAsync(sceneToLoad);”? It’s a method call, so I suppose that this method, other than loading asynchronously the scene, also return some kind of IEnumerator? Or does Unity “adapt” our class behind the scenes making it automatically implementing the IEnumerator interface?
In other words, the question is: if I yield return something, does this “something” has to be an IEnumerator? Or (maybe) the sole fact to use the keyword ‘yield return’ satisfies the need of returning an IEnumerator (since it will do some kind of code behind the scenes)?
Sorry for these questions but I basically got the argument of IEnumerable and IEnumerator interfaces with yield return statements and why someone should use them in C# (custom iteration and stateful iteration), but it’s not entirely clear to me how do they work in Unity’s coroutines.
Thanks in advance and best