问题
One of the comments on this thread: Checking condition and calling continuous method with periods of delay unity, said that:
Never never ever use coroutines. They teach bad habits from the point of view as a c# developer and will lead to a lynching if you take a regular c# job
My question is, why is this? Is this just in Unity or in general? Unity's official virtual reality samples https://www.assetstore.unity3d.com/en/#!/content/51519 use them very heavily (especially the flyer example) instead of Invoke or Invoke Repeating, these sample projects were released recently as well.
回答1:
This is very much a Unity thing, though the advice is applicable anywhere.
Developers do not expect iterating an IEnumerable
to change a program's state. It's not what IEnumerable
is intended for and is an inconsistent behavior with all of .NET.
What Unity is doing is using yield return
as a pseudo-coroutine, and it's going to cause confusion for any devs unfamiliar with that.
In modern C# we have async/await to accomplish what Unity intended, but Unity was made before that feature was available.
来源:https://stackoverflow.com/questions/35816905/c-sharp-why-shouldnt-i-ever-use-coroutines