Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Swapping out sprite resources in order to reuse animations

Discussion in '2D' started by stoilcho, Oct 9, 2014.

  1. stoilcho

    stoilcho

    Joined:
    Sep 8, 2014
    Posts:
    30
    Hello guys,
    similar questions have been asked before - I spent some time looking through threads and bottom line is I didn't really find what I was looking for.

    I'm working on a isometric/topdown game where each character's animation consists of a bunch of different animations (walk up, walkdown, walkDownLeft ect) that we change between according to the current direction. Now, the characters need to be holding weapons. And adding a weapon is simple enough: we add the weapon as a sprite child element to the character and then in the animation we add the necessary curves to make it look like the weapon is moving along with the characters (which also includes swapping the sprite altogether from animation curves).

    Now this is all well and good and looks fine but the issue is we need to have a big selection of weapons. Creation a brand new character animation for every weapon is possible but highly impractical. Instead we need to change the base resources being accessed by the animation.

    I'm wondering if anybody can suggest a best practice here. If I manually go into the file system and swap out the sprite sheet that are being access - it works line a charm. But I'm not sure accessing the project structure is really a possibility once the project is built. I basically need to swap out a whole series of sprites in the project structure while keeping the animation curve references completely intact. Any help is appreciated either on getting this to work the way I'm describing it or a better way to achieve the same effect.

    I really appreciate it guys!
     
  2. Moyses

    Moyses

    Joined:
    Feb 10, 2014
    Posts:
    2
    Hi!

    I'm running into a similar problem. I don't know if there is a best practice, I believe this is a limitation when working with sprite animations in Unity.

    This would be my workaround:

    1 - Go to your character Animator view and create an additional Layer. Set the "Blending" to "Override" and "Weight" to "1".
    2 - Use this new Layer to create animations just to change the weapon sprite (from the char child element). Each animation may have only one frame and must not be in loop. If you have 20 different weapons, you have to create 20 animations, one for each weapon.

    To change the character weapon, just play the corresponding "weapon change" animation.

    Sorry for my English, I'm not fluent.
    I hope it helps you.
     
  3. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    59
    Changing Sprites is not possible at the moment. I was stuck with this as well!
    Have a look at this vid from the Unite 2014, wich is really helpfull!

     
  4. mrPalomar

    mrPalomar

    Joined:
    Jan 2, 2014
    Posts:
    1
    I've been looking into this as well. The solution above seems pretty heavy handed and not very efficient if you have lots of sprites you're doing this with. I suspect you could drill down to the underlying texture resource and use SetPixels() to modify the texture data for the sprite, grabbing those pixels from a different sprite using GetPixels(), but I haven't tried it yet. Another thought I had was to modify the keys in the animation directly to point to another sprite, but it doesn't appear possible to access the curves for a loaded animation clip.

    It's somewhat surprising that this isn't possible yet, it seems like a fairly basic function. I have a situation where I have multiple characters that all want to use the same animations and states, and just swap out textures depending on what variant of the character you've selected, and so far I haven't figured out how to do it without a ton of duplicated data.

    Good Luck.
     
  5. EpicPants

    EpicPants

    Joined:
    Apr 16, 2015
    Posts:
    1
    I've had this problem for ages. I have a lot of pixel art characters that use animations to change sprites but I don't want to create new animations for every character. I think I might have the solution though. Instead of animating the sprite specifically you could animate a variable on a small script which selects the appropriate frame of whatever sprites you give it. That way you can reuse animations and controllers that just animate what frame it should be on.

    I will give it a go when I next have chance. Hope this helps.
    ------------------------------------------------------------------------------------------------------
    This seems to work but I ran into a few problems:
    Code (CSharp):
    1.  
    2. [RequireComponent(typeof(SpriteRenderer)), ExecuteInEditMode]
    3. public class SpriteSwaper : MonoBehaviour
    4. {
    5.     public float m_CurrentFrame;
    6.     public Sprite[] m_Frames;
    7.     float m_prevFrame;
    8.     SpriteRenderer m_renderer;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         m_prevFrame = m_CurrentFrame;
    13.         m_renderer = this.GetComponent<SpriteRenderer>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         if (m_CurrentFrame != m_prevFrame)
    20.         {
    21.             m_renderer.sprite = m_Frames[(int)m_CurrentFrame];
    22.             m_prevFrame = m_CurrentFrame;
    23.         }
    24.     }
    25. }
    26.  
    It seems that you can only edit float properties in an animation which isn't ideal as you need to cast it into an int to grab the right frame. I image floats are required because the animation wants to be able to lerp between frames which is were the second problem arises. You need to turn this off by selecting all the frames, right click, both tangents and selecting constant. Finally, if you want to see preview the animation in the editor then the script needs to be running one way or another in the editor. I just used the ExecuteInEditMode attribute.

    It would be nice to know if there is a better solution for this or if there is any way I could do this better.
     
    Last edited: May 5, 2015
  6. SirWeeble

    SirWeeble

    Joined:
    Jul 20, 2013
    Posts:
    29
    Is there still no built-in solution to this issue? I've been occasionally checking forums, blogs, etc for a long time hoping that there would eventually be a solution to this problem. In 2012 a staff member said they were "researching solutions for this issue"(on a similar thread), but 3 years later and we still have no options other than creating 100s of different animations and animators and manually setting every frame.

    I'm assuming that by this point, if there are no built-in options, and no one has been able to make something for the asset store, then there is very little chance of there ever being a fix to this.
     
  7. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    2D Toolkit has had a feature called "sprite attach points" for a very long while now, among many other useful features. Only possible downside to it is that it has its own animation workflow that doesn't use Mecanim.