【问题标题】:Unity Coroutine Wait Until true for x secondsUnity Coroutine 等到 true 持续 x 秒
【发布时间】:2021-02-16 16:20:48
【问题描述】:

我想实现与yield return new WaitUntil(() => Check()); 类似的东西,但增加了一个额外的功能。在满足Check() 条件后,它应该等待 x 秒检查每一帧,如果条件仍然成立。

这是我的实现:

private IEnumerator CheckCor(float waitTime)
{
    bool checkFlag = true;
    bool checkFlag2;
    float whileTime;
    while (checkFlag)
    {
        yield return new WaitUntil(() => Check());
        checkFlag2 = true;
        whileTime = waitTime;
        while (whileTime > 0)
        {
            if (!Check())
            {
                checkFlag2 = false;
            }
            whileTime -= Time.deltaTime;
            yield return null;
        }
        if (checkFlag2)
        {
            checkFlag = false;
        }
    }
}

Check() 在哪里

private bool Check();

我的实现运行良好,但似乎有点长。

有没有更短的方法来实现相同的行为?

(也使它通用将是一个加号,例如yield return WaitUntilForSeconds(Check(), 3f);,其中 Check() 是条件,3f 是时间检查每个帧的条件。我猜它可以使用CustomYieldInstruction 来完成,但是我不确定它是如何工作的。)

【问题讨论】:

标签: c# unity3d


【解决方案1】:

将其实现为CustomYieldInstruction 还不错。以Func<bool> 的形式传入检查器,保留一个标志以记住您是否已启动计时器,并在检查函数在任何时候返回 false 时重置该标志。您甚至可以接受Func<float> 在计时器重置时调用剩余时间:

using UnityEngine;

public class WaitUntilForSeconds: CustomYieldInstruction
{
    float pauseTime;
    float timer;
    bool waitingForFirst;
    Func<bool> myChecker;
    Action<float> onInterrupt;
    bool alwaysTrue;

    public WaitUntilForSeconds(Func<bool> myChecker, float pauseTime, 
            Action<float> onInterrupt = null)
    {
        this.myChecker = myChecker;
        this.pauseTime = pauseTime;
        this.onInterrupt = onInterrupt;

        waitingForFirst = true;
    }

    public override bool keepWaiting
    {
        get
        {
            bool checkThisTurn = myChecker();
            if (waitingForFirst) 
            {
                if (checkThisTurn)
                {
                    waitingForFirst = false;
                    timer = pauseTime;
                    alwaysTrue = true;
                }
            }
            else
            {
                timer -= Time.deltaTime;

                if (onInterrupt != null && !checkThisTurn && alwaysTrue)
                {
                    onInterrupt(timer);
                }
                alwaysTrue &= checkThisTurn;

                // Alternate version: Interrupt the timer on false, 
                // and restart the wait
                // if (!alwaysTrue || timer <= 0)

                if (timer <= 0)
                {
                    if (alwaysTrue)
                    {
                        return false;
                    }
                    else 
                    {
                        waitingForFirst = true;
                    }
                }
            }

            return true;
        }
    }
}

然后,你可以使用

yield return new WaitUntilForSeconds(Check, 3f);

// or

yield return new WaitUntilForSeconds(Check, 3f, (float t) => {Debug.Log($"Interrupted with {t:F3} seconds left!");});

如果你需要检查器的参数,你可以使用无参数的 lambda:

yield return new WaitUntilForSeconds(() => Check(Vector3.up), 3f);

而且,和 lambda 一样,be aware of any variable capture you do


如果您不想要整个 CustomYieldInstruction,我会使用另一个 WaitUntil 来暂停:

private IEnumerator CheckCor(float waitTime)
{
    bool stillWaiting = true;
    while (stillWaiting)
    {
        yield return new WaitUntil(() => Check());

        float pauseTime = waitTime;
        yield return new WaitUntil(() =>
        {
            pauseTime -= Time.deltaTime;
            stillWaiting = !Check();
            return stillWaiting || pauseTime <= 0;
        });

        if (stillWaiting)
        {
            // Stuff when pause is interrupted goes here
        }
    }

    // Stuff after pause goes here
}

【讨论】:

  • 很好的答案!替代版本是我想要的版本。但是,我认为有一个小错误:应该使用timer 而不是pauseTimepauseTime -= Time.deltaTime; => timer -= Time.deltaTime;if (pauseTime &lt;= 0) => if (timer &lt;= 0)
  • 如果在Check函数中需要参数的另一个例子:yield return new WaitUntilForSeconds(() =&gt; Check(5), 3f);
  • @sswwqqaa 很酷,我修复了这个问题,还添加了一个可选的回调,用于计时器中断时。
猜你喜欢
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2016-02-15
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多