Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DaJuice · Sep 21, 2012 at 04:17 PM · coroutinepauseframes

C#, some kind of coroutine that can pause?

Hi! I'd like to have some kind of tool to let me determine when an action will be triggered (in frames is the best way for me), there is the StartCoroutine that seems to work, but I can only use it with seconds and I cannot pause and resume this coroutine. Anyone has an idea on how to make something as easy to implement as the coroutine, but that would support frames and stop/resume?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Sep 21, 2012 at 04:30 PM

You can absolutely do that with a coroutine - here is an example using a helper function to make it easier to control.

DelayedExecution.cs

 public static class DelayedExecution
 {
     public static void StartCoroutine(this GameObject gameObject, IEnumerator coroutine)
     {
       var behaviour = gameObject.GetComponent<CoroutineHelper>();
       if(!behaviour)
            behaviour = gameObject.AddComponent<CoroutineHelper>();
       behaviour.StartCoroutine(coroutine);
     }
 
     public class CoroutineHelper : MonoBehaviour {}
 
     public class WaitController
     {
         public bool cancel;
         public bool pause;
     }
 
     static IEnumerator WaitForANumberOfFrames(int numberOfFrames, Action thingToDo, WaitController controller)
     {
         while(numberOfFrames > 0)
         {
             if(!controller.pause)
                 numberOfFrames--;
             if(controller.cancel)
                 yield break;
             yield return null;
         }
         thingToDo();
     }

     static IEnumerator WaitForAPeriodOfTime(float timeToWait, Action thingToDo, WaitController controller)
     {
         while(timeToWait > 0)
         {
             if(!controller.pause)
                 timeToWait -= Time.deltaTime;
             if(controller.cancel)
                 yield break;
             yield return null;
         }
         thingToDo();
     }
     
     public static WaitController DoSomethingLater(this GameObject gameObject, Action thingToDo, int numberOfFrames)
     {
         var controller = new WaitController();
         gameObject.StartCoroutine(WaitForANumberOfFrames(numberOfFrames, thingToDo, controller));
         return controller;
     }
     public static WaitController DoSomethingLater(this GameObject gameObject, Action thingToDo, float timeToWait)
     {
         var controller = new WaitController();
         gameObject.StartCoroutine(WaitForAPeriodOfTime(timeToWait, thingToDo, controller));
         return controller;
     }
     public static WaitController DoSomethingLater(this MonoBehaviour behaviour, Action thingToDo, int numberOfFrames)
     {
         var controller = new WaitController();
         behaviour.StartCoroutine(WaitForANumberOfFrames(numberOfFrames, thingToDo, controller));
         return controller;
     }
     public static WaitController DoSomethingLater(this MonoBehaviour behaviour, Action thingToDo, float timeToWait)
     {
         var controller = new WaitController();
         behaviour.StartCoroutine(WaitForAPeriodOfTime(timeToWait, thingToDo, controller));
         return controller;
     }


 }

That's a static utility class - you would use if from your code like this:

TestIt.cs#

 public class TestIt : MonoBehaviour
 {
     public DelayedExecution.WaitController controller;
     
     void Start()
     {
         //Either
         controller = gameObject.DoSomethingLater(DisplayMessage, 15);
         //Or
         controller = gameObject.DoSomethingLater(()=>{
             //You can execute anything you like here e.g.
             Destroy(gameObject);
         }, 20);
             //Or to wait for a period of time instead
              controller = gameObject.DoSomethingLater(DisplayMessage, 5f); //Use a float to wait for time and an int for frames
     }
     
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.P))
         {
             controller.pause = !controller.pause;
         }
     }
     
     void DisplayMessage()
     {
         //Do something here
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Sep 21, 2012 at 04:49 PM 0
Share

yield break; ! man that's awesome

avatar image DaJuice · Sep 21, 2012 at 05:53 PM 0
Share

This is indeed awesome! You sir are a savior, but maybe there's something I don't understand, but GameObject does not have any method called StartCoroutine according to Unity...

avatar image whydoidoit · Sep 21, 2012 at 06:05 PM 0
Share

@DaJuice - Oops - sorry - I forgot that's another extension I wrote! I've added it to the example (didn't have chance to test it - looks good though)

avatar image DaJuice · Sep 21, 2012 at 06:09 PM 0
Share

Do you think that putting a static $$anonymous$$onoBehaviour in the DelayedTask class, and always use this $$anonymous$$onoBehaviour to call StartCoroutine would be a good idea? Or would there be memory management issues maybe?

avatar image whydoidoit · Sep 21, 2012 at 06:12 PM 0
Share

The reason I do it that way is that a) it doesn't take too much memory b) it gets destroyed if the GameObject is destroyed and c) it gets disabled if the GameObject get's SetActiveRecursively(false) - so I use it like this to not link one thing to a particular script. It would be valid to actually have a version that returned an IEnumerator so you could start it on any behaviour you wanted to make sure it was tied to that particular $$anonymous$$onoBehaviour's state.

I use my GameObject extension when I don't want to have it tied to a particular behaviour.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutines, WaitForSeconds with function Update? 3 Answers

How to pause and unpause my game - Input.GetAxis doesn't work when Time.timeScale is 0 3 Answers

Pause and resume coroutine 0 Answers

How to disable game elements for a fixed duration then re-enable them automatically, even if game is reopened 1 Answer

Pause coroutine with infinite loop 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges