【Unity杂记】Spine 替换精灵

using System.Collections.Generic;
using Spine.Unity;
using Spine.Unity.AttachmentTools;
using UnityEngine;

namespace Spine
{
    public class SpriteMix : MonoBehaviour
    {
        [Tooltip("目标源文件")]
        public SkeletonDataAsset skeletonDataAsset;
        [Tooltip("源文件的材质球")]
        public Material sourceMaterial;
        [Tooltip("请参考官方文档")]
        public bool applyPMA;
        [SpineSlot(dataField: "skeletonDataAsset")]
        public string slot;
        [SpineSkin(dataField: "skeletonDataAsset")]
        public string skin;

        private readonly Dictionary<Sprite, Attachment> cachedAttachments = new Dictionary<Sprite, Attachment>();
        public SkeletonAnimation skeletonAnimation;
        Skin equipsSkin;

        internal void Start()
        {
            equipsSkin = new Skin("Equips");

            // OPTIONAL: Add all the attachments from the template skin.
            var templateSkin = skeletonAnimation.Skeleton.Data.FindSkin(skin);
            if (templateSkin != null)
                equipsSkin.AddSkin(templateSkin);

            skeletonAnimation.Skeleton.Skin = equipsSkin;
            RefreshSkeletonAttachments();
        }

        public void Mix(Sprite mixSprite)
        {
            var skeletonData = skeletonDataAsset.GetSkeletonData(true);
            int slotIndex = skeletonData.FindSlot(slot).Index;
            var attachment = GenerateAttachmentFromEquipAsset(mixSprite, slotIndex, skin, slot);
            equipsSkin.SetAttachment(slotIndex, slot, attachment);
            skeletonAnimation.Skeleton.SetSkin(equipsSkin);
            RefreshSkeletonAttachments();
        }

        internal Attachment GenerateAttachmentFromEquipAsset(Sprite sprite, int slotIndex, string templateSkinName, string templateAttachmentName)
        {
            Attachment attachment;
            cachedAttachments.TryGetValue(sprite, out attachment);

            if (attachment == null)
            {
                var skeletonData = skeletonDataAsset.GetSkeletonData(true);
                var templateSkin = skeletonData.FindSkin(templateSkinName);
                Attachment templateAttachment = templateSkin.GetAttachment(slotIndex, templateAttachmentName);
                attachment = templateAttachment.GetRemappedClone(sprite, sourceMaterial, premultiplyAlpha: applyPMA);
                // Note: Each call to `GetRemappedClone()` with parameter `premultiplyAlpha` set to `true` creates
                // a cached Texture copy which can be cleared by calling AtlasUtilities.ClearCache() as shown in the method below.

                cachedAttachments.Add(sprite, attachment); // Cache this value for next time this asset is used.
            }

            return attachment;
        }

        internal void RefreshSkeletonAttachments()
        {
            skeletonAnimation.Skeleton.SetSlotsToSetupPose();
            skeletonAnimation.AnimationState.Apply(skeletonAnimation.Skeleton); //skeletonAnimation.Update(0);
        }
    }
}

将该组件挂载于目标游戏物体,并设置相应的插槽,执行 Mix 就可以将贴图替换上去

发表回复