Пример #1
0
        public void AddBone(string name)
        {
            var binfo = new BoneInfo();

            binfo.tpose = Matrix4x4.identity;
            binfo.bone  = this.transform.Find(name);
            if (binfo.bone == null)
            {
                Debug.LogError("No find bone");
            }
            bones.Add(binfo);
            _checkBone();
        }
Пример #2
0
        public void InitBone(string saveAvatarName = "", string exportAvatarName = "")
        {
            if (bones == null)
            {
                bones = new List <BoneInfo>();
            }

            var ss = this.transform.GetComponentsInChildren <SkinnedMeshRenderer> ();

            foreach (var s in ss)
            {
                if (s.sharedMesh != null)
                {
                    for (int i = 0; i < s.bones.Length; i++)
                    {
                        var  b    = s.bones [i];
                        bool bnew = true;
                        foreach (var _b in bones)
                        {
                            if (_b.bone == b)
                            {
                                bnew = false;
                                break;
                            }
                        }
                        if (bnew)
                        {
                            var binfo = new BoneInfo();
                            binfo.bone  = b;
                            binfo.tpose = s.sharedMesh.bindposes [i];
                            bones.Add(binfo);
                        }
                    }
                }
            }

            //骨骼配置文件操作
            if (!string.IsNullOrEmpty(saveAvatarName) || !string.IsNullOrEmpty(exportAvatarName))
            {
                //先记录下当前骨骼的信息
                Dictionary <string, string> curDic = new Dictionary <string, string> ();
                for (int i = 0; i < bones.Count; i++)
                {
                    curDic.Add(bones [i].bone.name, bones [i].tpose.ToString());
                }

                string avatarInfoName = !string.IsNullOrEmpty(saveAvatarName) ? saveAvatarName : exportAvatarName;
                Dictionary <string, string> allDic;
                string infoPath = AvatarInfoPath + avatarInfoName + ".txt";

                if (!string.IsNullOrEmpty(exportAvatarName))
                {
                    if (File.Exists(infoPath))
                    {
                        string ctx = File.ReadAllText(infoPath);
                        try {
                            allDic = JsonUtil.DeserializeObject <Dictionary <string, string> > (ctx);
                        } catch {
                            allDic = new Dictionary <string, string> ();
                        }

                        //将配置中所有的节点添加进去
                        foreach (KeyValuePair <string, string> item in allDic)
                        {
                            if (!curDic.ContainsKey(item.Key))
                            {
                                if (item.Key == "shoulder_R")
                                {
                                    Debug.Log("aaa");
                                }
                                var binfo = new BoneInfo();
                                binfo.bone  = getTransform(this.transform, item.Key);
                                binfo.tpose = getMatrixByString(item.Value);
                                bones.Add(binfo);
                            }
                        }
                    }
                }
                else
                {
                    StartCoroutine(WaitForSaveConfig(infoPath, curDic));
                }
            }

            //自动添加asbone对象
            var ss2 = this.transform.GetComponentsInChildren <Asbone>();

            foreach (var asb in ss2)
            {
                var  b    = asb.transform;
                bool bnew = true;
                foreach (var _b in bones)
                {
                    if (_b.bone == b)
                    {
                        bnew = false;
                        break;
                    }
                }
                if (bnew)
                {
                    var binfo = new BoneInfo();
                    binfo.bone  = b;
                    binfo.tpose = Matrix4x4.identity;
                    bones.Add(binfo);
                }
            }
        }