/// <summary> /// Save any modifications to the EditableObject. If the mesh is a scene mesh or imported mesh, it /// will be saved to a new asset. If the mesh was originally an asset mesh, the asset is overwritten. /// </summary> /// <param name="mesh">mesh to save</param> /// <param name="meshFilter">will update the mesh filter with the new mesh if not null</param> /// <param name="skinnedMeshRenderer">will update the skinned mesh renderer with the new mesh if not null</param> /// <returns>return true if save was successful, false if user-canceled or otherwise failed.</returns> internal static bool SaveMeshAsset(Mesh mesh, MeshFilter meshFilter = null, SkinnedMeshRenderer skinnedMeshRenderer = null, int overridenDialogResult = -1, string overridenPath = "") { if (mesh == null) { return(false); } string save_path = !string.IsNullOrEmpty(overridenPath) ? overridenPath : DO_NOT_SAVE; ModelSource source = GetMeshGUID(mesh); switch (source) { case ModelSource.Asset: int saveChanges = overridenDialogResult != -1 ? overridenDialogResult : EditorUtility.DisplayDialogComplex( "Save Changes", "Save changes to edited mesh?", "Save", // DIALOG_OK "Cancel", // DIALOG_CANCEL "Save As"); // DIALOG_ALT if (saveChanges == DIALOG_OK) { save_path = AssetDatabase.GetAssetPath(mesh); } else if (saveChanges == DIALOG_ALT) { save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to"); } else { return(false); } break; case ModelSource.Imported: case ModelSource.Scene: default: save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to"); break; } if (!save_path.Equals(DO_NOT_SAVE) && !string.IsNullOrEmpty(save_path)) { Mesh existing = AssetDatabase.LoadAssetAtPath <Mesh>(save_path); bool overwrite = existing != null; Mesh dst = overwrite ? existing : new Mesh(); PolyMeshUtility.Copy(mesh, dst); if (!overwrite) { AssetDatabase.CreateAsset(dst, save_path); } AssetDatabase.Refresh(); return(true); } // Save was canceled return(false); }
/// <summary> /// Save any modifications to the EditableObject. If the mesh is a scene mesh or imported mesh, it /// will be saved to a new asset. If the mesh was originally an asset mesh, the asset is overwritten. /// </summary> /// <param name="mesh">mesh to save</param> /// <param name="meshFilter">will update the mesh filter with the new mesh if not null</param> /// <param name="skinnedMeshRenderer">will update the skinned mesh renderer with the new mesh if not null</param> /// <returns>return true if save was successful, false if user-canceled or otherwise failed.</returns> internal static bool SaveMeshAsset(Mesh mesh, MeshFilter meshFilter = null, SkinnedMeshRenderer skinnedMeshRenderer = null, int overridenDialogResult = -1, string overridenPath = "") { if (mesh == null) { return(false); } string save_path = !string.IsNullOrEmpty(overridenPath) ? overridenPath : DO_NOT_SAVE; ModelSource source = GetMeshGUID(mesh); switch (source) { case ModelSource.Asset: int saveChanges = overridenDialogResult != -1 ? overridenDialogResult : EditorUtility.DisplayDialogComplex( "Save Changes", "Save changes to edited mesh?", "Save", // DIALOG_OK "Cancel", // DIALOG_CANCEL "Save As"); // DIALOG_ALT if (saveChanges == DIALOG_OK) { save_path = AssetDatabase.GetAssetPath(mesh); } else if (saveChanges == DIALOG_ALT) { save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to"); } else { return(false); } break; case ModelSource.Imported: case ModelSource.Scene: default: save_path = EditorUtility.SaveFilePanelInProject("Save Mesh As", mesh.name + ".asset", "asset", "Save edited mesh to"); break; } if (!save_path.Equals(DO_NOT_SAVE) && !string.IsNullOrEmpty(save_path)) { Object existing = AssetDatabase.LoadMainAssetAtPath(save_path); if (existing != null && existing is Mesh) { //if the mesh that we want to create is the same than the mesh found at this path, do nothing if (existing.GetInstanceID() == mesh.GetInstanceID()) { //nothing to do here } // save over an existing mesh asset else { PolyMeshUtility.Copy((Mesh)existing, mesh); Object.DestroyImmediate(mesh, true); } } else { Mesh newMesh = new Mesh(); PolyMeshUtility.Copy(newMesh, mesh); AssetDatabase.CreateAsset(newMesh, save_path); } AssetDatabase.Refresh(); return(true); } // Save was canceled return(false); }