void Apply(SkeletonRenderer skeletonRenderer) { atlas = AtlasAsset.GetAtlas(); if (atlas == null) { return; } float scale = skeletonRenderer.skeletonDataAsset.scale; foreach (var entry in attachments) { Slot slot = skeletonRenderer.Skeleton.FindSlot(entry.slot); Attachment originalAttachment = slot.Attachment; AtlasRegion region = atlas.FindRegion(entry.region); if (region == null) { slot.Attachment = null; } else if (inheritProperties && originalAttachment != null) { slot.Attachment = originalAttachment.GetRemappedClone(region, true, true, scale); } else { var newRegionAttachment = region.ToRegionAttachment(region.name, scale); slot.Attachment = newRegionAttachment; } if (!entry.UseOriginalData) { RegionAttachment ra = slot.Attachment as RegionAttachment; if (ra == null) { return; } ra = (RegionAttachment)ra.Copy(); //HeroAnimImgList.SlotRegionPair PAIR = SDDataManager.Instance // .GetPairBySlotAndRegion(CM.SkeletonIndex, entry.slot, entry.region); //Vector2 _pos = PAIR.PositionOffset; //Vector2 _scale = PAIR.Scale; //ra.Width = ra.RegionWidth * scale * _scale.x; //ra.Height = ra.RegionHeight * scale * _scale.y; //ra.SetPositionOffset( _pos * scale * _scale ); ra.UpdateOffset(); slot.Attachment = ra; } } }
void Apply() { var skeletonAnimation = GetComponent <SkeletonAnimation>(); var skeleton = skeletonAnimation.Skeleton; // STEP 0: PREPARE SKINS // Let's prepare a new skin to be our custom skin with equips/customizations. //We get a clone so our original skins are unaffected. customSkin = customSkin ?? new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine. //customSkin = customSkin ?? skeleton.UnshareSkin(true, false, skeletonAnimation.AnimationState); // use this if you are not customizing on the default skin. var templateSkin = skeleton.Data.FindSkin(templateAttachmentsSkin); // STEP 1: "EQUIP" ITEMS USING SPRITES // STEP 1.1 Find the original/template attachment. // Step 1.2 Get a clone of the original/template attachment. // Step 1.3 Apply the Sprite image to the clone. // Step 1.4 Add the remapped clone to the new custom skin. for (int i = 0; i < AllReplacePairs.Count; i++) { SlotRegionPair Pair = AllReplacePairs[i]; int slotIndex = skeleton.FindSlotIndex(Pair.Slot); // You can access GetAttachment and SetAttachment via string //, but caching the slotIndex is faster. Attachment templateAttachment = templateSkin.GetAttachment(slotIndex, Pair.Key); // STEP 1.1 Attachment newAttachment = templateAttachment.GetRemappedClone (Pair.newSprite, sourceMaterial); // STEP 1.2 - 1.3 if (newAttachment != null) { RegionAttachment ra = newAttachment as RegionAttachment; ra = (RegionAttachment)ra.Copy(); float scale = 1f / Pair.newSprite.pixelsPerUnit; ra.Width = ra.RegionWidth * scale; ra.Height = ra.RegionHeight * scale; customSkin.SetAttachment(slotIndex, Pair.Key, newAttachment);//STEP 1.4 } } if (repack) { var repackedSkin = new Skin("repacked skin"); repackedSkin.AddAttachments(skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders) repackedSkin.AddAttachments(customSkin); // Include your new custom skin. 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. if (bbFollower != null) { bbFollower.Initialize(true); } } else { skeleton.SetSkin(customSkin); // Just use the custom skin directly. } skeleton.SetSlotsToSetupPose(); // Use the pose from setup pose. skeletonAnimation.Update(0); // Use the pose in the currently active animation. Resources.UnloadUnusedAssets(); }