Ienumerable和Ienumerator的使用

using UnityEngine;
using System.Collections;

public class TestCoroutine : MonoBehaviour {
	void Start () {
		StartCoroutine (test());
		print ("_______________________________");
		TestIenumerator ();
		print ("_______________________________");
		TestIenumerable();
		print ("_______________________________");
	}
	void TestIenumerable(){
		IEnumerable t = a ();
		IEnumerator tp = t.GetEnumerator ();
		foreach(string k in t){
			print (k);
		}
	}
	void TestIenumerator(){
		IEnumerator e = YieldSomeStuff();
		while(e.MoveNext())
		{
			print(string.Format("while: {0}",e.Current));
		}
	}
	IEnumerator test(){
		print ("test");
//		yield break;
		yield return new WaitForSeconds(1);
		print ("test2");
	}
	IEnumerator YieldSomeStuff()
	{
		yield return new WaitForSeconds (1);
		print ("____________");
		yield return "hello";
		print ("____________");
		yield return "world";
		print ("____________");
	}
	IEnumerable a(){
		yield return "Tests";
		yield return "Tests";
		yield return "Tests";
	}
}

  

posted on 2016-11-14 14:25  魔天天  阅读(390)  评论(0编辑  收藏  举报