Home >Backend Development >C++ >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
WaitForSeconds
WaitForSecondsRealtime
<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
<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.
<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
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!