Created
August 25, 2015 14:01
-
-
Save liyonghelpme/87754c95e4a2d4e1f4c9 to your computer and use it in GitHub Desktop.
csharp实现的嵌套协程
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace TestIENumerator | |
{ | |
public class DelayCall { | |
static List<Coroutine> queue = new List<Coroutine>(); | |
public static void Run() { | |
var p = queue[0]; | |
queue.RemoveAt(0); | |
if(p != null) { | |
p.Run(); | |
}else { | |
Console.WriteLine("WaitForSecond"); | |
} | |
} | |
public static void AddCall(Coroutine c, int t){ | |
for(int i = 0; i < t; i++){ | |
queue.Add(null); | |
} | |
queue.Add(c); | |
} | |
public static int Count(){ | |
return queue.Count; | |
} | |
} | |
public class WaitForSecond{ | |
public int time; | |
public WaitForSecond(int t) { | |
time = t; | |
} | |
} | |
public class Coroutine { | |
Coroutine waitFor; | |
Coroutine continueWhenFinished; | |
IEnumerator runObj; | |
public void Run() | |
{ | |
var ret = runObj.MoveNext(); | |
if(!ret) { | |
if(continueWhenFinished != null) { | |
continueWhenFinished.waitFor = null; | |
var c = continueWhenFinished; | |
continueWhenFinished = null; | |
c.Run(); | |
} | |
return; | |
} | |
ProcessCoroutineCurrent(); | |
} | |
void ProcessCoroutineCurrent(){ | |
var cur = runObj.Current; | |
Console.WriteLine("CurrentValue "+cur); | |
if(cur == null) { | |
DelayCall.AddCall(this, 0); | |
return; | |
} | |
HandleIEnumerableCurrentReturnValue(cur); | |
} | |
void HandleIEnumerableCurrentReturnValue(object cur){ | |
if(cur.GetType() == typeof(WaitForSecond)){ | |
DelayCall.AddCall(this, (cur as WaitForSecond).time); | |
return; | |
} | |
if(cur.GetType() == typeof(Coroutine)){ | |
var child = cur as Coroutine; | |
waitFor = child; | |
child.continueWhenFinished = this; | |
return; | |
} | |
DelayCall.AddCall(this, 0); | |
} | |
public static Coroutine StartCoroutine(IEnumerator ie){ | |
var c = new Coroutine(); | |
c.runObj = ie; | |
c.Run(); | |
return c; | |
} | |
} | |
class MainClass | |
{ | |
static IEnumerator Test2() { | |
yield return new WaitForSecond(1); | |
yield return 3; | |
} | |
public static IEnumerator Test(){ | |
yield return 1; | |
yield return 2; | |
yield return Coroutine.StartCoroutine(Test2()); | |
yield return 4; | |
} | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
/* | |
var ie = Test(); | |
var ret = true; | |
while(ret = ie.MoveNext()) { | |
var cur = ie.Current; | |
Console.WriteLine(cur); | |
} | |
*/ | |
var ie = Test(); | |
Coroutine.StartCoroutine(ie); | |
while(DelayCall.Count() > 0) { | |
DelayCall.Run(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment