在Unity中如何在while循环中添加延迟?

huangapple go评论115阅读模式
英文:

How do i put a delay in a while loop in Unity?

问题

I just want a simple way to delay a while loop. I'm using C#.
Here's my code:

  1. while (value < 101)
  2. {
  3. Debug.Log(value);
  4. value = value + 10;
  5. // 在这里延迟(我不知道如何做)
  6. }

我只想要一个简单的方法来延迟一个 while 循环。我正在使用 C#。
这是我的代码:

  1. while (value < 101)
  2. {
  3. Debug.Log(value);
  4. value = value + 10;
  5. // 在这里延迟(我不知道如何做)
  6. }
英文:

I just want a simple way to delay a while loop. I'm using C#.
Here's my code:

  1. while (value&lt;101)
  2. {
  3. Debug.Log(value);
  4. value = value + 10;
  5. //delay here (that i don&#39;t know how to do)
  6. }

I used a bit of google translate so the text might be bad.

答案1

得分: 4

Unity允许您使用协程延迟代码执行。这避免了暂停整个线程,不像Thread.Sleep()。要在while循环中添加延迟,您可以这样做:

  1. private IEnumerator DelayedWhileLoop()
  2. {
  3. int value = 0;
  4. while (value < 101)
  5. {
  6. Debug.Log(value);
  7. value += 10;
  8. yield return new WaitForSeconds(1); // 延迟1秒
  9. }
  10. }
  11. void Start()
  12. {
  13. StartCoroutine(DelayedWhileLoop());
  14. }
英文:

Unity allows you to delay code execution using coroutines. This avoids pausing the entire thread, unlike Thread.Sleep(). To add a delay to a while loop you could do something like this:

  1. private IEnumerator DelayedWhileLoop()
  2. {
  3. int value = 0;
  4. while (value &lt; 101)
  5. {
  6. Debug.Log(value);
  7. value += 10;
  8. yield return new WaitForSeconds(1); // Delay for 1 second
  9. }
  10. }
  11. void Start()
  12. {
  13. StartCoroutine(DelayedWhileLoop());
  14. }

答案2

得分: 0

你可以使用Unity的Coroutine函数来实现这个,带有延迟。请运行以下代码:

  1. public class LoopWithDelayExample : MonoBehaviour
  2. {
  3. private bool isRunning = false;
  4. // 启动Coroutine
  5. public void StartCoroutineTest()
  6. {
  7. if (!isRunning)
  8. {
  9. StartCoroutine(DelayCoroutineWithLoop());
  10. }
  11. }
  12. // 带延迟的Coroutine
  13. private IEnumerator DelayCoroutineWithLoop()
  14. {
  15. isRunning = true;
  16. float loopingTime = 5f; // 循环的持续时间(秒)。
  17. float delay = 2f; // 延迟的持续时间(秒)。
  18. float startTime = Time.time;
  19. float endTime = startTime + loopingTime;
  20. // 在给定的持续时间内循环
  21. while (Time.time < endTime)
  22. {
  23. Debug.Log("Loop is running...");
  24. // 等待下一帧。
  25. yield return null;
  26. }
  27. // 延迟指定的持续时间。
  28. yield return new WaitForSeconds(delay);
  29. // 重置变量
  30. isRunning = false;
  31. }
  32. }

请注意,这是Unity中用于在一段时间内执行循环并添加延迟的示例代码。

英文:

you can do this using Unity's Coroutine function with delay.
Please run the beloved code.
public class LoopWithDelayExample : MonoBehaviour
{
private bool isRunning = false;

  1. // Start Coroutine
  2. public void StartCoroutineTest()
  3. {
  4. if (!isRunning)
  5. {
  6. StartCoroutine(DelayCoroutineWithLoop());
  7. }
  8. }
  9. // Delayed Coroutine
  10. private IEnumerator DelayCoroutineWithLoop()
  11. {
  12. isRunning = true;
  13. float loopingTime = 5f; // The duration of the loop in seconds.
  14. float delay = 2f; // The delay duration in seconds.
  15. float startTime = Time.time;
  16. float endTime = startTime + loopingTime;
  17. // Loop for given duration
  18. while (Time.time &lt; endTime)
  19. {
  20. Debug.Log(&quot;Loop is running...&quot;);
  21. // Wait for the next frame.
  22. yield return null;
  23. }
  24. // Delay for the specified duration.
  25. yield return new WaitForSeconds(delay);
  26. //Reset Vars
  27. isRunning = false;
  28. }
  29. }

huangapple
  • 本文由 发表于 2023年7月20日 08:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定