understating memory usage - which would be better?
Hi, I'm developing for mobile - I have an animator controller and many different scripts that are triggering and setting values within this animator controller (player01's anim controller)
right now i have it set up where each script has its own
private Animator anim;
referencing the player01 anim controller, but I'm wondering if this is a waste of memory - should I just have an AnimatorController script that contains a
public static animator anim;
? I'm trying to optimize my code, and am wondering which is better on performance.
Thanks for any insight
if there's only one animator that your referencing, then go ahead and make it static. makes things easier in some ways
Answer by digzelot · Sep 14, 2017 at 03:24 PM
as far as memory usage is concerned, Static objects are not instanced, therefor they are faster in code.
https://www.codeproject.com/Articles/15269/Static-Keyword-Demystified
"So using the static keyword will make your code a bit faster since no object creation is involved."
... Are bugs more common when using statics? Probably... so be careful not to misuse them
Answer by ooblii · Aug 31, 2017 at 01:43 AM
yes to the first one. no to the second. If you have that many scripts needing a reference to a character's animator controller, you might try making a singleton. Which would be bizarre and most likely a nightmare.
You're pre-optimizing in the wrong place. If you're worried about wasting RAM, look into audio and texture import settings best practice for mobile.
I should clarify; bizarre on a player character, not in principle.
But you certainly could (and should, probably?) have one single class on your player character deter$$anonymous$$ing the character's animation behavior, not "many" as it is now.
"yes to the first one. no to the second." - yes use private anim variables, no don't use a static?
I am optimizing everything; textures, bone counts, meshes, code, etc.
It seems wasteful to have any number (x) of private animator anim variables > 1 referencing the same animator controller
How many scripts are trying to access the animator controller?
The problem I think you'd run into if you made it static is that if you add that script to another gameObject, say player02, they'd both be referencing the same controller, which is bad.
One approach, would be to have an AnimationCenter class which could have a property for the controller:
public AnimatorController Anim {get; private set;}
it's not wildly different then private references to the controller everywhere, but it's cleaner.
A better way might be to have the AnimationCenter handle all of the animating and listen for events to set triggers, receive values, and update the controller.
thanks - there's no worry about a player 2 issue. this isn't a "static health on all enemies" type of question
Your answer
Follow this Question
Related Questions
Performance on 2D only: Animator.Update 1 Answer
Animator Layers performance (No motion) 0 Answers
Is the animator in Unity really that expensive? 0 Answers
Static method performance vs non static 0 Answers
Animator does not show up 0 Answers