Unity协程yield return、yield break、WaitUntil等等区别和使用案例

简介

yield returnyield break 是 C# 中迭代器方法的关键字,它们在 Unity 协程中被用来控制协程的执行流程。而 WaitForSecondsWaitUntilWaitWhile 是 Unity 内置的 Yield Instruction 类型,用于实现异步等待特定条件满足时继续执行协程。

1. yield return

  • yield return 语句在协程(IEnumerator)中使用,允许函数在执行过程中暂停并返回一个值给调用者。
  • 当Unity通过 StartCoroutine 调用包含 yield return 的函数时,该函数不会像普通函数那样立即执行到底,而是每遇到一次 yield return 就会暂停并返回当前的“结果”(可以是任意类型,包括各种 Yield Instruction),然后在下一帧或指定条件满足时继续执行后续代码。
  • 示例:yield return new WaitForSeconds(2f); 表示协程将暂停执行2秒后继续。

2. yield break

  • yield break; 在协程中表示终止整个迭代过程,相当于跳出整个协程。
  • yield break; 执行时,协程会立刻停止,不会执行任何剩余的 yield return 语句。

3. WaitForSeconds

  • WaitForSeconds(float seconds) 是一个 Yield Instruction 类型,当它作为 yield return 的返回值时,会让协程暂停指定秒数之后再继续执行。
  • 示例:
    IEnumerator MyCoroutine()
    {
        Debug.Log("Coroutine start");
        yield return new WaitForSeconds(3);
        Debug.Log("Coroutine resumes after 3 seconds");
    }
    
    void Start()
    {
        StartCoroutine(MyCoroutine());
    }
    

4. WaitUntil

  • WaitUntil(Func<bool> predicate) 接收一个布尔函数作为参数,在该函数返回 true 之前,协程会一直暂停执行。
  • 示例:
    bool isPlayerReady = false;
    
    IEnumerator WaitForPlayerReady()
    {
        while (!isPlayerReady)
        {
            yield return new WaitUntil(() => isPlayerReady);
        }
        Debug.Log("Player is now ready!");
    }
    
    public void SetPlayerReady() { isPlayerReady = true; }
    

5. WaitWhile

  • WaitWhile(Func<bool> predicate) 类似于 WaitUntil,但是它会一直等待直到传入的布尔函数返回 false
  • 示例:
    float countdown = 5f;
    
    IEnumerator Countdown()
    {
        while (countdown > 0)
        {
            Debug.Log(countdown.ToString());
            countdown -= Time.deltaTime;
            yield return new WaitWhile(() => countdown > 0);
        }
        Debug.Log("Countdown finished!");
    }
    

6. WaitForEndOfFrame

  • yield return new WaitForEndOfFrame();
  • 使协程在当前帧所有渲染完成后继续执行。这对于需要在每一帧渲染完成后处理逻辑(例如屏幕截图或者某些后期处理效果)非常有用。

7. WaitForFixedUpdate

  • yield return new WaitForFixedUpdate();
  • 让协程等待到下一次 FixedUpdate 更新循环时再继续执行。主要用于物理相关计算或更新,因为Unity的物理引擎是在FixedUpdate中进行更新的。

8. WWW (Unity 5.x及更早版本) 或者 UnityWebRequest

  • 在网络请求完成前暂停协程。
  • 示例(UnityWebRequest):
    using UnityEngine.Networking;
    
    IEnumerator LoadWebData()
    {
        UnityWebRequest www = UnityWebRequest.Get("http://example.com/data.json");
        yield return www.SendWebRequest();
        
        if (www.result == UnityWebRequest.Result.Success)
        {
            Debug.Log(www.downloadHandler.text);
        }
        else
        {
            Debug.LogError(www.error);
        }
    }
    

9. Custom Yield Instructions

  • 用户可以自定义Yield Instruction来实现特定的等待逻辑。
  • 示例:
    public class CustomYieldInstruction : IEnumerator
    {
        public bool MoveNext() { ... } // 实现你的条件判断逻辑
        public void Reset() { throw new NotSupportedException(); } // Unity通常不使用Reset方法
        object IEnumerator.Current => null; // 因为Yield Instruction只需要MoveNext返回值,所以Current返回null
    }
    
    IEnumerator MyCoroutine()
    {
        yield return new CustomYieldInstruction();
        // 当CustomYieldInstruction的MoveNext返回true时,这里将继续执行
    }
    

在Unity中,除了标准的Yield Instruction之外,还有一些不那么常见但同样有用的等待机制:

10. WaitForSecondsRealtime

  • yield return new WaitForSecondsRealtime(float seconds);
  • 类似于WaitForSeconds,但是其计时不受Time.timeScale的影响,即使游戏时间被暂停(如Time.timeScale设置为0),该指令依然会按照真实时间进行等待。

11. Coroutine.WaitForCompletion

  • 从Unity 2019.3开始引入,用于等待另一个协程完成。
  • 示例:
    IEnumerator AnotherCoroutine()
    {
        // ...
    }
    
    IEnumerator WaitForAnotherCoroutine()
    {
        var waitForCompletion = StartCoroutine(AnotherCoroutine());
        yield return waitForCompletion;
        Debug.Log("AnotherCoroutine has finished.");
    }
    

12. Custom Yield Instructions(自定义Yield Instruction)

  • 如前所述,开发者可以创建自定义的Yield Instruction类以实现特定的等待逻辑。例如,等待某个游戏事件的发生、UI动画结束或者其他任何需要异步等待的情况。

13. WaitUntil/WaitWhile with Unity Events

  • 在某些情况下,可以通过Unity Event结合WaitUntilWaitWhile来监听组件事件的发生。
  • 示例:
    public class MyComponent : MonoBehaviour
    {
        public UnityEvent OnEventTriggered;
    
        void Start()
        {
            StartCoroutine(WaitForEvent());
        }
    
        IEnumerator WaitForEvent()
        {
            yield return new WaitUntil(() => OnEventTriggered != null && OnEventTriggered.IsInvoking);
            Debug.Log("Event was triggered!");
        }
    }
    

以上这些方法和机制进一步丰富了Unity中协程的功能和应用场景,让开发者能够根据实际需求灵活地编写异步任务流程。

😍😍 大量H5小游戏、微信小游戏、抖音小游戏源码😍😍
😍😍试玩地址: https://www.bojiogame.sg😍😍
😍看上哪一款,需要源码的csdn私信我😍

​最后我们放松一下眼睛
在这里插入图片描述

  • 35
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极致人生-010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值