- Home /
Is it reasonable to use OnBecameVisible for controlling animations, lights and particle systems?
Hi everybody,
I want to reduce performance overhead in my game. From the docs I've learned about using OnBecameVisible and OnBecameInvisible for enabling/disabling scripted behaviour depending on visibility.
In the examples, these functions are used to enable/disable whole scripts, e.g. like this:
function OnBecameVisible()
{
enabled = true;
}
function OnBecameInvisible()
{
enabled = false;
}
This sounds quite clear and reasonable to me.
However, the most performance consuming parts of my game objects are not the scripts, but rather
animations
lights
particle emitters
So I've scripted my OnBecameVisible/OnBecameInvisible methods in a way that they enable/disable these components (which seems to work fine).
My question however is: Does this make sense? Is it necessary at all? Or does the game engine optimize the rendering of such components by itself in a way that invisible animations etc. are suspended automatically whenever no camera can see them? Am I doing something completely redundant here?
I found some discussion about animations here: http://answers.unity3d.com/questions/48683/Animations--Animate-only-when-visible.html. But this seems to relate to an older Unity version, because in my Animation component, there doesn't seem to be an option called "Animate only if visible" anymore. Instead, we now have the "culling type" option, but according to my understanding of the documentation (Animation Reference) this seems to be meant for something different.
Thanks for any hints!
Answer by jacobschellenberg · Jul 30, 2013 at 05:46 PM
It is a reasonable option, but keep in mind the GameObject will not update during the time it is off camera view. Therefore, if say that GameObject needs to do something during update, it will not happen. Instead, you could give the GameObject an "Off" state which is activated when no longer in camera view. Allowing it to continue to run Updates if needed, but is not actually disabled.
Thanks for your answer. Especially the last thing is a good point! I am aware of the Update pausing, and in my case it doesn't matter (because my objects are not moving, they simply need to run animations and receive RPC calls). But it's a good idea to use some boolean on/off flag in case one should have to still run Update() in invisible state!
Your answer
Follow this Question
Related Questions
Is 2d animation better for performance over particle system? 1 Answer
Adding animation clips via script 2 Answers
What's best (performant) way to animate light bulbs? 1 Answer
Eyes animation texture problem 0 Answers
How to animate a color value? 1 Answer