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

在unity C#中yield(中断)语句必须要在IEnumerator类型里。

C#方法,方法的返回类型为IEnumerator,返回值如(eg: yield return new WaitForSeconds(2); 或者 yield return null;)。
yield不可以在Update或者FixedUpdate里使用。

yield就像是一个红绿灯,在满足紧跟在它后面的条件之前,这个协程会挂起,把执行权交给调用它的父函数,满足条件时就可以执行yield下面的代码。

Normal coroutine updates are run after the Update function returns. A coroutine is function that can suspend its execution (yield) until the given given YieldInstruction finishes. Different uses of Coroutines:

yield;   Wait all Update functions called,The coroutine will continue on the next frame.
yield WaitForSeconds(2);   Continue after a specified time delay, after all Update functions have been called for the frame
yield WaitForFixedUpdate();   Continue after all FixedUpdate has been called on all scripts
yield WWW;   Continue after a WWW download has completed.
yield StartCoroutine(MyFunc);    Chains the coroutine, and will wait for the MyFunc coroutine to complete first.

yield return 跟 return 的区别:return 之后不会再返回到 return 后面的语句继续执行。

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

运行结果:

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

运行结果:

示例二

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

球的控制器

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

运行结果

参考

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

时间: 2024-08-04 09:17:35

IEnumerator/ IEnumerable/ yield return/ StartCoroutine 详解的相关文章

return view详解(转载)

1.return View(); 返回值类型:System.Web.Mvc.ViewResult将视图呈现给响应的 View() 结果. 注释 View() 类的此方法重载将返回一个具有空 ViewName 属性的 ViewResult 对象. 如果你正在编写控制器操作的单元测试,则需为那些不采用字符串视图名称的单元测试考虑到空 ViewName 属性. 在运行时,如果 ViewName 属性为空,则将使用当前操作名称取代 ViewName 属性. 2.return View(object mo

c# yield关键字原理详解

c# yield关键字的用法 1.yield实现的功能 yield return: 先看下面的代码,通过yield return实现了类似用foreach遍历数组的功能,说明yield return也是用来实现迭代器的功能的. using static System.Console; using System.Collections.Generic; class Program { //一个返回类型为IEnumerable<int>,其中包含三个yield return public stat

python中yield的用法详解——最简单,最清晰的解释

本文收自:冯爽朗 ,下面有博主名片 个人分类: python 首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂的,我就想问没有有考虑过读者的感受. 接下来是正题: 首先,如果你还没有对yield有个初步分认识,那么你先把yield看做"return",这个是直观的,它首先是个return,普通的return是什么意思,就是在程序中返回某

Python中yield的用法详解

首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂的,我就想问没有有考虑过读者的感受. 接下来是正题: 首先,如果你还没有对yield有个初步分认识,那么你先把yield看做"return",这个是直观的,它首先是个return,普通的return是什么意思,就是在程序中返回某个值,返回之后程序就不再往下运行了.看做return之后再把它

C#break ,continue, return区别详解 [转载]

C#编程语法中break ,continue, return这三个常用的关键字的学习对于我们编程开发是十分有用的,那么本文就向你介绍break ,continue, return具体的语法规范. C#编程语法中我们会碰到break ,continue, return这三个常用的关键字,那么关于这三个关键字的使用具体的操作是什么呢?我们在使用这三关键字的时候需要注意和需要理解的规则是什么呢?让我们开始介绍吧: 一.C#编程语法之break语句: break语句会使运行的程序立刻退出包含在最内层的循

javascript return false 详解

在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页. Return False 就相当于终止符,Return True 就相当于执行符. 在js中return false的作用一般是用来取消默认动作的.比如你单击一个链接除了触发你的 onclick时间(如果你指定的话)以外还要触发一个默认事件就是执行页面的跳转.所以如果 你想取消对象的默认动作就可以return false. <input ty

return view 详解 MVC

1.return View(); 返回值 类型:System.Web.Mvc.ViewResult将视图呈现给响应的 View() 结果. 注释 View() 类的此方法重载将返回一个具有空 ViewName 属性的 ViewResult 对象. 如果你正在编写控制器操作的单元测试,则需为那些不采用字符串视图名称的单元测试考虑到空 ViewName 属性. 在运行时,如果 ViewName 属性为空,则将使用当前操作名称取代 ViewName 属性. 2.return View(object m

Unity StartCoroutine 和 yield return 深入研究

StartCoroutine和yield return表面意思很好理解,StartCoroutine就是开启一个协程,yield return 是迭代器块返回调用迭代的地方. 是吧?不知道你什么感觉,反正我觉得,还是需要深入研究一下的.OK,here we go! 首先,先看一下StartCoroutine在Unity官方的解释. 意思是:一个协程的执行可以在任何地方用yield语句来暂停,yield return的值决定了什么时候协程恢复执行.协程在协调在几帧中执行的操作时有极大的用处.协程几

IEnumerable和IEnumerator详解

引言 IEnumerable是可枚举的所有非泛型集合的基接口, IEnumerable包含一个方法GetEnumerator(),该方法返回一个IEnumerator:IEnumerator提供通过Current属性以及MoveNext()和Reset()方法来循环访问集合的功能. IEnumerable 接口 公开枚举数,该枚举数支持在非泛型集合上进行简单迭代.接口源码如下: public interface IEnumerable { [DispId(-4), __DynamicallyIn