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
1
Question by FlintCLeaver · Sep 22, 2012 at 09:31 AM · animationoptimizationstringinvokerepeatingcaching

Will caching strings improve performance for animation? (and other string calls)

Hello, I was wondering if caching strings in the Awake function will increase performance or would it take the same amount of time to call - specifically for calling animations.

Ex:

 var walk : AnimationState;
 var walkAnim : String;
 var _animation : Animation;
 
 var thisTransform : Transform;
 
 function Awake(){
 
 thisTransform = transform;
 
 _animation = thisTransform.animation;
 walkAnim = "walk";
 walk = _animation[walkAnim];
 
 }
 
 function Update(){
 
 _animation.Play(walkAnim);
 
 }

Basically I'm trying to make my code as optimized as possible - this is just an example. I know strings are expensive to process so I was wondering if caching it this way was any help or if walkAnim will just refer back to "walk" as if the same as using "walk" in the first place.

Also, I use InvokeRepeating(); a lot which is formatted InvokeRepeating("someFunction", float, float) so would caching the name of the function help? I'm not sure how to go about testing these very well because I'm still saving up for Pro so no profiler.

Thanks!

Comment
Add comment · Show 1
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 Fattie · Sep 22, 2012 at 09:36 AM 1
Share

great Q !!

1 Reply

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

Answer by sanderman0 · Sep 22, 2012 at 11:25 AM

Strings have to be compared and stuff to find the right AnimationState in the dictionary, so yes, caching that reference to the AnimationState will likely be more efficient.

In practice the difference should matter very little. Did you profile to see what sections of code need optimizing? It seems to me that this caching isn't really needed here. Wouldn't it be better to not call `Animation.Play()` every frame but instead to only call it when the animation should actually change? That way you only call it when the character stops moving or otherwise changes direction, which should be once per second or even less. I believe you would win much more performance with that than this caching I see happening everywhere.

Always remember: Premature optimization is the root of all evil. Also think before optimizing, often it's better to call a heavy operation less often than simply trying to make the thing more efficient.

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 02:52 AM 0
Share

Thank you for your answer. The example above is merely an example - I don't call animation.Play every frame but merely used it as an example of looking up a string very often - however you did re$$anonymous$$d me to check certain crossfades that are unnecessarily called more than once (they are looping already). Unfortunately I don't have the profiler (basic only). I have everything already functioning and after optimizing graphics I wanted to optimize the code ( i have a lot of different animation calls) but there is still some slow down - my guess is the enemies' textures are to blame but i already made every object use one texture atlas/material (with alpha) so I'm scraping around for performance.

///EDIT: Hey Sanderman0 it would be quite nifty if you could take a look at another question I posted here. It works off this topic but is different enough I thought it would be more helpful to others to create a new AS$$anonymous$$

http://answers.unity3d.com/questions/322134/more-animation-optimzation-using-booleans-to-check.html

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

10 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

Related Questions

Problem with makeing text appear on screen 0 Answers

Playing animation by String 3 Answers

Adding animation clips via script 2 Answers

Flash raw animation data to Unity 0 Answers

Optimizing OnGUI 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