Unity Spine 换装方案

Spine是目前用的比较广泛的2D骨骼动画插件,在unity中的使用也是比较多的,现在的项目有需要做一个外部资源来换装的换装系统,下面是根据官方的例子实现的一种方案

using System.Collections;
using System.Collections.Generic;
using Spine;
using Spine.Unity;
using Spine.Unity.Modules.AttachmentTools;
using UnityEngine;
public class Princess : MonoBehaviour
{
    /// 
    /// 替换用的脸
    /// 
    [SerializeField] SpineAtlasAsset []faces = null;

    void Start()
    {

    }
    /// 
    /// 换脸
    /// 
    void ChangeFace(int value)
    {
        //获取SkeletonRenderer组件
        var skeletonRenderer = GetComponent();
        //获取选中的脸的Atlas
        var atlas = faces[value].GetAtlas();
        //获取现在的scale
        float scale = skeletonRenderer.skeletonDataAsset.scale;
        //获取要修改的插槽
        Slot slot = skeletonRenderer.Skeleton.FindSlot("face");
        //来源的Attachment
        Attachment originalAttachment = slot.Attachment;
        //获取来源的region
        AtlasRegion region = atlas.FindRegion("face");
        //继承的属性
        bool inheritProperties = true;
        //如果准备的素材都不为null
        if (region == null)
        {
            slot.Attachment = null;
            Debug.Log("获取不到想要的插槽");
        }
        else if (inheritProperties && originalAttachment != null)
        {
            //克隆一个 附件 来赋值给 源插槽的附件
            slot.Attachment = originalAttachment.GetRemappedClone(region, true, true, scale);
        }
        else
        {
            //克隆一个 附件 来赋值给 源插槽的附件
            var newRegionAttachment = region.ToRegionAttachment(region.name, scale);
            slot.Attachment = newRegionAttachment;
        }
    }
}

 

你可能感兴趣的:(unity,经验)