Search Unity

Can I pause my game using Time.timeScale = 0 and still run animated UI Elements? YES!

Discussion in 'UGUI & TextMesh Pro' started by Adam-Buckner, Sep 11, 2014.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    (This has been added to the stickies thread.)

    Yes! :) Set the "Update Mode" to "Unscaled Time"



    Sample Pause Script [C#]
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. #if UNITY_EDITOR
    5. using UnityEditor;
    6. #endif
    7.  
    8. public class PauseManager : MonoBehaviour {
    9.  
    10.     Canvas canvas;
    11.  
    12.     void Start()
    13.     {
    14.         canvas = GetComponent<Canvas>();
    15.         canvas.enabled = false;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.Escape))
    21.         {
    22.             Pause();
    23.         }
    24.     }
    25.  
    26.     public void Pause()
    27.     {
    28.         canvas.enabled = !canvas.enabled;
    29.         Time.timeScale = Time.timeScale == 0 ? 1 : 0;
    30.     }
    31.  
    32.     public void Quit()
    33.     {
    34.         #if UNITY_EDITOR
    35.         EditorApplication.isPlaying = false;
    36.         #else
    37.         Application.Quit();
    38.         #endif
    39.     }
    40. }
    Sample Pause Script [US/JS]
    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.UI;
    3.  
    4. private var canvas : Canvas;
    5.  
    6. function Start()
    7. {
    8.     canvas = GetComponent.<Canvas>();
    9.     canvas.enabled = false;
    10. }
    11.  
    12. function Update()
    13. {
    14.     if (Input.GetKeyDown(KeyCode.Escape))
    15.     {
    16.         Pause();
    17.     }
    18. }
    19.  
    20. public function Pause()
    21. {
    22.     canvas.enabled = !canvas.enabled;
    23.     Time.timeScale = Time.timeScale == 0 ? 1 : 0;
    24. }
    25.  
    26. public function Quit()
    27. {
    28.     #if UNITY_EDITOR
    29.     EditorApplication.isPlaying = false;
    30.     #else
    31.     Application.Quit();
    32.     #endif
    33. }
     
    artcad, salmannabi, jamtangan and 4 others like this.
  2. rorakin3

    rorakin3

    Joined:
    Jan 2, 2013
    Posts:
    464
    Nice to see this is built in. I remember needing to write HOTween animation code to handle this use case in other GUI systems.
     
  3. xuanyusong

    xuanyusong

    Joined:
    Apr 10, 2013
    Posts:
    49
    DoTween you can use
    tweener.SetUpdate(true);

    this is will Ignore TimeScale
     
    casmap likes this.