- Home /
Invoke/yield in non MonoBehaviour classes
Hi guys,
In my game I have a state machine for game states and In one of these states I need to use either `Invoke` or `yield return new WaitForSecinds`. But the problem is that these state classes are non-mono behaviour classes. So I can't call either one of those type of instructions on it. I can't use yield in C# out of the box since it has no WaitForSeconds.
Anyway I managed to figure out a way to do it. But it's really a brute force way of doing things, and it doesn't look that pretty and elegant.
So does anyone have a good solution for this?
Answer by MadDave · Aug 22, 2012 at 10:54 AM
There is no easy solution, unfortunately. The whole co-routing thing is a built-in Unity feature. The yield keyword in C# actually does something completely different; Unity just "abuses" it to implement a fiber-like execution logic.
A few friendly guys have implemented something similar (not exactly the same) you can use in your own code and even in non-Unity projects. It is also really helpful if you want to understand how coroutines work internally. http://wiki.unity3d.com/index.php?title=CoroutineScheduler
Thanks for this. It looks really great I'll have a look at it. Thanks again.