void SaveMeshFile(string savePath) { if (format == SaveFormat.None) { return; } string relativePath = ""; if (savePath.StartsWith(Application.dataPath)) { relativePath = "Assets" + savePath.Substring(Application.dataPath.Length); } if (format == SaveFormat.MeshAsset) { if (copy) { Mesh assetMesh = MeshUtility.Copy(filter.sharedMesh); AssetDatabase.CreateAsset(assetMesh, relativePath); } else { AssetDatabase.CreateAsset(filter.sharedMesh, relativePath); } } if (format == SaveFormat.OBJ) { MeshRenderer renderer = meshGen.GetComponent <MeshRenderer>(); string objString = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials); File.WriteAllText(savePath, objString); if (!copy) { DestroyImmediate(filter.sharedMesh); } if (relativePath != "") //Import back the OBJ { AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceSynchronousImport); #if UNITY_5_0 if (!copy) { filter.sharedMesh = (Mesh)AssetDatabase.LoadAssetAtPath(relativePath, typeof(Mesh)); } #else if (!copy) { filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativePath); } #endif } } }
private void Bake(MeshGenerator gen) { MeshFilter filter = gen.GetComponent <MeshFilter>(); if (filter == null) { EditorUtility.DisplayDialog("Save error", "No mesh present in " + gen.name, "OK"); return; } if (copy) { UnityEditor.MeshUtility.Optimize(filter.sharedMesh); Unwrapping.GenerateSecondaryUVSet(filter.sharedMesh); } else { gen.Bake(isStatic, true); } if (format == BakeMeshWindow.SaveFormat.OBJ) { MeshRenderer renderer = gen.GetComponent <MeshRenderer>(); dirInfo = new DirectoryInfo(savePath); FileInfo[] files = dirInfo.GetFiles(filter.sharedMesh.name + "*.obj"); string meshName = filter.sharedMesh.name; if (files.Length > 0) { meshName += "_" + files.Length; } string path = savePath + "/" + meshName + ".obj"; string objString = MeshUtility.ToOBJString(filter.sharedMesh, renderer.sharedMaterials); File.WriteAllText(path, objString); if (copy) { string relativepath = "Assets" + path.Substring(Application.dataPath.Length); AssetDatabase.ImportAsset(relativepath, ImportAssetOptions.ForceSynchronousImport); filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(relativepath); } } if (format == BakeMeshWindow.SaveFormat.MeshAsset) { dirInfo = new DirectoryInfo(savePath); FileInfo[] files = dirInfo.GetFiles(filter.sharedMesh.name + "*.asset"); string meshName = filter.sharedMesh.name; if (files.Length > 0) { meshName += "_" + files.Length; } string path = savePath + "/" + meshName + ".asset"; string relativepath = "Assets" + path.Substring(Application.dataPath.Length); if (copy) { Mesh assetMesh = MeshUtility.Copy(filter.sharedMesh); AssetDatabase.CreateAsset(assetMesh, relativepath); } else { AssetDatabase.CreateAsset(filter.sharedMesh, relativepath); } } if (permanent && !copy) { SplineComputer meshGenComputer = gen.computer; if (permanent) { meshGenComputer.Unsubscribe(gen); Object.DestroyImmediate(gen); } if (removeComputer) { if (meshGenComputer.GetComponents <Component>().Length == 2) { Object.DestroyImmediate(meshGenComputer.gameObject); } else { Object.DestroyImmediate(meshGenComputer); } } } }