如何异步执行WaitUntil?

Noo*_*001 1 c# wait unity-game-engine async-await

await我在网上找到的async都是教我如何“等待一段时间”,这不是我想要的。

我希望它等到满足某个条件。

只是里尔

yield return new WaitUntil(()=>conditionIsMet);
Run Code Online (Sandbox Code Playgroud)

Coroutine

我想做类似的事情

await Task.WaitUntil(()=>conditionIsMet);
Run Code Online (Sandbox Code Playgroud)

这样的事可能吗?

有人能好心帮助我吗?

非常感谢您的帮助。

Qus*_*zam 5

您可以使用 UniTask 插件,它为 Unity 提供高效的免分配异步/等待集成。

它有很多功能并且很容易使用。首先从这里
下载它,然后将其包含在您的项目中并像这样使用它:

using Cysharp.Threading.Tasks;
using UnityEngine;

public class Test : MonoBehaviour
{
    bool _condition;
    // Start is called before the first frame update
    void Start()
    {
        _ = ProcessAsyncTask();
    }

    public async UniTask ProcessAsyncTask()
    {
        await UniTask.WaitUntil( ()=> ConditionResult());
    }

    public bool ConditionResult()
    {
        // _condition 
        return _condition;
    }
}
Run Code Online (Sandbox Code Playgroud)