Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FlintCLeaver · Sep 24, 2012 at 03:26 AM · animationmobileoptimizationifisplaying

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sanderman0 · Sep 24, 2012 at 08:34 AM 1
Share

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.

avatar image FlintCLeaver · Sep 24, 2012 at 09:00 AM 0
Share

Thanks sanderman! I'll definitely look into that until I get the full unity profiler!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FlintCLeaver · Sep 24, 2012 at 04:05 AM 0
Share

Thanks! Just the answer I was looking for.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mobile, Scenes with animations make input lagged -sometimes- 0 Answers

Should i bake the skinned mesh? 1 Answer

Too much assets for mobile game 0 Answers

animation.isPlaying is not working 1 Answer

What Resolution to draw sprites for Mobile game?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges