Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Question] 2D Character Customization - Mesh Swapping

Discussion in '2D' started by Caidran, Aug 20, 2017.

  1. Caidran

    Caidran

    Joined:
    May 20, 2015
    Posts:
    2
    Hello,

    I've been reading all different kinds of threads all over the internet for the majority of today and although I felt like I was getting somewhere sometimes, I am no further forward I don't think, so before I go do something else to refresh my mind I thought I'd post here, just in case someone has a brilliant idea/solution :)

    Setup and Goal;
    2D Characters, individual PNG files for arms, legs, body etc
    Bone animations (currently done via Anima2d SpriteMesh)
    What I am trying to achieve is a character customization menu that would allow players to change aspects of their character such as their clothing, hair style & colour. I am trying to achieve this in a way that would allow all of the animations to be reusable across all characters (of the same body type etc)
    I had hoped that Anima2d would allow me to simply change the sprite of the sprite mesh or something similar to this (reskinning?) but this doesn't appear to be possible with Anima2d at the moment.

    I am certainly open to using a different bone animation tool if it was capable of achieving what I am trying to do.

    Related to this, I do find it surprising that there isn't something that offers this functionality in the asset store what with how many assets are now available through there. I did try one but it didn't appear to work with bone animations sadly.

    Cheers

    Edit:
    Following this up, I'm still at a complete loss for how to cycle through body parts etc, I suspect this may require some coding that is beyond my capabilities for now but I have managed to re-use the bone mesh animations at a simple level by saving a character with the animations required as a prefab, copying the character and using the same animation controller. With this, I am then able to change out the sprite meshes in use for other meshes, bind them to the bones and away they go.
     
    Last edited: Aug 20, 2017
  2. LastAi

    LastAi

    Joined:
    Aug 15, 2013
    Posts:
    8
    Ive been waiting for a fix to this since 4.5 back when Sprite Atlas was added to the future experimental features on the roadmap.

    Sprite Atlas just came out and its not what most of us had hoped for. (Atleast based on the API description of how variants work)
    there is a way to hot swap the with LateUpdate() but you take a performance hit most devs would not want, if you plan to have more that 1 dynamic character on screen.

    there may be a way to generate animations and have them swap the sprites based on matching names and an atlas for refrance but it seems like a nightmare, and you will be left with millions of animations, that will bloat you project but sacrifice much less in run-time performance than LateUpdate().

    from my prospective i don't see a GOOD solution, but i may be wrong. (most likely)
    there are probly other with more experience that may have a solution, Good luck.
     
  3. Deleted User

    Deleted User

    Guest

    Yes the method would be something like this for a single sprite sheet swap:
    Code (csharp):
    1.  
    2. public class ReskinAnimator : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     private string spriteSheetName; //this will be the name of your spritesheet, no file extension
    6.  
    7.     void LateUpdate()
    8.     {
    9.  
    10.         foreach (var renderer in GetComponents<SpriteRenderer>())
    11.         {
    12.             string spriteName = renderer.sprite.name; //finds the name of the sprite to be rendered
    13.             var subSprites = Resources.LoadAll<Sprite>(spriteSheetName); //loads all the sprites in your new sprite sheet (in Resources folder)
    14.             foreach (var sprite in subSprites)
    15.             {
    16.  
    17.                 if (sprite.name == spriteName) //if the sprite has the same name as one you're trying to replace than replace it
    18.                 {
    19.                     renderer.sprite = sprite;
    20.                 }
    21.             }
    22.         }
    23.     }
    24. }