Best way to queue animation coroutines in unity

by Youssef   Last Updated April 15, 2017 00:13 AM

In my project, I am retrieving data called states from an outside source. I can get up to 3 states per second. My goal is to the following:

  1. Each state will run an animation and a logic
  2. An animation should wait for the previous one to finish before running.

For that goal I am planning to do the following, and I'm looking for advices or improvements on this logic in case it is needed.

  1. When a state is retrieved, put it in a queue
  2. Have a main coroutine running all the time (while true). This main coroutine has a boolean called isAnimationRunning
  3. The main coroutine will check the queue, if it has a state and isAnimationRunning=false then it dequeue a state, create the animation coroutine for it, and start it.
  4. The animation coroutine will perform the animation then set isAnimationRunning to false, in order for the main queue to dequeue the next state

Is this a good approach or there is a better way?

Some alternatives:

  • Use the Update function instead of the main coroutine. Will that cause a problem/improvement?
  • Instead of queuing the **states*, I queue the animation coroutines, and run one after the other.
  • Instead of having the main coroutine running all the time, I only run it when there is states in the queue. Once the queue is empty, I stop it, and start it again as soon as I get a new state

Thanks a lot for any input, validation, or improvement on this.

Tags : unity coroutines

Related Questions

NullReferenceException in StartCoroutine method

Updated November 25, 2016 08:05 AM