I have many different png pictures. I use the following method to change the clothes. Each picture acts as a sprite:

  public void ChangeAvatar(Sprite sprAvatar, string strTargetSlot,string strTargetAttach)
    {
        Debug.Log("sprAvatar:" + sprAvatar.name);
        var templateSkin = skeleton.Data.FindSkin("default");
        int slotIndex = skeleton.Data.FindSlot(strTargetSlot).Index;
        Attachment templateAttach = templateSkin.GetAttachment(slotIndex, strTargetAttach); // STEP 1.1
        Attachment newAttach = templateAttach.GetRemappedClone(sprAvatar, sourceMaterial, pivotShiftsMeshUVCoords: false); // STEP 1.2 - 1.3
        if (newAttach != null) customSkin.SetAttachment(slotIndex, strTargetAttach, newAttach); // STEP 1.4
        this.skeleton.SetAttachment(strTargetSlot, strTargetAttach);//设置显示新的服装
    }

Therefore, each time this method is called, a separate material will be generated:

Since too many materials affect performance, I want to merge these materials together, so I used the method in the document:

private void Repack()
{
var skeletonAnimation = GetComponent<SkeletonAnimation>();
var skeleton = skeletonAnimation.Skeleton;
var sourceMaterial = skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
var customSkin = new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine.

        var repackedSkin = new Skin("repacked skin");
            repackedSkin.AddSkin(skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders)
            repackedSkin.AddSkin(customSkin); // Include your new custom skin.

        // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
        if (runtimeMaterial)
            Destroy(runtimeMaterial);
        if (runtimeAtlas)
            Destroy(runtimeAtlas);
        
        repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas); // Pack all the items in the skin.
            skeleton.SetSkin(repackedSkin); // Assign the repacked skin to your Skeleton.
        skeleton.SetSlotsToSetupPose(); // Use the pose from setup pose.
        //skeleton.SetToSetupPose();
        skeletonAnimation.Update(0); // Use the pose in the currently active animation.
                                     //if (bbFollower != null) bbFollower.Initialize(true);
        AtlasUtilities.ClearCache();
        Resources.UnloadUnusedAssets();
    }

However, after using this method, the spine object mapping is wrong:

In this case, how should I use the repack function correctly?

Related Discussions
...

To address the issue of incorrect mapping after using the repack function, you need to ensure that the textures you are using for repacking have the Read/Write Enabled property set. This is a common requirement for the GetRepackedSkin method to work correctly. Here are the steps to ensure proper repacking:

  1. Enable Read/Write on Textures: Make sure that all the textures you are using for repacking have the Read/Write Enabled property set in their import settings.

  2. Check Texture Compression: Ensure that the texture compression is set to None to avoid any issues with texture quality and mapping.

  3. Power of Two Textures: Ensure that your textures are power-of-two dimensions or set the Non-Power of Two setting to None in Unity's texture import settings.

  4. Repack Function: Use the GetRepackedSkin method correctly by following the example provided in the documentation. Here is a refined version of your Repack method:

private void Repack() {
    var skeletonAnimation = GetComponent<SkeletonAnimation>();
    var skeleton = skeletonAnimation.Skeleton;
    var sourceMaterial = skeletonAnimation.SkeletonDataAsset.atlasAssets[0].PrimaryMaterial;
    var customSkin = new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine.

    var repackedSkin = new Skin("repacked skin");
    repackedSkin.AddSkin(skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders)
    repackedSkin.AddSkin(customSkin); // Include your new custom skin.

    // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
    if (runtimeMaterial)
        Destroy(runtimeMaterial);
    if (runtimeAtlas)
        Destroy(runtimeAtlas);

    repackedSkin = repackedSkin.GetRepackedSkin("repacked skin", sourceMaterial, out runtimeMaterial, out runtimeAtlas); // Pack all the items in the skin.
    skeleton.SetSkin(repackedSkin); // Assign the repacked skin to your Skeleton.
    skeleton.SetSlotsToSetupPose(); // Use the pose from setup pose.
    skeletonAnimation.Update(0); // Use the pose in the currently active animation.
    AtlasUtilities.ClearCache();
    Resources.UnloadUnusedAssets();
}
  1. Debugging: If the issue persists, you can debug by checking the following:
    • Ensure that the attachments are correctly mapped to the new texture coordinates.
    • Verify that the customSkin is correctly populated with the new attachments before repacking.

By following these steps, you should be able to use the repack function correctly and avoid issues with incorrect mapping.