码迷,mamicode.com
首页 > 编程语言 > 详细

unity实现换装系统

时间:2018-06-02 22:21:39      阅读:447      评论:0      收藏:0      [点我收藏+]

标签:one   .sh   reac   child   each   装系统   foreach   contains   share   


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实现换装系统

标签:one   .sh   reac   child   each   装系统   foreach   contains   share   

原文地址:https://www.cnblogs.com/xiaomao21/p/9126918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!