天天看点

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解

Update逻辑

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解

IEnumerator/ IEnumerable

public interface IEnumerable  
{  
    IEnumerator GetEnumerator();  
}  
   
public interface IEnumerator  
{  
    bool MoveNext();  
    void Reset();  
   
    Object Current { get; }  
}  
           

在两者的使用上,有下面几点需要注意

1、一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。

2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。

3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现枚举器(iterator);IEnumerator是一个实现式的接口,IEnumerator object就是一个iterator。

4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,client可以通过IEnumerable的GetEnumerator()得到IEnumerator object,在这个意义上,将GetEnumerator()看作IEnumerator object的factory method也未尝不可。

yield return

使用yield语句可以暂停(pause)协同程序的执行,yield的返回值指定在什么时候继续(resume)协同程序。

yield return null; //暂停协同程序,下一帧再继续往下执行

yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdate方法时再继续往下执行

yield return new WaitForSeconds(2);//暂停协同程序,2秒之后再继续往下执行

yield return StartCoroutine("SomeCortoutineMethod");//暂停此协同程序,开启SomeCortoutineMethod协同程序,直到SomeCortoutineMethod里的内容全部搞定。

StartCoroutine

在Unity3D中,使用MonoBehaviour.StartCoroutine方法即可开启一个协同程序,也就是说该方法必须在MonoBehaviour或继承于MonoBehaviour的类中调用。

在Unity3D中,使用StartCoroutine(string methodName)和StartCoroutine(IEnumerator routine)都可以开启一个线程。区别在于使用字符串作为参数可以开启线程并在线程结束前终止线程,相反使用IEnumerator 作为参数只能等待线程的结束而不能随时终止(除非使用StopAllCoroutines()方法);另外使用字符串作为参数时,开启线程时最多只能传递 一个参数,并且性能消耗会更大一点,而使用IEnumerator 作为参数则没有这个限制。

在Unity3D中,使用StopCoroutine(string methodName)来终止一个协同程序,使用StopAllCoroutines()来终止所有可以终止的协同程序,但这两个方法都只能终止该 MonoBehaviour中的协同程序。

还有一种方法可以终止协同程序,即将协同程序所在gameobject的active属性设置为false,当再次设置active为ture时,协同程 序并不会再开启;如是将协同程序所在脚本的enabled设置为false则不会生效。这是因为协同程序被开启后作为一个线程在运行,而 MonoBehaviour也是一个线程,他们成为互不干扰的模块,除非代码中用调用,他们共同作用于同一个对象,只有当对象不可见才能同时终止这两个线 程。然而,为了管理我们额外开启的线程,Unity3D将协同程序的调用放在了MonoBehaviour中,这样我们在编程时就可以方便的调用指定脚本 中的协同程序,而不是无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计保证了对象、脚本的条理化管理,并防止了重 名。

示例1

public class GameManager : MonoBehaviour {

     void Start() 
    { 
       Debug.Log("Starting " + Time.time);
        StartCoroutine(WaitAndPrint(2));
        Debug.Log("Done " + Time.time);
    }

	IEnumerator WaitAndPrint(float waitTime) 
    { 
        yield return new WaitForSeconds(waitTime); 
        Debug.Log("WaitAndPrint " + Time.time);
    }
}
           

运行结果:

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解
public class GameManager : MonoBehaviour {

	IEnumerator Start() 
    {
		Debug.Log("Starting " + Time.time);
        yield return StartCoroutine(WaitAndPrint(2.0F));
		Debug.Log("Done " + Time.time);
    }
	IEnumerator WaitAndPrint(float waitTime) 
    { 
        yield return new WaitForSeconds(waitTime);
		Debug.Log("WaitAndPrint " + Time.time);
    } 
}
           

运行结果:

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解

再看一个yield return StartCoroutine嵌套的例子

void Start () {
        Debug.Log("start1");
        StartCoroutine(Test());
        Debug.Log("start2");
    }

    IEnumerator Test()
    {
        Debug.Log("test1");
        yield return StartCoroutine(DoSomething());
        Debug.Log("test2");
    }

    IEnumerator DoSomething()
    {
        Debug.Log("load 1");
        yield return null;
        Debug.Log("load 2");
    }
           

执行结果:

start1

test1

load1

start2

load2

test2

这种StartCoroutine中嵌套一个yield return StartCoroutine,第一个StartCoroutine会等到第二个StartCoroutine中所有代码结束后再继续执行,而第二个StartCoroutine中的yield语句会先返回第一个,然后立即返回他的调用处,也就是调用处会继续执行,而第一个StartCoroutine会等待第二个执行完再继续执行。

示例二

场景如下,一个球一个平面。

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解

球的控制器

using UnityEngine;
using System.Collections;

public class SphereController : MonoBehaviour {
	private Vector3 target;
	public float smoothing = 7f;

	public Vector3 Target
	{
		get { return target; }
		set
		{
			target = value;

			StopCoroutine("Movement");
			StartCoroutine("Movement", target);
			Debug.Log("Move!");
		}
	}

	IEnumerator Movement(Vector3 target)
	{
		while(Vector3.Distance(transform.position, target) > 0.05f)
		{
			transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
			yield return null;
		}
	}

}
           

下面的脚本拖拽到地面上

using UnityEngine;
using System.Collections;

public class ClickSetPosition : MonoBehaviour {
	public SphereController sphereController;
	// Update is called once per frame
	void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			Physics.Raycast(ray, out hit);

			if (hit.collider.gameObject == gameObject)
			{
				Debug.Log("Clicked!");
				Vector3 newTarget = hit.point + new Vector3(0, 0.5f, 0);
				sphereController.Target = newTarget;
			}
		}
	}
}
           

运行结果

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解

参考

Unity协程(Coroutine)原理深入剖析再续 - http://dsqiu.iteye.com/blog/2049743

协同的理解 - http://www.cnblogs.com/shawnzxx/archive/2013/01/01/2841451.html

yield(C# 参考) - https://msdn.microsoft.com/zh-cn/library/9k7k7cf0.aspx

COROUTINES Unity Tutorial - http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

Unity StartCoroutine 和 yield return 深入研究 -  http://www.cnblogs.com/fly-100/p/3910515.html

Coroutines document - https://docs.unity3d.com/Manual/Coroutines.html