####协程(Coroutine) 协程就像一个函数,可以暂停执行并将控制权返还给 Unity,而后在指定的时间继续执行。 协程本质上是一个用返回类型 IEnumerator 声明的函数,并在主体中的某个位置包含 yield return 语句。 yield return 是暂停执行并随后在下一个时间点恢复。 注意: Fade 函数中的循环计数器可以在协程的生命周期内保持正确值。实际上,在 yield 语句之间能够正确保留任何变量或参数。canvas
禁用 MonoBehaviour 时,不会中止协程,仅在明确销毁 MonoBehaviour 时才会中止协程。 能够使用 MonoBehaviour.StopCoroutine 和 MonoBehaviour.StopAllCoroutines 来中止协程。 销毁 MonoBehaviour 时,也会中止协程。 MonoBehaviour所绑定的GameObject,SetActive(false)时,也会中止协程async
using UnityEngine; using System.Collections; public class Test:MonoBehaviour{ private CanvasGroup m_canvasGroup; private void Start(){ m_canvasGroup=GetComponent<CanvasGroup>(); StartCoroutine(Delay()); //StartCoroutine(Fade()); } IEnumerator Delay(){ Debug.Log("暂停执行5秒"); yield return new WaitForSeconds(5); Debug.Log("等待完成"); } IEnumerator Fade(){ for (float f=1f;f>=0;f-=0.1f){ m_canvasGroup.alpha=f; Debug.Log(m_canvasGroup.alpha); //yield return new WaitForFixedUpdate();//等待,直到下一个固定帧率更新函数 //yield return null;//等待下一帧执行,在Update后,LateUpdate前。**注意:null、任意数字、字符串、true、false效果同样** //yield return new WaitForEndOfFrame();//等待,直到该帧结束,在渲染全部摄像机和 GUI 以后,在屏幕上显示该帧以前,LateUpdate后。 //yield return new WaitForSecondsRealtime(5);//使用未缩放时间将协同程序执行暂停指定的秒数。 //yield return new WaitForSeconds(5);//使用缩放时间将协程执行暂停指定的秒数。 //yield return new WaitWhile(() => frame < 10);//暂停协程执行,直到提供的委托评估为 /false/。 //yield return new WaitUntil(() => frame >= 10);//暂停协程执行,直到提供的委托评估为 /true/。 yield return null; } Debug.Log("complete"); } }
####async、await函数
using System; using System.Threading.Tasks; using UnityEngine; public class Test:MonoBehaviour{ private CanvasGroup m_canvasGroup; private void Start(){ m_canvasGroup=GetComponent<CanvasGroup>(); //Delay(); Fade(); } private async void Delay(){ Debug.Log("暂停执行5秒"); int ms=5000; await Task.Delay(ms); Debug.Log("等待完成"); } private async void Fade(){ for (float f=1f;f>=0;f-=0.1f){ m_canvasGroup.alpha=f; Debug.Log(m_canvasGroup.alpha); int ms=Convert.ToInt32(Time.deltaTime*1000f); await Task.Delay(ms); } Debug.Log("complete"); } }
延时过程当中取消code
using System; using System.Threading; using System.Threading.Tasks; using UnityEngine; public class Test:MonoBehaviour{ private CancellationTokenSource _sayHelloTokenSource; private void Start () { _sayHelloTokenSource=new CancellationTokenSource(); //延时5秒输出"Hello" delaySayHello(5000,_sayHelloTokenSource); //在2秒的时候取消输出"Hello" delayDestroyTask(2000); // Debug.Log("Start"+","+Time.time); } private async void delayDestroyTask(int ms){ await Task.Delay(ms); Debug.Log("cancel delay say hello"+","+Time.time); _sayHelloTokenSource.Cancel(); } private async void delaySayHello(int ms,CancellationTokenSource tokenSource){ try{ await Task.Delay(ms,tokenSource.Token); }catch (Exception){ } if(!tokenSource.IsCancellationRequested){ Debug.Log("Hello"+","+Time.time); } } }