Search Unity

Using Sprite Swap with spritesheets

Discussion in '2D Experimental Preview' started by Zephus, Apr 20, 2021.

  1. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    247
    I'm trying to get a very simple setup running using the Sprite Library und Sprite Resolver Components. I have this spritesheet with two different character skins:


    Now I want to make one single animation consisting of two frames. And then I want to be able to swap the sprites at runtime for the other skin. So I thought I could set up the Sprite Library like this:


    Now I only need to change the category to switch my dark skin for the red one. But when I try to animate this, it doesn't work, as the category can't be changed separately anymore. It's now saved in one single Sprite Key. What happens is that I set up my animation for the dark skin, but then have no way of actually swapping to the red one. The animation will always pick the dark skin as it has a unique key.

    The documentation makes it seem like this is exactly what the Sprite Resolver is supposed to be used for, but it just doesn't work. Why can't I just change the category separately? Or am I doing this wrong?
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    496
    Hello @Zephus,
    It seems you are using Unity 2021.1 with 2D Animation 6.0. In this version, we merged the categories and label hashes into one hash in the Animation clip, Sprite Key Hash (documentation link).

    In the Flipbook Animation swap example, we showcase how we would go about setting up the assets and clips for the type of animation you are trying to create. The idea in that example is to split the different characters into different Sprite Library Assets. So you would have one Sprite Library Asset fo your Grey Hero, and another one for your Red Hero. Having one Sprite Library Asset per character gives you the ability to not have to keep both character textures in memory if not needed. Lets say your level only has the Red Hero, then you only need to reference the Sprite Library Asset with the Red Hero, and can leave out the Grey Hero Sprite Library Asset for some memory saving.

    Let me know if you would like to structure your animation clips in some other way. We are always looking for feedback on how you all use the features, so we can improve them in future releases.
     
    Zephus likes this.
  3. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    247
    @Ted_Wikman
    Thank you, this is exactly what I was looking for. I couldn't get it to work by changing the Sprite in the Sprite Renderer like the example shows, but it works when I change the Sprite Library Asset in the Sprite Library Component.

    The workflow is pretty nice, but one thing I've noticed is that it got tedious pretty quickly. All of my spritesheets are set up in the exact same way, but I have to drag in the corresponding sprites for every single frame. What I would like to see is some kind of "Sprite Library Asset Override" or an inspector field where I can drag in a spritesheet that has the same layout as the original one, and the Sprite Library Asset is automatically filled with the correct sprites.

    This could probably be solved with an Editor script, but something like this would speed up my workflow tremendously. I could just draw over an existing spritesheet in Photoshop and only have to drag in the texture once to set it up.
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    496
    I'm happy this feature solved your issue!

    Thanks for the feedback, we are currently looking into how we can improve working with the Sprite Library Assets/Component, so this feedback is right on point. I'll pass this over to our UX Designer.

    Let us know if you have any other pain points while using the Sprite Swap flow.
     
    Zephus likes this.
  5. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    247
    All right, thank you. While I'm at it here are two more things I've noticed:

    1) I think it's really annoying that you have to set both tangents to constant in the animator. Now I understand why I have to do this, but it feels very clunky. I forgot this multiple times, and when I do I basically have to start over from scratch as the animation curve messes up the sprite keys.

    I have no idea how to improve this, given how the animator interpolates between the values by default, but I can't understate how fiddly and error-prone this is. There's also no indicator anywhere other than somewhere in the documentation. When I first tried Sprite Swap, I thought it would make sense to animate the Sprite Resolver. So I chose the sprites and everything seemed to work, but then it just didn't. Took me a while to find out the animator was interpolating. From a user-standpoint it feels like I have to right-click on this tiny keyframe for some reason, go to "both tangents" for some reason and then choose "constant". And I have to do this after adding one single keyframe and before continuing with the others. It's just... weird.

    If I had to suggest something - how about an "add sprites to animation" button in the Sprite Resolver? This could create keyframes for all sprites in the category (one each frame) and set their values to constant.


    2) I think the Component names are pretty confusing. This again comes down to the workflow not being intuitive, but requiring the documentation to get anything to run. I created a Sprite Library Asset and added a Sprite Library to my character. If I hadn't read the tutorial I would've had no idea that I also need a Sprite Resolver. Inside the Editor there's no indication that my library is pretty much useless without it, and on top of that the Component looks like a generic script.

    But then I began to wonder why we even need two different Components. The Sprite Library only has one single field and nothing else, that is then used by the Sprite Resolver. Why not just put that field in the Sprite Resolver itself? My suggestion would be: merge Sprite Library and Sprite Resolver component into one and call it "Sprite Library". I think that would be really self-explanatory. You add the Component and see it needs a Sprite Library Asset, so you create that and you'll immediately see the functionality in the inspector instead of having to search the documentation for this mysterious Sprite Resolver. There could even be a "create new Sprite Library Asset" on the Component if no asset has been assigned yet.


    I hope this feedback helps.
     
  6. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    496
    Thanks again for your feedback!

    For number 1, I fully agree with you that this is not an ideal way of working with Sprite Swap in animations. We are currently looking into ways on how this can be improved and hope to have a solution to this issue in a future version of 2D Animation.

    For number 2, the reason why we have two components, Sprite Library and Sprite Resolver, is because they have a one to many relationship. If you are animating a character with multiple Sprite Renderers and Sprite Resolvers, you only need one Sprite Library in the root of the hierarchy. All Sprite Resolvers will search for a Sprite Library starting from its own GameObject and moving upwards in the hierarchy.
    This is not to say that it couldn't be improved and be made more intuitive, though.

    Thanks again for your feedback, we will take this insight with us when planning the upcoming releases of 2D Animation.
     
    Zephus likes this.
unityunity