본문 바로가기
프로그래밍/Unity

Spine Skin Attachment

by neive 2021. 4. 28.
728x90
public class Test_Attachment : MonoBehaviour
{
    internal SkeletonAnimation m_Spine;

    [SpineSlot(dataField: "m_Spine")]
    public string m_SlotName;
    public float m_Rotate = 0.0f;

    private void Awake()
    {
        m_Spine = GetComponent<SkeletonAnimation>();
    }
    

    // http://suyeongpark.me/archives/1906
    // http://ko.esotericsoftware.com/forum/Combining-Skins-Additive-6507
    void Load2()
    {
        string PartsName = "Leg_F2";

        var Origin = m_Spine.skeleton.FindSlot(m_SlotName).Attachment as Spine.RegionAttachment;

        Texture2D tx = Resources.Load<Texture2D>(string.Format("SpineAtlas/1/{0}", PartsName));
        Sprite spr = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), new Vector2(0.5f, 0.5f));
        spr.name = PartsName;
        var attach = spr.ToRegionAttachmentPMAClone(Shader.Find("Spine/Skeleton Tint"));
        
        
        attach.X = Origin.X;
        attach.Y = Origin.Y;
        attach.A = Origin.A;
        attach.R = Origin.R;
        attach.G = Origin.G;
        attach.B = Origin.B;

        //attach.SetRegion(Origin.GetRegion());
        //attach.SetUVs(Origin.UVs[4], Origin.UVs[5], Origin.UVs[0], Origin.UVs[1], true);
        attach.Rotation = Origin.Rotation;
        attach.UpdateOffset();

        /*
        attach.Path = Origin.Name;
        attach.SetRegion(Origin.GetRegion());
        attach.X = Origin.X;
        attach.Y = Origin.Y;
        attach.Rotation = Origin.Rotation;
        attach.Width = Origin.Width;
        attach.Height = Origin.Height;
        attach.SetUVs(Origin.UVs[4], Origin.UVs[5], Origin.UVs[0], Origin.UVs[1], true);
        attach.UpdateOffset();
        */
        Spine.Skin skin = new Spine.Skin("Cha_1");
        skin.SetAttachment(m_Spine.skeleton.FindSlotIndex(m_SlotName), PartsName, attach);

        Spine.Skin combined = m_Spine.skeleton.Data.FindSkin("Cha_1");
        combined.AddFromSkin(skin);

        m_Spine.skeleton.Skin = null;
        m_Spine.skeleton.SetSkin(combined);
        m_Spine.skeleton.SetSlotsToSetupPose();
        m_Spine.AnimationState.Apply(m_Spine.skeleton);
        m_Spine.skeleton.Update(0);
    }
        
    void ChangeSkin_(string SlotName, string TextureName)
    {
        var Origin = m_Spine.skeleton.FindSlot(SlotName).Attachment as Spine.RegionAttachment;

        SpineAtlasAsset atlas = Resources.Load<SpineAtlasAsset>("SpineAtlas/cha_1p_Atlas");
        Spine.AtlasRegion region = atlas.GetAtlas().FindRegion(TextureName);
        var attach = region.ToRegionAttachment(region.name);

        attach.X = Origin.X;
        attach.Y = Origin.Y;
        attach.A = Origin.A;
        attach.R = Origin.R;
        attach.G = Origin.G;
        attach.B = Origin.B;
        attach.Rotation = Origin.Rotation;
        attach.UpdateOffset();

        Spine.Skin skin = new Spine.Skin("Cha_1");
        skin.SetAttachment(m_Spine.skeleton.FindSlotIndex(SlotName), TextureName, attach);

        Spine.Skin combined = m_Spine.skeleton.Data.FindSkin("Cha_1");
        combined.AddFromSkin(skin);

        m_Spine.skeleton.Skin = null;
        m_Spine.skeleton.SetSkin(combined);
        m_Spine.skeleton.SetSlotsToSetupPose();
        m_Spine.AnimationState.Apply(m_Spine.skeleton);
        m_Spine.skeleton.Update(0);
    }
    
    void Combine_(string slotName, string skinName)
    {
        Spine.Skeleton skeleton = m_Spine.skeleton;
        Spine.SkeletonData skeletonData = skeleton.Data;
        int gunSlotIndex = skeleton.FindSlotIndex(slotName);
        // Get the template skin.
        Spine.Skin combined = skeletonData.FindSkin("Cha_1");

        // Clone the template gun Attachment, and map the sprite onto it.
        // This sample uses the sprite and material set in the inspector.
        SpineAtlasAsset atlas = Resources.Load<SpineAtlasAsset>("SpineAtlas/cha_1p_Atlas");
        Spine.AtlasRegion region = atlas.GetAtlas().FindRegion(skinName);

        // Get the gun
        Spine.Attachment templateGun = combined.GetAttachment(gunSlotIndex, skinName);
        Spine.Attachment newGun = templateGun.GetRemappedClone(region); // This has some optional parameters. See below.
        //Attachment newGun = templateGun.GetRemappedClone(gunSprite, sourceMaterial, premultiplyAlpha: true, cloneMeshAsLinked: true, useOriginalRegionSize: false); // (Full signature.)

        // Prepare the custom skin.
        Spine.Skin skin = new Spine.Skin("Cha_1");
        // Add the gun to your new custom skin.
        skin.SetAttachment(gunSlotIndex, skinName, newGun);

        combined.AddFromSkin(skin);

        // Set and apply the Skin to the skeleton.
        skeleton.SetSkin(combined);
        skeleton.SetSlotsToSetupPose();
        m_Spine.Update(0);
    }
}

 

728x90

댓글