- Home /
More animation optimzation - using booleans to check IsPlaying()
Hello- first of all this is not premature optimzaion - My code is functioning as intended and my game is meant for mobile devices so I'm taking all steps I can to increase performance. I don't have unity Pro but I will buying it once I generate some revenue.
Animation can be expensive to call every frame (I use InvokeRepeating so every frame to me is not every frame - just using Update() as example) though I have conditions which need to be checked every frame to see if any changes to animations need to be made.
Now in an attempt to optimize code I have a question about whether using a custom boolean is faster than than using the animation.IsPlaying() function.
Here are two examples below of what the code could look like
var anim : Animation; // cache transform.animation
var walkAnim: String; // cache the sting "walk"
function Update(){
/////////stuff/////
if(canMove){
//movement code goes here
if(!anim.IsPlaying(walkAnim)){//if the attack
//animation isn't playing
anim.Play(walkAnim); // play the attack
//animation
}
}
}
VERSUS
var anim : Animation; // cache transform.animation
var walkAnim: String; // cache the sting "walk"
var walkAnimIsPlaying : boolean; // game changer right here
function Update(){
/////////stuff/////
if(canMove){
//movement code goes here
if(!walkAnimationIsPlaying){//if the attack
//animation isn't playing boolean version
anim.Play(walkAnim); // play the attack
//animation
walkAnimationIsPlaying = true;//set to stop
//calling more once
}
}
}
As a final note this is for looping walk cycles and such. So I figured Unity loops them regardless until you tell it to Stop() or Play() something else on the same animation layer. My main concern is IsPlaying() is calling a function while boolean is defined in the current script.
if(boolean) or if(animation.IsPlaying()) - which is faster?
I agree with Eric that any performance difference will probably be so small as to be insignificant.
$$anonymous$$now that if you feel you really need to profile a lot but don't have pro, writing a simple profiler by yourself is not too difficult. There's quite a few guides on how to do it. $$anonymous$$akes identifying bottlenecks much easier.
Thanks sanderman! I'll definitely look into that until I get the full unity profiler!
Answer by Eric5h5 · Sep 24, 2012 at 03:54 AM
if(boolean) or if(animation.IsPlaying()) - which is faster?
There will not be any measurable difference.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081412im_/https://answers.unity.com/themes/thub/images/avi.jpg)