前一阵自己看了《unity脚本编程》其中讲到了unity中协程的实现原理,讲的比较难懂。我总结了给个基础点的。
首先是c#中yield关键字
yield 关键字向编译器指示它所在的方法是迭代器块。 编译器生成一个类来实现迭代器块中表示的行为。 在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值。 这是一个返回值,例如,在 foreach 语句的每一次循环中返回的值。 yield 关键字也可与 break 结合使用,表示迭代结束。——msdn
msdn讲的有点抽象,下面看看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace YieldTest
{
class Program
{
static void Main(string[] args)
{
foreach (int i in Feige.Fei())
{
Console.WriteLine("返回的结果是:" + i );
}
}
class Feige
{
public static IEnumerable<int> Fei()
{
for (int i = 0; i < 10; i++)
{
yield return i;
Thread.Sleep(1000);
}
}
}
}
}
结果:
返回的结果是:0
返回的结果是:1
返回的结果是:2
返回的结果是:3
返回的结果是:4
返回的结果是:5
返回的结果是:6
返回的结果是:7
返回的结果是:8
返回的结果是:9
下面这种实现方式也是一样的
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace YieldTest
{
class Program
{
static void Main(string[] args)
{
IEnumerator i = Feige.Fei();
while (i.MoveNext())
{
Console.WriteLine(i.Current);
Thread.Sleep(500);
}
Console.ReadLine();
}
class Feige
{
public static IEnumerator<int> Fei()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}
}
}
}
模拟协程的实现代码
using System.Collections;
using System.Collections.Generic;
namespace Com.Coroutine
{
public class Coroutine
{
internal IEnumerator m_Routine;
internal IEnumerator Routine
{
get { return m_Routine; }
}
internal Coroutine()
{
}
internal Coroutine(IEnumerator routine)
{
this.m_Routine = routine;
}
internal bool MoveNext()
{
var routine = m_Routine.Current as Coroutine;
if (routine != null)
{
if (routine.MoveNext())
{
return true;
}
else if (m_Routine.MoveNext())
{
return true;
}
else
{
return false;
}
}
else if (m_Routine.MoveNext())
{
return true;
}
else
{
return false;
}
}
}
// use this as a template for functions like WaitForSeconds()
public class WaitForCount : Coroutine
{
int count = 0;
public WaitForCount(int count)
{
this.count = count;
this.m_Routine = Count();
}
IEnumerator Count()
{
while (--count >= 0)
{
System.Console.WriteLine(count);
yield return true;
}
}
}
// use this as the base class for enabled coroutines
public class CoroutineManager
{
internal List<Coroutine> m_Coroutines = new List<Coroutine>();
// just like Unity's MonoBehaviour.StartCoroutine
public Coroutine StartCoroutine(IEnumerator routine)
{
var coroutine = new Coroutine(routine);
m_Coroutines.Add(coroutine);
return coroutine;
}
// call this every frame
public void ProcessCoroutines()
{
for (int i = 0; i < m_Coroutines.Count; )
{
var coroutine = m_Coroutines[i];
if (coroutine.MoveNext())
{
++i;
}
else if (m_Coroutines.Count > 1)
{
m_Coroutines[i] = m_Coroutines[m_Coroutines.Count - 1];
m_Coroutines.RemoveAt(m_Coroutines.Count - 1);
}
else
{
m_Coroutines.Clear();
break;
}
}
}
}
}