IEnumerator/ IEnumerable

IEnumerator/ IEnumerablepublic 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 frameyield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scriptsyield 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中,这样我们在编程时就可以方便的调用指定脚本 中的协同程序,而不是无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计保证了对象、脚本的条理化管理,并防止了重 名。

示例1public 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 framevoid 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)原理深入剖析再续 –

协同的理解 –

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

COROUTINES Unity Tutorial –

发光并非太阳的专利,你也可以发光

IEnumerator/ IEnumerable

相关文章:

你感兴趣的文章:

标签云: