- Home /
Optimization: Central "update" system vs using Updates
I have searched on the internetz and found nothing about a system that would use a coroutine to update each frame, then in this coroutine, I'd run through a List of objects that contains all the Actions.
According to most people, using coroutine is more efficient than using Update, what if I use only one coroutine and iterate through a List each tick of the coroutine?
I'm not used to C#/Unity, let alone optimization, so I don't know if it a good way to go.
Answer by whydoidoit · Sep 24, 2012 at 02:43 PM
A coroutine used like that is not more efficient that an Update because it would always run every frame. Further - having a single "Update" call does not tie together the lifetimes of your objects - so you could still be calling the action on an object that had been destroyed unless you also add that management to your central routine. In the end it would probably be better to just use Update or coroutines on the objects in question.
Hey man! You are everywhere! Handling would be done, automatically too, don't worry ;)(the handling would be done by a component in the GameObject from which everything about updates will be called, but it will just be some kind of bridge between my central iterator and my behaviours, this component will be the core of my "pausing system" anyway, so I don't think it'll be much heavier to add this update control to it.)
I'm taking notes here... So when a coroutine is called every frame and it is in a $$anonymous$$onoBehaviour (or something that already has the working Update method), it is not more efficient.
$$anonymous$$y game will need my characters to have several "Updates" and rather than having a switch to lead to the good method inside my main Update or anything like this, I'd prefer to skip this and have the good update method called directly.
Now, is centralizing all the actions less efficient than starting a coroutine for each object that needs an update? Or maybe I should just stick to the normal Update method as my solution seems too heavy on memory and/or cpu?
Yeah coroutines are really just like Update calls apart from you get the wait instructions - which are surely actually doing some checking in an optimal way (there is no "magic" support for coroutines - they are just enumerators that Unity decides when to execute the next step of). It's more optimal to put yield return new WaitForSeconds(3) than it is to fiddle with the time variables and the ifs necessary to get that to work in an Update function (which would get messy quick and would be sub optimal).
So a coroutine that looks like this:
IEnumerator $$anonymous$$yCoRoutine()
{
while(true)
{
//Do somthing
yield null;
}
}
Is like an Update routine - it's nice because you can have more than one - but it isn't per se more efficient.
I guess every coroutine you start has a little overhead, as would any iteration of a loop and a check to see if it was valid. Not sure on how much - but it seems pretty efficient to me - I have a lot (dozens) of things that I start "secondary" Update functions for.