kingBook

导航

unity 协程与async、await

协程(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) {
        // 必须判断 audioSource.isPlaying, 在连续播放很短的音效时,偶尔会出现不进入播放的情况, 因此判断 audioSource.isPlaying 如果未进入播放则直接销毁
        while (audioSource != null && audioSource.isPlaying) {
            yield return null;
        }
        Destroy(audioSource.gameObject);
    }
    
    private void Update () {
        if (Input.GetMouseButtonDown(0)) {
            PlayEffectAtPoint(audioClip, Camera.main.transform.position, 1f);
        }
    }
}

当在 Start() 中 StartCoroutine()IEnumerator Start(),且 yield return null 时会第2帧 FixedUpdate 后执行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCoroutine : MonoBehaviour {

    private void Awake() {
        StartCoroutine(AwakeCoroutine());
    }

    private void OnEnable() {
        StartCoroutine(OnEnableCoroutine());
    }

    private void Start() {
        StartCoroutine(StartCoroutine());
    }

    /*
     // 此代码与在 Start 函数内 StartCoroutine 等效
     private IEnumerator Start() {
        yield return null;
        Debug.Log("StartCoroutine");
    }*/

    private void FixedUpdate() {
        Debug.Log("FixedUpdate");
    }

    private void Update() {
        Debug.Log("Update");
    }

    private IEnumerator AwakeCoroutine() {
        yield return null;
        Debug.Log("AwakeCoroutine");
    }

    private IEnumerator OnEnableCoroutine() {
        yield return null;
        Debug.Log("OnEnableCoroutine");
    }

    private IEnumerator StartCoroutine() {
        yield return null; // or new WaitForEndOfFrame();
        Debug.Log("StartCoroutine");
    }
}

/* output:
FixedUpdate
Update
AwakeCoroutine
OnEnableCoroutine
FixedUpdate
Update
StartCoroutine
FixedUpdate
...
*/
  • 不能将Awake、OnEnable函数返回类型标记为 IEnumrator,以下代码是错误的
// Script error (TestCoroutine): Awake() can not be a coroutine.
private IEnumerator Awake() {
    yield return null;
    Debug.Log("AwakeCoroutine");
}

// Script error (TestCoroutine): OnEnable() can not be a coroutine.
private IEnumerator OnEnable() {
    yield return null;
    Debug.Log("OnEnableCoroutine");
}

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();
        _sayHelloTokenSource.Dispose();
        _sayHelloTokenSource=null;
    }
    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);
        }
    }

    private void OnDestroy() {
        if (_sayHelloTokenSource != null) {
            _sayHelloTokenSource.Cancel();
            _sayHelloTokenSource.Dispose();
            _sayHelloTokenSource = null;
        }
    }
}

posted on 2019-12-16 16:53  kingBook  阅读(7582)  评论(0编辑  收藏  举报