团结 |如何在不延迟游戏的情况下让事情在 10 秒后发生

Unity | how to make something happen after 10 seconds without delaying game

你好,我一直无法解决这个问题。基本上我想在 10 秒后发生一些事情,而不延迟启动功能或使用更新功能减慢帧速度。我是团结的新手,所以如果有什么我需要提供的,请告诉我。谢谢!

有很多方法!这里有几个例子:

  1. 使用 Unity 协程 (https://docs.unity3d.com/Manual/Coroutines.html)
    void Start()
    {
        StartCoroutine(DoSomethingAfterTenSeconds());
    }

    IEnumerator DoSomethingAfterTenSeconds()
    {
        yield return new WaitForSeconds(10);

        // now do something
    }
  1. 使用FixedUpdateUpdate等待10秒:
    private float _delay = 10;

    public void FixedUpdate()
    {
        if (_delay > 0)
        {
            _delay -= Time.fixedDeltaTime;

            if (_delay <= 0)
            {
                // do something, it has been 10 seconds
            }
        }
    }
  1. 使用 async/await 而不是协程 (https://forum.unity.com/threads/c-async-await-can-totally-replace-coroutine.1026571/)