はじめに
「Unity Coroutines Without MonoBehaviours」を Unity プロジェクトに導入することで
MonoBehaviour を使用せずにコルーチンを実行できるようになります
使用例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestRunner : MonoBehaviour { private AsyncProcessor m_processor; public void Start() { m_processor = new AsyncProcessor(); m_processor.Process( Coroutine1() ); m_processor.Process( GetExample1() ); m_processor.Process( GetExample2() ); } private IEnumerator Coroutine1() { yield return WaitABit(); } private IEnumerator WaitABit() { var start = Time.realtimeSinceStartup; while ( Time.realtimeSinceStartup - start < 2 ) { yield return null; } } private IEnumerator GetExample1() { yield return SlowLookup(); } private IEnumerator<string> SlowLookup() { var start = Time.realtimeSinceStartup; while ( Time.realtimeSinceStartup - start < 2 ) { yield return null; } yield return "return value string"; } private IEnumerator GetExample2() { var stringValue = SlowLookup2(); yield return stringValue; } private IEnumerator<string> SlowLookup2() { return CoRoutineUtil.Wrap<string>( SlowLookup2Impl() ); } private IEnumerator SlowLookup2Impl() { yield return WaitABit(); yield return WaitABit(); yield return WaitABit(); yield return "returned string value"; } private void Update() { m_processor.Tick(); } }