public class RequestAsync : IEnumerator
{
private WWW mWWW;
private int i = 0;
public object Current
{
get;
set;
}
public bool MoveNext()
{
Debug.Log("time frame processing : " + Time.frameCount);
if(i == 10)
{
return false;
}
else
{
i++;
return true;
}
}
public void Reset()
{
}
}
public class Test : MonoBehaviour {
void Start () {
StartCoroutine(CoLoad());
}
IEnumerator CoLoad()
{
Debug.Log("time frame start: " + Time.frameCount);
RequestAsync request = new RequestAsync();
yield return request;
Debug.Log("time frame over: " + Time.frameCount);
}
}
输出:
如上代码,我们实现了协程中自定义等待的请求,主要就是将自定请求继承于IEnumerator后,实现里面的Current,MoveNext,
Reset. 其中MoveNext返回true表示继续等待,返回false表示可以继续执行. 然后我们就可以按找我们的特定要求来决定是否让yield return返回,继续执行后面的代码。我们这里是当i==10的时候返回。
注意:就算我们让MoveNext直接返回false, 此时日志time frame start 和 time frame over 之间也是差了一帧的,所以不能用这种方法来实现同步加载!!!!