unity实现换装系统

Posted xiaomao21

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity实现换装系统相关的知识,希望对你有一定的参考价值。


public class AvatarSys : MonoBehaviour
{

    //private GameObject girlSource;//资源model
    private Transform girlSourceTrans;
    private GameObject girlTarget;//骨架物体
    private Dictionary<string, Dictionary<string, SkinnedMeshRenderer>> girlData = new Dictionary<string, Dictionary<string, SkinnedMeshRenderer>>();
    Transform[] girlHips;//小女孩骨骼信息
    private Dictionary<string, SkinnedMeshRenderer> girlSmr = new Dictionary<string, SkinnedMeshRenderer>();//换装骨骼身上的skm信息
    private string[,] girlStr = new string[,] { { "eyes", "1" }, { "hair", "1" }, { "top", "1" }, { "pants", "1" }, { "shoes", "1" }, { "face", "1" } };

    private void Start()
    {
        InstantiateSource();
        InstantiateTarget();
        SaveData();
        InitAvatart();
    }

    void InstantiateSource()
    {
        GameObject Go = Instantiate(Resources.Load("FemaleModel")) as GameObject;
        girlSourceTrans = Go.transform;
        Go.SetActive(false);
    }

    void InstantiateTarget()
    {
        girlTarget = Instantiate(Resources.Load("FemaleTarget")) as GameObject;
        girlHips = girlTarget.GetComponentsInChildren<Transform>();
    }

    void SaveData()
    {
        if (girlSourceTrans == null)
            return;
        //遍历所有子物体有SkinedMeshRenderer
        SkinnedMeshRenderer[] parts = girlSourceTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
        foreach (var part in parts)
        {
            string[] names = part.name.Split(‘-‘);
            if (!girlData.ContainsKey(names[0]))
            {
                //生成对应的部位,且只生成一个
                GameObject partGo = new GameObject();
                partGo.name = names[0];
                partGo.transform.parent = girlTarget.transform;
                girlSmr.Add(names[0], partGo.AddComponent<SkinnedMeshRenderer>());//把骨骼target身上的skm信息存储
                girlData.Add(names[0], new Dictionary<string, SkinnedMeshRenderer>());
            }
            girlData[names[0]].Add(names[1], part);//存储所以的skm信息到数据里面
        }
    }

    void ChangeMesh(string part, string num)
    {//传入部位,编号,从data里面拿取对应的skm
     //要跟换的部位
        SkinnedMeshRenderer skm = girlData[part][num];
        List<Transform> bones = new List<Transform>();
        foreach (var trans in skm.bones)
        {
            foreach (var bone in girlHips)
            {
                if (bone.name == trans.name)
                {
                    bones.Add(bone);
                    break;
                }
            }
        }
        //换装的实现
        girlSmr[part].bones = bones.ToArray();
        girlSmr[part].materials = skm.materials;
        girlSmr[part].sharedMesh = skm.sharedMesh;
    }

    void InitAvatart()
    {
        //初始化骨架,让他有mesh材质 骨骼信息
        int length = girlStr.GetLength(0);//获得行数
        for (int i = 0; i < length; i++)
        {
            ChangeMesh(girlStr[i, 0], girlStr[i, 1]);//让角色穿
        }
    }
}

以上是关于unity实现换装系统的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D换装系统

Unity记录一下skinnedmeshrender换装 系统的关键

Unity 实现 角色的换装

unity人物换装

Unity角色换装问题

Unity体感设备KinectV2虚拟换装解决方案