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?