Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PausableObject - Any interest in a tool that can pause coroutines?

Discussion in 'Assets and Asset Store' started by orionburcham, Jun 10, 2015.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    I'm not a big fan of the Time.timescale = 0 method of pausing your game. I'd rather pause Components individually, to control exactly what's going on.

    Tonight I'm wrapping up a tool for my own toolbox: PausableObject. Deriving scripts from PausableObject makes it easy to pause and unpause their activity.

    You get access to a Paused property (for pausing activity in Update loops), as well as OnPaused() and OnUnpaused() callbacks.

    Code (CSharp):
    1. public class TestPausableObject : PausableObject
    2. {
    3.     void Update()
    4.     {
    5.         // just a quick example to show how pausing/unpausing works
    6.         if(Input.GetKeyDown(KeyCode.Space))
    7.         {
    8.             Paused = !Paused;
    9.         }
    10.  
    11.         if(!Paused)
    12.         {
    13.             // do awesome Update stuff
    14.         }
    15.     }
    16.  
    17.     protected override void OnPaused ()
    18.     {
    19.        // pause something external, like an animation
    20.     }
    21.  
    22.     protected override void OnUnpaused ()
    23.     {
    24.         // unpause something external, like an animation
    25.     }
    26. }

    Ok sure, but more useful, you get access to a StartPausableCoroutine() method. This will start normal unity Coroutines, but will automatically pause their execution when Paused == true (and resume them when Paused == false).

    This coroutine will pause just fine:

    Code (CSharp):
    1. public class TestPausableObject : PausableObject
    2. {
    3.     void Awake()
    4.     {
    5.         StartPausableCoroutine(RunPauseTest(10));
    6.  
    7.         // or
    8.  
    9.         StartPausableCoroutine("RunPauseTest", 10);
    10.     }
    11.  
    12.     IEnumerator RunPauseTest(float seconds)
    13.     {
    14.         Debug.Log(Time.timeSinceLevelLoad);
    15.  
    16.         yield return null;
    17.  
    18.         Debug.Log(Time.timeSinceLevelLoad);
    19.  
    20.         yield return new WaitForSeconds(seconds);
    21.  
    22.         Debug.Log(Time.timeSinceLevelLoad);
    23.  
    24.         yield return new WaitForEndOfFrame();
    25.  
    26.         Debug.Log(Time.timeSinceLevelLoad);
    27.  
    28.         yield return new WaitForFixedUpdate();
    29.  
    30.         Debug.Log(Time.timeSinceLevelLoad);
    31.  
    32.         yield return StartPausableCoroutine(WaitForTime(0.1f));
    33.  
    34.         // (also works with yielding WWW objects)
    35.     }
    36.  
    37.     IEnumerator WaitForTime(float time)
    38.     {
    39.         yield return new WaitForSeconds(time);
    40.     }
    41. }

    I'm wondering if people have written scripts like this into their own project, and if there'd be any general interest for having something like this on the AS.

    Cheers!
    -Orion
     
    Last edited: Jun 10, 2015
    theANMATOR2b likes this.
  2. swyrazik

    swyrazik

    Joined:
    Apr 21, 2013
    Posts:
    50
    I haven't searched for anything similar and I didn't have the need yet, but if I do have the need at some point, this seems like a solution I would use for pausing.
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    A question for anyone: How do you currently handle pausing in your projects? Timescale = 0, or have you rolled your own method?

    Thanks!
    Orion
     
    Last edited: Jun 10, 2015