Home >Backend Development >C++ >How to Implement Game Pauses in Unity Using Different Methods?

How to Implement Game Pauses in Unity Using Different Methods?

DDD
DDDOriginal
2025-01-31 13:22:11476browse

How to Implement Game Pauses in Unity Using Different Methods?

The multiple implementation methods of the unity game of the unity game

Introduction

Inserting or pause in the game can enhance the interaction and rhythm of the game. In Unity, a variety of technologies can be used to achieve this.

<.> 1.

corporate

The most direct way is to use WaitForSeconds coroutine. This will suspend the execution of the coroutine specified duration. For example:

This coroutine distributes the text to you WaitForSeconds, with a delay of 3 seconds between assignment.

<code class="language-C#">IEnumerator Waiter()
{
    TextUI.text = "欢迎来到数字巫师!";
    yield return new WaitForSeconds(3);
    TextUI.text = ("你选择的最高数字是 " + max);
    yield return new WaitForSeconds(3);
    TextUI.text = ("你选择的最低数字是 " + min);
}</code>
<.> 2.

corporate TextUI

Similar to , will also be suspended, but it will ignore time -sharing (for example, for slow action effects). WaitForSecondsRealtime

<.> 3.

WaitForSeconds WaitForSecondsRealtime

You can also create a delay by comparing the value of each frame and compare its value with the target value. This allows dynamic waiting time and can be interrupted.
<code class="language-C#">IEnumerator Waiter()
{
    TextUI.text = "欢迎来到数字巫师!";
    yield return new WaitForSecondsRealtime(3);
    TextUI.text = ("你选择的最高数字是 " + max);
    yield return new WaitForSecondsRealtime(3);
    TextUI.text = ("你选择的最低数字是 " + min);
}</code>

<.> 4. Time.deltaTime Function

This function executes the coroutine until it meets the specified conditions. For example, waiting for the player score to reach the target value:

<code class="language-C#">float counter = 0;
float waitTime = 3;
bool quit = false;

void Update()
{
    if (!quit)
    {
        counter += Time.deltaTime;
    }

    if (!quit && counter >= waitTime)
    {
        // 等待时间已过时执行代码
        counter = 0;
    }
}</code>

<.> 5. Function WaitUntil

Similar to , this function executes coroutine, until the conditions are no longer true. For example, wait at holding the button:

<code class="language-C#">IEnumerator Waiter()
{
    float targetScore = 100;
    yield return new WaitUntil(() => playerScore >= targetScore);
    // 加载下一关或执行所需操作
}</code>
<.> 6.

Function WaitWhile This will arrange a function to be called after delay. For example, feed the dog after 5 seconds:

WaitUntil <.> 7.

Function and
<code class="language-C#">IEnumerator Waiter()
{
    yield return new WaitWhile(() => Input.GetKey(KeyCode.Escape));
    // 退出或执行所需操作
}</code>

Invoke Similar to options 3, but in the function, check the condition:

Solution for your problems
<code class="language-C#">Invoke("feedDog", 5f);
void feedDog() { Debug.Log("正在喂狗"); }</code>

To achieve delay in your specific code: Update() Time.deltaTime Use the

call coroutine in your or

function. Update()

The above is the detailed content of How to Implement Game Pauses in Unity Using Different Methods?. 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