public bool MoveNext() { switch (curState) { case 0: Debug.Log("Star" + index); curDelay = new Delay(time); curState++; return true; case 1: Debug.Log("End" + index); curState++; return true; default: return false; } }
public void Reset() { curState = 0; } } public class TinyCoroutine { public static List coroutines = new List(); public static List removeCoroutines = new List(); public static void CoroutineStart(TinyIEnumerator enumerator) { coroutines.Add(enumerator); } public static void UpdateCoroutines() { removeCoroutines.Clear(); if (coroutines.Count > 0) { foreach (TinyIEnumerator item in coroutines) { if (item.Current == null) { item.MoveNext(); } if (!(item.Current as Delay).CallFun()) { if (!item.MoveNext()) { removeCoroutines.Add(item); } } } foreach (TinyIEnumerator item in removeCoroutines) { coroutines.Remove(item); } } } }
public class Delay { private float time; private float delayTime;
public Delay(float delayTime) { time = 0; this.delayTime = delayTime; }
public bool CallFun() { if (time < delayTime) { time += Time.deltaTime; return true; } else { return false; } }
调用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class NewBehaviourScript : MonoBehaviour { TinyIEnumerator tinyIEnumerator_1 = new TinyIEnumerator(1, 1); TinyIEnumerator tinyIEnumerator_2 = new TinyIEnumerator(2, 5); TinyIEnumerator tinyIEnumerator_3 = new TinyIEnumerator(3, 3); void Start() { TinyCoroutine.CoroutineStart(tinyIEnumerator_1); TinyCoroutine.CoroutineStart(tinyIEnumerator_2); TinyCoroutine.CoroutineStart(tinyIEnumerator_3); } private void Update() { TinyCoroutine.UpdateCoroutines(); } }