協程(Coroutine)
協程就像一個函數,能夠暫停執行並將控制權返還給 Unity,然后在指定的時間繼續執行。
協程本質上是一個用返回類型 IEnumerator 聲明的函數,並在主體中的某個位置包含 yield return 語句。
yield return 是暫停執行並隨后在下一個時間點恢復。
注意:
Fade 函數中的循環計數器能夠在協程的生命周期內保持正確值。實際上,在 yield 語句之間可以正確保留任何變量或參數。
禁用 MonoBehaviour 時,不會停止協程,僅在明確銷毀 MonoBehaviour 時才會停止協程。
可以使用 MonoBehaviour.StopCoroutine 和 MonoBehaviour.StopAllCoroutines 來停止協程。
銷毀 MonoBehaviour 時,也會停止協程。
MonoBehaviour所綁定的GameObject,SetActive(false)時,也會停止協程
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");
}
}
協程示例:在音效播放完成銷毀 AudioSource 所在綁定的游戲對象。
using System.Collections;
using UnityEngine;
public class TestDestroyAudioSourceOnComplete : MonoBehaviour {
public AudioClip audioClip;
public void PlayEffectAtPoint (AudioClip clip, Vector3 position, float volume) {
GameObject gameObj = new GameObject("One shot audio (AudioManager)");
gameObj.transform.position = position;
AudioSource audioSource = gameObj.AddComponent<AudioSource>();
audioSource.volume = volume;
audioSource.loop = false;
audioSource.clip = clip;
audioSource.playOnAwake = true;
audioSource.Play();
StartCoroutine(DestroyAudioSourceOnComplete(audioSource));
}
private IEnumerator DestroyAudioSourceOnComplete (AudioSource audioSource) {
while (audioSource.time < audioSource.clip.length) {
yield return null;
}
Destroy(audioSource.gameObject);
}
private void Update () {
if (Input.GetMouseButtonDown(0)) {
PlayEffectAtPoint(audioClip, Camera.main.transform.position, 1f);
}
}
}
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");
}
}
延時過程中取消
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);
}
}
}