HaZcker Asked:2020-01-01 18:26:22 +0000 UTC2020-01-01 18:26:22 +0000 UTC 2020-01-01 18:26:22 +0000 UTC Unity 中的函数,例如 JavaScript 中的 SetInterval 772 您需要每隔一定的第 n 个时间段调用该函数。 例如:如果n == 0.5,则每 0.5 秒(半秒)调用一次 func1()。 如何轻松简单地做到这一点?例如在 JS 中有一个函数SetInterval,在 C# Unity 中有这样的东西吗? c# 2 个回答 Voted Suvitruf - Andrei Apanasik 2020-01-01T19:05:42Z2020-01-01T19:05:42Z 统一有StartCoroutine。 创建你的方法: private IEnumerator Func1() { // бесконечный цикл for (;;) { yield return new WaitForSeconds(0.5f); // этот код будет выполняться каждые полсекунды // а здесь какое-то условие для выхода из цикла } } 并在您的MonoBehaviour脚本中运行StartCoroutine: StartCoroutine(Func1()); Func1将每半秒执行一次。 Best Answer Yaroslav 2020-01-01T22:25:20Z2020-01-01T22:25:20Z 选项通过Time.deltaTime. float DelayTimer = 0; float Delay = 0.5f; void Update () { DelayTimer += Time.deltaTime; if (DelayTimer >= Delay) { DelayTimer -= Delay; GetOverHere(); } }
统一有
StartCoroutine。创建你的方法:
并在您的
MonoBehaviour脚本中运行StartCoroutine:Func1将每半秒执行一次。选项通过
Time.deltaTime.