jacksondunstan / UnityNativeScripting

Unity Scripting in C++
https://jacksondunstan.com/articles/3938
MIT License
1.34k stars 135 forks source link

How to do Coroutine in c++ #46

Closed sekkit closed 5 years ago

sekkit commented 5 years ago

As the title indicates, is it possible to enable coroutine in c++.

jacksondunstan commented 5 years ago

Yes, but with caveats. You can certainly expose MonoBehaviour.StartCoroutine, call it with an IEnumerator, and use its returned Coroutine. You can even create your own base class implementing IEnumerator in the same way you create a MonoBehaviour derivative. That class' Current and MoveNext can be written in C++ just like how you'd write MonoBehaviour message handlers like Start or OnDestroy in C++. However, that's not as natural a way to write a coroutine as you'd usually do in C#. That's because there's currently no support for iterator functions (i.e. those that yield) in the bindings or in C++ itself. This may change if coroutines make it into C++20.