首页 > 代码库 > Ienumerable和Ienumerator的使用

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";
	}
}

  

Ienumerable和Ienumerator的使用