Home >Backend Development >C++ >How to Effectively Pause Unity Scripts Using WaitForSeconds, WaitForSecondsRealtime, and While Loops?

How to Effectively Pause Unity Scripts Using WaitForSeconds, WaitForSecondsRealtime, and While Loops?

DDD
DDDOriginal
2025-01-31 13:26:10394browse

How to Effectively Pause Unity Scripts Using WaitForSeconds, WaitForSecondsRealtime, and While Loops?

Efficiently paused Unity script simple method

Unity provides a variety of methods to suspend scripts and simulate dormant. This article will introduce a simple and commonly used method:

<.> 1. Use WaitForseconds:

This method is simple and direct, and uses corporates. The coroutine function allows the codes to execute on multiple frames. In the corporate function, you can use WaitForseconds to specify the duration of the suspension.

<.> 2. Use the waitforsecondsRealtime:

<code class="language-csharp">StartCoroutine(waiter());

IEnumerator waiter()
{
    // 旋转90度
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    // 使用 WaitForSeconds 暂停4秒
    yield return new WaitForSeconds(4);

    // 继续执行其他代码
}</code>

This is the replacement method of WaitForseconds, which is very useful when you need to suspend execution without the time ratio of the game. WaitForseconds is influenced by Time.TimeScale, and Time.TimeScale can slow down or speed up the game. However, WaitForsecondsRealTime ensures that the duration of duration remains unchanged even if the time ratio changes.

<.> 3. Use the WHILE loop and time.deltatime:

<code class="language-csharp">StartCoroutine(waiter());

IEnumerator waiter()
{
    // 旋转90度
    transform.Rotate(new Vector3(90, 0, 0), Space.World);

    // 使用 WaitForSecondsRealtime 暂停4秒
    yield return new WaitForSecondsRealtime(4);

    // 继续执行其他代码
}</code>
You can use the WHILE loop and time.Deltatime to achieve a suspension. Time.Deltatime said the time has passed since the previous frame. This method allows you to monitor the duration of duration and end it if necessary.

Apply the solution to your code:

In order to solve the specific problems that you pause between the text UI display, you can use the following code:
<code class="language-csharp">float pauseDuration = 4f;
float elapsedTime = 0f;

while (elapsedTime < pauseDuration)
{
    elapsedTime += Time.deltaTime;
}</code>

Calling the corporate function by calling the corporate function from start () or update (), you can control the time interval between each text UI message display.

The above is the detailed content of How to Effectively Pause Unity Scripts Using WaitForSeconds, WaitForSecondsRealtime, and While Loops?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn