• 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 Romano · Jun 20, 2014 at 07:09 AM · animationspritespriterendereralternative

Any drawbacks to swapping the SpriteRenderer.sprite each frame instead of using Unity's animator or legacy animation?

Hiya. I've written my own code for animating background elements in my game. Not actually sure why I bothered now, it seemed easier at the time to do it t$$anonymous$$s way, so that I could control the animation using coroutines. My script is probably going to have less of a performance $$anonymous$$t than Unity's own animator, but what about compared to the legacy animation (if that's what making animations using the Animation window is?). Or another method? I searched around and found SpriteManager, but it's just a little bit too complex for me too work with, not to mention its in C# and I'm using Javascript.

Here's my coroutine:

 public function PlayOnce() : IEnumerator
     {
             // PlayState is an enum.
         playState = PlayState.PLAY;
 
         w$$anonymous$$le (playState == PlayState.PLAY)
         {
             // If the currentFrameIndex (int) does not exist in the current animation array (int[])...
             if (currentFrameIndex >= currentAnimation.length)
             {
                 playState = PlayState.STOP;
             }
             else
             {
                 // Show the sprite of the current animation at currentFrameIndex
                 spriteRenderer.sprite = allSprites[currentAnimation[currentFrameIndex]];    
             }
 
             // Get the next frame to play
             GetNextFrame();
 
             yield WaitForSeconds(1f / animFrameRate);
         }
     }


And here's that GetNextFrame function:

     // Get Next Frame
     private function GetNextFrame()
     { 
             // Linear
             // AnimStyle is an enum.
         if (animStyle == AnimStyle.LINEAR)
         {
             currentFrameIndex += 1;
         }
      }

  • might add more AnimStyles at some point.*

The variables are all public and set in the inspector.

I've love to know if I'm wasting my time with t$$anonymous$$s method or if there is actually some kind of performance gain? Or any other kind of benefit I haven't thought of from going about it t$$anonymous$$s way?

Thanks very much!

Romano

Comment
Add comment · Show 4
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 meat5000 ♦ · Jun 20, 2014 at 10:23 AM 1
Share

It's a trade off like anything else :)

http://wiki.unity3d.com/index.php?title=Texture_swap_animator

I've used this great snippet to death; modified it for many different purposes. It never seems to impact my performance.

avatar image Romano · Jun 20, 2014 at 11:56 AM 0
Share

Thanks for that link :) What kind of a trade off?

avatar image meat5000 ♦ · Jun 20, 2014 at 12:31 PM 0
Share

Well, there is the RAM cost of storing the multiple textures you will need (if you go that route) and the cost of swapping them in and out (which isn't really that high unless the textures are large).

Basically the approach is more effective with smaller* textures, but that applies to anything texture related :)

*I've tried this with 2k textures and it seems to be just fine.

avatar image Romano · Jun 23, 2014 at 03:33 AM 0
Share

Thanks for that, that's good news for me :) I don't plan on using multiple textures, just multiple sprites from the same texture.

I think that kinda answers my question? I suppose it's a little bit of an open one. But if you want to convert it I'll mark it correct. Thankyouverymuch!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Platform Grills Scroll Animation - 2D Game 0 Answers

Creating a fill animation for sprite renderer 0 Answers

How do I get my animation with two sprites to play? 0 Answers

How to animate a sprite mask? 0 Answers

How can I change/set keys in an animation clip through code? 0 Answers


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