// 合并SkinedMesh到MESH
    void CombineSkinedMeshes(int sel = 0)
    {
#if UNITY_EDITOR
        if (m_SelGameObj == null || m_SelSkinMehes == null || m_SelSkinMehes.Length <= 0)
        {
            Debug.LogError("选中合并对象为NULL");
            return;
        }

        string filePath = string.Empty;
        if (sel != 4)
        {
            filePath = AssetDatabase.GetAssetPath(m_SelGameObj);
            if (string.IsNullOrEmpty(filePath))
            {
                string scenePath = AssetDatabase.GetAssetOrScenePath(m_SelGameObj);
                if (!string.IsNullOrEmpty(scenePath))
                {
#if !UNITY_5_3
                    filePath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelGameObj);
#endif
                    if (string.IsNullOrEmpty(filePath))
                    {
                        Debug.LogError("选中对象文件路径为空");
                        return;
                    }
                }
            }
        }

        List <Mesh> meshList = new List <Mesh>();
        for (int i = 0; i < m_SelSkinMehes.Length; ++i)
        {
            var sklMesh = m_SelSkinMehes[i];
            if (sklMesh != null)
            {
                Mesh         mesh = null;
                MeshRenderer mR   = sklMesh as MeshRenderer;
                if (mR != null)
                {
                    MeshFilter mF = mR.GetComponent <MeshFilter>();
                    if (mF != null)
                    {
                        mesh = mF.sharedMesh;
                    }
                }
                else
                {
                    SkinnedMeshRenderer smR = sklMesh as SkinnedMeshRenderer;
                    if (smR != null)
                    {
                        if (sel == 2)
                        {
                            mesh = new Mesh();
                            smR.BakeMesh(mesh);
                        }
                        else
                        {
                            mesh = smR.sharedMesh;
                        }
                    }
                }
                if (mesh != null)
                {
                    meshList.Add(mesh);
                }
                //var mesh = new Mesh();
                //sklMesh.BakeMesh(mesh);
                //meshList.Add(mesh);
            }
        }

        if (sel == 0)
        {
            filePath = Path.ChangeExtension(filePath, ".dae");
            ExportCollada.Export(meshList, m_SelSkinMehes, filePath);
        }
        else if (sel == 1)
        {
            ExportCollada.ExportSklsToAsset(m_SelSkinMehes, filePath);
        }
        else if (sel == 2)
        {
            string fileName = Path.GetFileNameWithoutExtension(filePath);
            filePath = string.Format("{0}/{1}_bakedMesh.dae", Path.GetDirectoryName(filePath), fileName);
            ExportCollada.Export(meshList, m_SelSkinMehes, filePath);
            if (meshList != null)
            {
                for (int i = 0; i < meshList.Count; ++i)
                {
                    var mesh = meshList[i];
                    if (mesh != null)
                    {
                        GameObject.DestroyImmediate(mesh);
                    }
                }
                meshList.Clear();
            }
        }
        else if (sel == 3)
        {
            // 合并动画到贴图
            filePath = Path.GetDirectoryName(filePath);
            ExportCollada.ExportAnimToTex(meshList, m_SelSkinMehes, m_SelClip, filePath);
        }
        else if (sel == 4)
        {
            // bindPose转TPose
            for (int j = 0; j < m_SelSkinMehes.Length; ++j)
            {
                SkinnedMeshRenderer sklRender = m_SelSkinMehes[j] as SkinnedMeshRenderer;
                if (sklRender != null)
                {
                    Mesh        mesh      = GameObject.Instantiate <Mesh>(sklRender.sharedMesh);
                    Matrix4x4[] bindposes = mesh.bindposes;
                    Transform[] bones     = sklRender.bones;
                    // 按照的bone的位置重新计算bindpose
                    for (int i = 0; i < bones.Length; ++i)
                    {
                        /*
                         * 因为有可能外部进行了缩放或者其他变换,bone.worldToLocalMatrix有节点影响,所以要返乘一个变换sklRender.transform.localToWorldMatrix
                         */
                        bindposes[i] = bones[i].worldToLocalMatrix * sklRender.transform.localToWorldMatrix;
                    }
                    mesh.bindposes = bindposes;
                    // 赋值新的mesh
                    sklRender.sharedMesh = mesh;
                }
            }
        }
        //  ExportCollada.ExportToScene(meshList, m_SelSkinMehes);

        AssetDatabase.Refresh();
#endif
    }
Example #2
0
    // 合并SkinedMesh到MESH
    void CombineSkinedMeshes(int sel = 0)
    {
#if UNITY_EDITOR
        if (m_SelGameObj == null || m_SelSkinMehes == null || m_SelSkinMehes.Length <= 0)
        {
            Debug.LogError("选中合并对象为NULL");
            return;
        }
        string filePath = AssetDatabase.GetAssetPath(m_SelGameObj);
        if (string.IsNullOrEmpty(filePath))
        {
            string scenePath = AssetDatabase.GetAssetOrScenePath(m_SelGameObj);
            if (!string.IsNullOrEmpty(scenePath))
            {
                #if !UNITY_5_3
                filePath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelGameObj);
                #endif
                if (string.IsNullOrEmpty(filePath))
                {
                    Debug.LogError("选中对象文件路径为空");
                    return;
                }
            }
        }

        List <Mesh> meshList = new List <Mesh>();
        for (int i = 0; i < m_SelSkinMehes.Length; ++i)
        {
            var sklMesh = m_SelSkinMehes[i];
            if (sklMesh != null)
            {
                Mesh         mesh = null;
                MeshRenderer mR   = sklMesh as MeshRenderer;
                if (mR != null)
                {
                    MeshFilter mF = mR.GetComponent <MeshFilter>();
                    if (mF != null)
                    {
                        mesh = mF.sharedMesh;
                    }
                }
                else
                {
                    SkinnedMeshRenderer smR = sklMesh as SkinnedMeshRenderer;
                    if (smR != null)
                    {
                        if (sel == 2)
                        {
                            mesh = new Mesh();
                            smR.BakeMesh(mesh);
                        }
                        else
                        {
                            mesh = smR.sharedMesh;
                        }
                    }
                }
                if (mesh != null)
                {
                    meshList.Add(mesh);
                }
                //var mesh = new Mesh();
                //sklMesh.BakeMesh(mesh);
                //meshList.Add(mesh);
            }
        }

        if (sel == 0)
        {
            filePath = Path.ChangeExtension(filePath, ".dae");
            ExportCollada.Export(meshList, m_SelSkinMehes, filePath);
        }
        else if (sel == 1)
        {
            ExportCollada.ExportSklsToAsset(m_SelSkinMehes, filePath);
        }
        else if (sel == 2)
        {
            string fileName = Path.GetFileNameWithoutExtension(filePath);
            filePath = string.Format("{0}/{1}_bakedMesh.dae", Path.GetDirectoryName(filePath), fileName);
            ExportCollada.Export(meshList, m_SelSkinMehes, filePath);
            if (meshList != null)
            {
                for (int i = 0; i < meshList.Count; ++i)
                {
                    var mesh = meshList[i];
                    if (mesh != null)
                    {
                        GameObject.DestroyImmediate(mesh);
                    }
                }
                meshList.Clear();
            }
        }
        else if (sel == 3)
        {
            // 合并动画到贴图
            filePath = Path.GetDirectoryName(filePath);
            ExportCollada.ExportAnimToTex(meshList, m_SelSkinMehes, m_SelClip, filePath);
        }
        //  ExportCollada.ExportToScene(meshList, m_SelSkinMehes);

        AssetDatabase.Refresh();
#endif
    }