Search Unity

Coroutine Manager on the Asset Store: Start, Pause, Resume, Reset your coroutines

Discussion in 'iOS and tvOS' started by sidev, Jun 6, 2011.

  1. sidev

    sidev

    Joined:
    Jun 6, 2011
    Posts:
    22
    Hi all,

    Coroutine Manager has been released on the asset store. I hope to use this thread as a support forum for this tool.
    Please check it out at :
    http://u3d.as/content/sidev/coroutine-manager/1Ux


    Basically, it enables centralize your coroutines, and provide the additional abilities to pause, resume and stop coroutines the easy way, and preventing spaghetti code :(. It works for both C# and Javascript scripts.

    Additionnally, you can use this package to manage coroutine as if they were regular objects with members and methods like (isRunning(), isPaused, Destroy(), Pause(), Resume(), Reset()). It is open source, so you can modify it, extend it at will.

    If you wish your modifications to be added to the future versions of Coroutine Manager and be supported, please forward them to me. I ll be glad to look at them and give my opinion.

    Extract from the readme file:
    --------------------------------------
    Coroutine Manager is a single C# file that can be attached to an empty game object. A prefab is indeed included.
    It is used to provide more control over coroutines in Unity3D.
    Coroutines can be started, paused, resumed, reset, looped, and stopped(destroyed).
    Coroutines are managed like any regular objects and provide a set of easy to use methods that can be accessed by any script in Unity.
    Developer can have unity Coroutine co-exist with coroutines managed by Coroutine Manager.
    Additionnally, it is advised to use IEnumerable coroutines instead of the traditional IEnumerator coroutines for a better control(reset and loop on coroutines).


    Code (csharp):
    1.  
    2. 1. Available methods and usage:
    3. ----------------------------
    4.     public static CoroutineBase StartCoroutine(IEnumerable coroutineEnumerable)
    5.    
    6.     Example C#:
    7.         IEnumerable myBounce(){
    8.             print("I bounce");
    9.             yield return 0;
    10.         }
    11.        
    12.         /.... Somewhere in your code ..../
    13.         CoroutineManager.StartCoroutine(myBounce());
    14.  
    15.     Example JavaScript:
    16.         function myBounce() : IEnumerable{
    17.             print("I bounce");
    18.             yield return 0;
    19.         }
    20.        
    21.         /.... Somewhere in your code ..../
    22.         CoroutineManager.StartCoroutine(myBounce());
    23.        
    24.        
    25.  
    26.     public static CoroutineBase StartCoroutine (IEnumerable coroutineEnumerable, string coroutineName)
    27.    
    28.     Example C#:
    29.         IEnumerable myBounce(){
    30.             print("I bounce");
    31.             yield return 0;
    32.         }
    33.        
    34.         /.... Somewhere in your code ..../
    35.         CoroutineManager.StartCoroutine(myBounce());
    36.  
    37.     Example JavaScript:
    38.        
    39.         function myBounce() : IEnumerable{
    40.             print("I bounce");
    41.             yield return 0;
    42.         }
    43.        
    44.         /.... Somewhere in your code ..../
    45.         CoroutineManager.StartCoroutine(myBounce(),"my Bounce Coroutine Name");
    46.    
    47.     //Add a coroutine into the coroutinemanager, method returns true, then the coroutine will run
    48.     public static CoroutineBase StartCoroutine (IEnumerable coroutineEnumerable, CoroutineRunCondition method)
    49.    
    50.    
    51.         Example C#:
    52.            
    53.         bool runIfSomeValueIsTrue(){
    54.             return true;
    55.         }
    56.            
    57.         IEnumerable myBounce(){
    58.             while(true){
    59.                 print("I bounce");
    60.                 yield return 0;
    61.             }
    62.         }
    63.        
    64.         /.... Somewhere in your code ..../
    65.         CoroutineManager.StartCoroutine(myBounce(), runIfSomeValueIsTrue);
    66.  
    67.         Example JavaScript:
    68.         function runIfSomeValueIsTrue() {
    69.             return true;
    70.         }
    71.        
    72.         function myBounce() : IEnumerable{
    73.             while(true){
    74.                 print("I bounce");
    75.                 yield return 0;
    76.             }
    77.         }
    78.        
    79.         /.... Somewhere in your code ..../
    80.         CoroutineManager.StartCoroutine(myBounce(), runIfSomeValueIsTrue);
    81.    
    82.     public static CoroutineBase StartCoroutine (IEnumerable coroutineEnumerable, string coroutineName, CoroutineRunCondition method)
    83.    
    84.     // Same as before but put loop as true if the coroutine should be looping.
    85.     public static CoroutineBase StartCoroutine (IEnumerable coroutineEnumerable, string coroutineName, CoroutineRunCondition method, bool loop)
    86.    
    87.     Example C#:
    88.         IEnumerable myBounce(){
    89.             print("I bounce");
    90.             yield return 0;
    91.         }
    92.         /.... Somewhere in your code ..../
    93.         CoroutineManager.StartCoroutine(myBounce(), "myBouncingCoroutine", null, true);
    94.    
    95.     Example JavaScript:
    96.         function myBounce() : IEnumerable{
    97.             print("I bounce");
    98.             yield return 0;
    99.         }
    100.        
    101.         /.... Somewhere in your code ..../
    102.         CoroutineManager.StartCoroutine(myBounce(), "myBouncingCoroutine", null, false);   
    103.    
    104.    
    105.     public static CoroutineBase StartCoroutine(IEnumerator coroutineYield)
    106.    
    107.  
    108.     public static CoroutineBase StartCoroutine (IEnumerator coroutineYield, string coroutineName)
    109.    
    110.     public static CoroutineBase StartCoroutine (IEnumerator coroutineYield, CoroutineRunCondition method)
    111.    
    112.     public static CoroutineBase StartCoroutine (IEnumerator coroutineYield, string coroutineName, CoroutineRunCondition method)
    113.    
    114.     Example C#:
    115.         bool runIfSomeValueIsTrue(){
    116.             return true;
    117.         }
    118.         IEnumerator myBounce(){
    119.             print("I bounce");
    120.             yield return 0;
    121.         }
    122.         /.... Somewhere in your code ..../
    123.         CoroutineManager.StartCoroutine(myBounce(), "myBouncingCoroutine", runIfSomeValueIsTrue);
    124.    
    125.     Example JavaScript:
    126.         function runIfSomeValueIsTrue() {
    127.             return true;
    128.         }
    129.         function myBounce(){
    130.             print("I bounce");
    131.             yield return 0;
    132.         }
    133.        
    134.         /.... Somewhere in your code ..../
    135.         CoroutineManager.StartCoroutine(myBounce(), "myBouncingCoroutine", runIfSomeValueIsTrue);
    136.    
    137.    
    138.     //Will stop and remove all the coroutines that have this name
    139.     public static void DestroyCoroutine (string name)
    140.  
    141.     //Will stop this coroutine and remove it from the Coroutine Manager
    142.     //An easier way is to call myCoroutine.Destroy()
    143.     public static void DestroyCoroutine (CoroutineBase coroutine)
    144.    
    145.     //Will pause the coroutine
    146.     //An easier way is to call myCoroutine.Pause()
    147.     public static void PauseCoroutine (CoroutineBase coroutine)
    148.    
    149.     //Will pause all coroutines that have this name
    150.     public static void PauseCoroutine (string name)
    151.  
    152.     //Will pause all coroutines
    153.     public static void PauseAllCoroutines ()
    154.    
    155.     //Will resume all coroutines that have this name
    156.     //An easier way is to call myCoroutine.Resume()
    157.     public static void ResumeCoroutine (CoroutineBase coroutine)
    158.    
    159.     //Will resume all coroutines that have this name
    160.     public static void ResumeCoroutine (string name)
    161.  
    162.     //Will resume all coroutines that were paused
    163.     public static void ResumeAllCoroutines ()
    164.    
    165.     //Will reset the coroutine
    166.     //An easier way is to call myCoroutine.Reset()
    167.     //Will only work with IEnumerable coroutine. Not IEnumerator coroutine.
    168.     public static void ResetCoroutine (CoroutineBase coroutine)
    169.  
    170.     //Will reset all coroutines that have this name
    171.     public static void ResetCoroutine (string name)
    172.  
    173.     //Will reset all coroutines
    174.     public static void ResetAllCoroutines ()
    175.    
    176.     //Will return true if the coroutine is paused.
    177.     public static bool isCoroutinePaused(CoroutineBase coroutine){
    178.    
    179.    
    180.     //Will return true if the first found coroutine that has this name is paused.
    181.     public static bool isCoroutinePaused(string name)
    182.    
    183.     //Will return true if the coroutine has finished.
    184.     //You can use the isFinished public member instead
    185.     public static bool isCoroutineFinished(CoroutineBase coroutine)
    186.    
    187.     //Will return true if the first found coroutine that has this name is finished.
    188.     //Be careful with this. It is better to use:
    189.     //if (myCoroutine.isFinished){ /.../}
    190.     public static bool isCoroutineFinished(string name)
    191.    
    192.     //Will return true if the coroutine is running.
    193.     public static bool isCoroutineRunning(CoroutineBase coroutine){
    194.    
    195.    
    196.     //Will return true if the first found coroutine that has this name is running.
    197.     public static bool isCoroutineRunning(string name)
    198.    
    199.     //Will stop all coroutines and remove them from the Coroutine Manager
    200.     public static void DestroyAllCoroutines ()
    201.  
    202.  
    203.    
    204.        
    205.                
    206. 2. WaitForSeconds / WaitForEndOfFrame / WaitForFixedUpdate:
    207. --------------------------------------------------------
    208.  
    209.     In order to make a Coroutine pause for several seconds inside the Coroutine Manager,
    210.     please replace "UnityEngine.WaitForSeconds(float seconds)" by "CoroutineManager.WaitForSeconds(float seconds)".
    211.     In version 1.0, WaitForFixedUpdate and WaitForEndOfFrame are not supported (yet).
    212.    
    213.    
    214. 3. IEnumerable versus IEnumerator:
    215. -------------------------------
    216.  
    217.     By declaring your coroutine code as IEnumerable instead of Ienumerator, you will have access as two more capabilities:
    218.         -Reset an IEnumerable coroutine.
    219.         -Loop a IEnumerable coroutine until you decide to stop(destroy) it.
    220.     Start, stop, pause and resume are valid both for IEnumerable and IEnumerator
    221.    
    222.     In C#, just replace IEnumerator by IEnumerable. See above examples.
    223.         replace:
    224.             IEnumerator myBounce(){ //Traditional way
    225.         by
    226.             IEnumerable myBounce(){  //Recommended way
    227.            
    228.            
    229.     In Javascript, do as the folowwing :
    230.         replace:
    231.             function myBounce(){ //Traditional way
    232.         by
    233.             function myBounce() : IEnumerable{ //Recommended way
    234.    
    235.    
    236. 4. FAQ:
    237. -------
    238.     How can I yield on a coroutine, so that my code waits for the end of the execution of coroutine
    239.     You can find examples in the sample included in the package in C# and in Javascript.
    240.  
    241.     What are the methods available from a CoroutineBase coroutine?
    242.     coroutine.Pause()
    243.     coroutine.Resume()
    244.     coroutine.Reset()
    245.     coroutine.Destroy()
    246.     coroutine.isRunning()
    247.  
    248.  
    249.  
    250.  
    251.  
     
  2. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    does this get around the issue with coroutines NOT stopping unless they're on a deactivated gameobject, even if called explicitly, using quotes"".?
     
    Last edited: Jun 8, 2011
  3. sidev

    sidev

    Joined:
    Jun 6, 2011
    Posts:
    22
    Hi,

    I actually never experienced this behavior when trying to stop a coroutine the "unity way". I should try sometimes.
    In fact, when you "stop" a coroutine using Coroutine Manager, it is destroyed, in the sense that you will not be able to resume it as if you were pausing it. So that should solve your issue. If it does not, please let me know.

    Also, it is always better to not access Unity objects via strings, so with coroutine manager you can do either ways:
    CoroutineBase coroutine = CoroutineManager.StartCoroutine(myCoroutine()); //Could add more arguments there. See doc
    then
    coroutine.Destroy();

    or if you prefer the old way:
    CoroutineManager.StartCoroutine(myCoroutine(), "myCoroutine");
    then
    CoroutineManager.DestroyCoroutine("myCoroutine");
    Note that in the second case, all coroutines that have this name will be destroyed.

    If you only want to pause (or reset) some coroutines, please look at the documentation for coroutine.Pause() or CoroutineManager.PauseCoroutine() and coroutine.Reset() or CoroutineManager.ResetCoroutine()

    I hope this makes sense!

    Sidev
     
  4. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    You've never experienced the inability to stop a coroutine in Unity when it is NOT called explicitly in quotes ""?
     
  5. sidev

    sidev

    Joined:
    Jun 6, 2011
    Posts:
    22
    Ahhhh yes I did indeed ;)
    I tried to answer to you earlier without success. Anyway, when you use CoroutineManager, you get a handle on an object, and you can manipulate it the way you like ;)

    Hope this helps

    Sidev