Reusing Skinning Bones Structures for Different Sprites (PSD Import)

Hi Unity Developers,

I’m seeking strategies for reusing skinning bone structures with different sprites, particularly for PSD imports in Unity’s 2D animation system. I’d also like to share the solution I’ve developed.

Background:
I was working with a 2D character represented in Unity by a PSD file that was split into different layers for each body part. After setting up bones and weights through the initial PSD import and Sprite- / skinning editor, I was faced with the challenge of aging my character and using all created animations. The traditional approach of creating a PSD file for each age phase and re-setting the bones and weights for each of those age phase files seemed inefficient to me. Especially if you want to add more characters later.

Unsuccessful Attempt:
Initially, I tried changing the character’s sprites in the scene via a script. Although this worked, I lost the bones and weights. My next step was to script a reset of these elements: saving the bones and weights, switching the sprite, and then reassigning them. This method worked for the bones but not for the weights, resulting in gaps in the animations where body parts didn’t connect correctly.

Solution Approach:
I have discovered the following workaround for myself:

  • Duplicate the original PSD file: I duplicated the original PSD file of the character (already rigged with bones and weights). The duplicated file contains the bones and weights. And I don’t want to reset them.
  • Modifying duplicates for ageing: In an image editing tool, I opened the duplicates and adjusted the layers to represent different ages.
  • The file does not need to be re-imported and can be used as it is.

Implemented Code:

public class AgeSpriteController : MonoBehaviour[/COLOR]
{
    // Prefabs for different age stages of the character
    public GameObject kidPrefab, teenPrefab, adultPrefab, seniorPrefab;

    // Singleton instance for easy access
    public static AgeSpriteController instance;
    private void Awake()
    {
        instance = this; // Initialize the singleton instance
    }
    // Method to transform the character to a specific age stage
    public void transformToAge(GameObject agePrefab)
    {
        // Reference to the normal form of the life form character
        GameObject vLifeFormNormal = VLifeFormBehaviour.instance.vLifeFormNormal;
        // List of body part names to be updated
        string[] partsToUpdate = {"Head", "Tail", "Left arm", "Left feet", "Body", "right Arm", "right feet"};
        // Iterate through all child transforms of the character
        for (int i = 0; i < vLifeFormNormal.transform.childCount; i++)
        {
            Transform bodyPart = vLifeFormNormal.transform.GetChild(i);
            // Update the sprite of the body part if it's in the partsToUpdate list
            if (Array.IndexOf(partsToUpdate, bodyPart.name) >= 0)
                UpdateBodyPartSprite(bodyPart, agePrefab);
        }
    }
    // Method to update the sprite of a given body part
    private void UpdateBodyPartSprite(Transform bodyPart, GameObject agePrefab)
    {
        // Get the SpriteRenderer component of the body part
        SpriteRenderer spriteRenderer = bodyPart.GetComponent<SpriteRenderer>();
        // Proceed if the SpriteRenderer exists
        if (spriteRenderer != null)
        {
            // Find the corresponding new sprite for this body part
            Sprite newBodyPartSprite = FindCorrespondingSprite(bodyPart.name, agePrefab);
            // Set the sprite to the new one if found
            if (newBodyPartSprite != null) {
                spriteRenderer.sprite = newBodyPartSprite;

                //#### OLD CODE: Try to rebind Bones & Weights ####
                //SpriteBone[] bones = spriteRenderer.sprite.GetBones();
                //NativeArray<Matrix4x4> bindPoses = spriteRenderer.sprite.GetBindPoses();
                ////NativeSlice<Vector3> positionSlice = spriteRenderer.sprite.GetVertexAttribute<Vector3 (UnityEngine.Rendering.VertexAttribute.Position);
                //NativeSlice<BoneWeight> weightsSlice = spriteRenderer.sprite.GetVertexAttribute<BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight);
                //NativeSlice<Color32> colorsSlice = spriteRenderer.sprite.GetVertexAttribute<Color32>(UnityEngine.Rendering.VertexAttribute.Color);

                //spriteRenderer.sprite = newBodyPartSprite;

                //NativeArray<Vector3> positionArray = ConvertSliceToArray(positionSlice);
                //NativeArray<BoneWeight> weightsArray = ConvertSliceToArray(weightsSlice);
                //NativeArray<Color32> colorsArray = ConvertSliceToArray(colorsSlice);

                //spriteRenderer.sprite.SetBones(bones);
                //spriteRenderer.sprite.SetBindPoses(bindPoses);
                //spriteRenderer.sprite.SetVertexAttribute<Vector3>(UnityEngine.Rendering.VertexAttribute.Position, positionArray);
                //spriteRenderer.sprite.SetVertexAttribute<BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight, weightsArray);
                //spriteRenderer.sprite.SetVertexAttribute<Color32> (UnityEngine.Rendering.VertexAttribute.Color, colorsArray);
}
        }
    }
    // Method to find the corresponding sprite based on the body part name
    private Sprite FindCorrespondingSprite(string bodyPartName, GameObject agePrefab)
    {
        // Iterate through all child transforms of the age prefab
        for (int i = 0; i < agePrefab.transform.childCount; i++)
        {
            Transform bodyPartNew = agePrefab.transform.GetChild(i);

            // Return the sprite if the name matches the body part name
            if (bodyPartNew.name == bodyPartName)
                return bodyPartNew.GetComponent<SpriteRenderer>().sprite;
        }

        // Return null if no matching sprite is found
        return null;
    }
}

In my case, it works now, but I’m interested in other solutions.[/SIZE]

In Unity, there are already mechanisms that allow for convenient changes to both individual sprites (character parts) and all at once. Please take a look at the helpful guide at Setting up for Sprite Swap | 2D Animation | 10.1.2, which can introduce you to many new concepts. Additionally, there are numerous examples available for download and study.