Esempio n. 1
0
        void SaveToFile(string path, List <Mesh> exportMeshes)
        {
            MeshExportData data = ScriptableObject.CreateInstance <MeshExportData>();

            data._meshes = exportMeshes.ToArray();

            AssetDatabase.CreateAsset(data, path);
            foreach (Mesh mesh in exportMeshes)
            {
                AssetDatabase.AddObjectToAsset(mesh, data);
            }
        }
Esempio n. 2
0
        public void ExportMeshToFile(GameObject go)
        {
            string path = EditorUtility.SaveFilePanelInProject("Save Mesh", go.name, "asset", "Please enter a file name to save the mesh(es) to");

            if (!string.IsNullOrEmpty(path))
            {
                Dictionary <int, MeshFilter>          index2MeshFilter      = new Dictionary <int, MeshFilter>();
                Dictionary <int, SkinnedMeshRenderer> index2SkinnedRenderer = new Dictionary <int, SkinnedMeshRenderer>();

                List <Mesh> exportMeshes = new List <Mesh>();

                {
                    MeshFilter[] meshFilters;
                    if (_includeAllChildren)
                    {
                        meshFilters = go.GetComponentsInChildren <MeshFilter>(_includeInactive);
                    }
                    else
                    {
                        meshFilters    = new MeshFilter[1];
                        meshFilters[0] = go.GetComponent <MeshFilter>();
                    }

                    foreach (MeshFilter meshFilter in meshFilters)
                    {
                        if (meshFilter != null && meshFilter.sharedMesh != null)
                        {
                            Mesh mesh = CopyMesh(meshFilter.sharedMesh.name, meshFilter.sharedMesh, false);
                            if (mesh != null)
                            {
                                if (_autoAssignMesh)
                                {
                                    index2MeshFilter.Add(exportMeshes.Count, meshFilter);
                                }

                                exportMeshes.Add(mesh);
                            }
                        }
                    }
                }


                {
                    SkinnedMeshRenderer[] skinnedRenderers;
                    if (_includeAllChildren)
                    {
                        skinnedRenderers = go.GetComponentsInChildren <SkinnedMeshRenderer>(_includeInactive);
                    }
                    else
                    {
                        skinnedRenderers    = new SkinnedMeshRenderer[1];
                        skinnedRenderers[0] = go.GetComponent <SkinnedMeshRenderer>();
                    }

                    foreach (SkinnedMeshRenderer skinnedRenderer in skinnedRenderers)
                    {
                        if (skinnedRenderer != null && skinnedRenderer.sharedMesh != null)
                        {
                            Mesh mesh = CopyMesh(skinnedRenderer.sharedMesh.name, skinnedRenderer.sharedMesh, true);
                            if (mesh != null)
                            {
                                if (_autoAssignMesh)
                                {
                                    index2SkinnedRenderer.Add(exportMeshes.Count, skinnedRenderer);
                                }

                                exportMeshes.Add(mesh);
                            }
                        }
                    }
                }


                if (exportMeshes.Count > 0)
                {
                    SaveToFile(path, exportMeshes);
                    AssetDatabase.ImportAsset(path);

                    MeshExportData meshExportData = AssetDatabase.LoadAssetAtPath(path, typeof(MeshExportData)) as MeshExportData;
                    if (_autoAssignMesh)
                    {
                        for (int i = 0; i < meshExportData._meshes.Length; i++)
                        {
                            MeshFilter meshFilterOrgin;
                            if (index2MeshFilter.TryGetValue(i, out meshFilterOrgin))
                            {
                                meshFilterOrgin.sharedMesh = meshExportData._meshes[i];
                            }
                            else
                            {
                                SkinnedMeshRenderer skinnedRendererOrgin;
                                if (index2SkinnedRenderer.TryGetValue(i, out skinnedRendererOrgin))
                                {
                                    skinnedRendererOrgin.sharedMesh = meshExportData._meshes[i];
                                }
                            }
                        }

                        UtilityFuncs.MarkCurrentSceneIsDirty();
                    }

                    Debug.Log("成功导出Mesh数据到" + path, meshExportData);
                }
                else
                {
                    EditorUtility.DisplayDialog("提示", "没有导出任何Mesh数据!", "OK");
                }
            }
        }