• 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
9
Question by FeetSlashBirds · Mar 30, 2014 at 05:06 PM · 2danimatoranimator controlleranimationclip

Unity 2D : Can you change a 2D animation clip's sprites at runtime?

Hi there,

I'm working in Unity's 2D mode and using the Unity UI I've created a GameObject with an Animation Controller (Animator) linked up with six different Animation Clips. While running the game this GameObject properly transitions between all of the animation clips.

I'd like to use this object as a prefab. At runtime I want to clone it and swap out the sprites being used for the animation clip key frames.

In other words I have object A that uses a Sprite_Sheet_A. Now I want to clone Object A and start using Sprite_Sheet_B.

Once I clone object A how can I access its Animation Controller, view the Animation Clips being controlled and swap out their sprites?

EDIT: When I say Animation Clip... I might mean Animation. The difference between these classes in 2D is not super clear for me.

Comment
Add comment · Show 3
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 joshpsawyer · Mar 30, 2014 at 05:47 PM 0
Share

If you want to clone an object and change it's sprites at runtime, you'll need to do it with scripting. The relevant references can be found here, but it's likely that it won't be easy:

http://docs.unity3d.com/Documentation/ScriptReference/index.html

It's a better idea to make another prefab for the object with different sprites.

avatar image FeetSlashBirds · Mar 31, 2014 at 05:00 AM 0
Share

Thanks for the reply but I think you misunderstand my question. I am trying to clone a GameObject that has an Animator, Animator Controller and several Animation Clips attached to it.

Each Animation Clip is a series of sprites that add up to a frame-by-frame animation. I want to clone that and then replace each frame with sprites from a new sheet.

For example, lets say I have 2D game with a character that walks from left to right. I have animated the walking motion. Now I want to add a new character that reuses all of the animation logic. I don't want to remake ALL of the animations using a new set of sprites. I just want to copy the animation I made for the first character and apply the new sprites.

avatar image joshpsawyer · Mar 31, 2014 at 02:06 PM 0
Share

http://forum.unity3d.com/threads/213431-Changing-Animation-Sprites Unity says there's no easy way to do what you want in this thread, but supposedly Sprite$$anonymous$$anager can help:

http://wiki.unity3d.com/index.php?title=Sprite$$anonymous$$anager

8 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lachee1 · Jun 12, 2016 at 05:12 AM

I use a script like this to swap sprites out

 using UnityEngine;
 using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SkinController : MonoBehaviour {
 
     private new SpriteRenderer renderer;
     public List<SpriteCollection> skins = new List<SpriteCollection>();
     public int skin = 0;
 
     void Start()
     {
         renderer = GetComponent<SpriteRenderer>();
 
         //First thing we must do is load all the sprites for the character
         foreach (SpriteCollection coll in skins)
             coll.sprites = Resources.LoadAll<Sprite>(coll.sheet.name);
     }
 
     void LateUpdate()
     {
         //Select the correct sprite
         SpriteCollection coll = skins[this.skin];
 
         //Get the name
         string spriteName = renderer.sprite.name;
 
         //Search for the correct name
         if (coll == null || coll.sprites == null) return;
 
         Sprite newSprite = coll.sprites.Where(item => item.name == spriteName).ToArray()[0];
 
         //Set the sprite
         if (newSprite)
             renderer.sprite = newSprite;
     }
 }
 
 [System.Serializable]
 public class SpriteCollection
 {
     public string name;
     public Texture sheet;
 
     [System.NonSerialized]
     public Sprite[] sprites;
 }
 
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 KyleJensen72 · Jun 15, 2016 at 09:27 PM 1
Share

Could you possibly explain this a bit? This may be useful to me and I am having a little trouble understanding exactly what its supposed to do.

avatar image
-1

Answer by Loius · Mar 31, 2014 at 04:58 PM

No, you can't. I have an asset on the asset store specifically for this problem:

https://www.assetstore.unity3d.com/#/content/13602

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 soulburner · Apr 04, 2016 at 10:11 PM 2
Share

You're answering wrong just to advertise your own asset?

Reported.

avatar image
2

Answer by Mike-Geig · Mar 31, 2014 at 04:54 PM

Edit: See the accepted answer for a better solution

Animations are stored in .anim asset files. As such, there is no easy way to modify them at run time. My recommendation would be to create multiple .anim files with the variations you desire and then swap them out on the fly at runtime.

Comment
Add comment · Show 10 · 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 FeetSlashBirds · Apr 02, 2014 at 09:07 AM 0
Share

Thanks... I get that this is the intended use but if you're doing random/procedural content or you just have too many permutations to animate by hand then this solution is just too tedious.

avatar image noio · Jun 14, 2015 at 09:49 AM 0
Share

The actual answer to OP's question.

avatar image antk · Apr 04, 2016 at 07:59 PM 0
Share

How would I actually do this? I have multiple .anim files, but how would I swap them out on the animator controller?

avatar image mos3465 · Apr 05, 2016 at 07:24 AM 0
Share

See soulburners answer and the video posted in the comment to that.

There is absolutely no need to create multiple anim files if you have the same animations for different sprites.

avatar image antk mos3465 · Apr 05, 2016 at 07:15 PM 0
Share

Thanks for the clarification.

avatar image KyleJensen72 · Jun 12, 2016 at 04:26 AM 0
Share

So would you swap out the animation controllers at runtime then? And have a seperate controller for each piece of equipment? Or is there a way to switch out the animation files in the controller? Im also confused by this

avatar image mos3465 KyleJensen72 · Jun 14, 2016 at 07:54 AM 0
Share

Depends on what you want to do.

OPs question was about switching sprites at runtime, not animations/animation controllers. $$anonymous$$g.: Regardless of whether your character wears a chainmail or a smoking - everything moves the same way. If that´s what you have in $$anonymous$$d: no need to swap animation controllers, see here (about $$anonymous$$ute 20): https://www.youtube.com/watch?v=r$$anonymous$$CLWt1DuqI

avatar image KyleJensen72 mos3465 · Jun 14, 2016 at 02:52 PM 0
Share

The way I am animating the separate parts of the body uses an animator and an animator controller for each piece of armour. This seems a little bit unrealistic, but it makes sense to me because the only way to display the armor moving on the character is to have it as an animation. If there was a way to switch out which sprites the animation was looking at, that would be amazing and I could just use one animation controller but I have not been successful.. Any ideas? @mos3456

Show more comments
  • ‹
  • 1
  • 2

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

36 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 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

2D Animation does not start 1 Answer

Why does my transition freeze the second animation on it's first frame? 1 Answer

How to move Animation Clips into Animator Controller? 1 Answer

Stop all animators 0 Answers

Animator Controller 2D RPG Best Practices 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