Exemple #1
0
        private void ChangeModel(Transform childTrans, string anchorName)
        {
            if (childTrans != null)
            {
                SkinnedMeshRenderer skinnedMesh = childTrans.gameObject.GetComponent <SkinnedMeshRenderer>();
                MeshRenderer        staticMesh  = childTrans.gameObject.GetComponent <MeshRenderer>();
                bool avatarChanged = false;
                if (skinnedMesh != null)
                {
                    skinnedMesh.receiveShadows = false;
                    Transform rootbone = skinnedMesh.rootBone;
                    Transform oldRoot  = UnityTools.FindChildTransform(GameObject.transform, rootbone.name);
                    if (oldRoot != null)
                    {
                        UnityTools.ReplaceNewTran(rootbone, oldRoot);
                    }
                    else
                    {
                        Debug.LogError("找不到替换的骨骼位置");
                    }
                    List <Transform> newTargetBones = new List <Transform>();
                    foreach (Transform srcBone in skinnedMesh.bones)
                    {
                        if (renderNodeObjects.Contains(srcBone.name))
                        {
                            newTargetBones.Add((renderNodeObjects[srcBone.name] as GameObject).transform);
                        }
                    }

                    skinnedMesh.bones    = newTargetBones.ToArray();
                    skinnedMesh.rootBone = (renderNodeObjects[skinnedMesh.rootBone.name] as GameObject).transform;
                    childTrans.parent    = GameObject.transform;

                    avatarChanged = true;

                    NotifySkinnedMeshChanged(skinnedMesh);
                }
                else if (staticMesh != null)
                {
                    staticMesh.receiveShadows = false;
                    bool isExist = bindNodeTable.ContainsKey(anchorName);
                    if (isExist)
                    {
                        Transform bindParent = bindNodeTable[anchorName] as Transform;
                        childTrans.parent        = bindParent;
                        childTrans.localPosition = new Vector3(0, 0, 0);
                        childTrans.localRotation = Quaternion.identity;
                        childTrans.localScale    = Vector3.one;
                        avatarChanged            = true;

                        NotifyStaticMeshChanged(staticMesh);    //< 回调函数aa
                    }
                }
                if (avatarChanged)
                {
                    if (renderNodeObjects.Contains(childTrans.name))
                    {
                        GameObject oldAvatarPart = renderNodeObjects[childTrans.name] as GameObject;
                        oldAvatarPart.transform.parent = null;
                        GameObject.Destroy(oldAvatarPart);
                    }
                    renderNodeObjects[childTrans.name] = childTrans.gameObject;
                    meshTable[childTrans.name]         = childTrans.gameObject;
                    RefreshAvatar();
                }
            }
        }