private static void ResetAllSavedAssets(MenuCommand menuCommand) { var objectTarget = menuCommand.context as VoxelChunksObject; if (objectTarget == null) { return; } var objectCore = new VoxelChunksObjectCore(objectTarget); Undo.RecordObject(objectTarget, "Reset All Assets"); if (objectTarget.chunks != null) { Undo.RecordObjects(objectTarget.chunks, "Save All Unsaved Assets"); } objectCore.ResetAllAssets(); objectCore.ReCreate(); InternalEditorUtility.RepaintAllViews(); }
private static void SaveAllUnsavedAssets(MenuCommand menuCommand) { var objectTarget = menuCommand.context as VoxelChunksObject; if (objectTarget == null) { return; } var objectCore = new VoxelChunksObjectCore(objectTarget); var folder = EditorUtility.OpenFolderPanel("Save all", objectCore.GetDefaultPath(), null); if (string.IsNullOrEmpty(folder)) { return; } if (folder.IndexOf(Application.dataPath) < 0) { EditorCommon.SaveInsideAssetsFolderDisplayDialog(); return; } Undo.RecordObject(objectTarget, "Save All Unsaved Assets"); if (objectTarget.chunks != null) { Undo.RecordObjects(objectTarget.chunks, "Save All Unsaved Assets"); } if (objectTarget.materialMode == VoxelChunksObject.MaterialMode.Combine) { #region Material if (objectTarget.materials != null) { for (int index = 0; index < objectTarget.materials.Count; index++) { if (objectTarget.materials[index] == null || EditorCommon.IsMainAsset(objectTarget.materials[index])) { continue; } var path = folder + "/" + string.Format("{0}_mat{1}.mat", objectTarget.gameObject.name, index); path = FileUtil.GetProjectRelativePath(path); path = AssetDatabase.GenerateUniqueAssetPath(path); AssetDatabase.CreateAsset(Material.Instantiate(objectTarget.materials[index]), path); objectTarget.materials[index] = AssetDatabase.LoadAssetAtPath <Material>(path); } } #endregion #region Texture if (objectTarget.atlasTexture != null && !EditorCommon.IsMainAsset(objectTarget.atlasTexture)) { var path = folder + "/" + string.Format("{0}_tex.png", objectTarget.gameObject.name); path = EditorCommon.GenerateUniqueAssetFullPath(path); File.WriteAllBytes(path, objectTarget.atlasTexture.EncodeToPNG()); path = FileUtil.GetProjectRelativePath(path); AssetDatabase.ImportAsset(path); objectCore.SetTextureImporterSetting(path, objectTarget.atlasTexture); objectTarget.atlasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(path); } #endregion if (objectTarget.chunks != null) { for (int i = 0; i < objectTarget.chunks.Length; i++) { if (objectTarget.chunks[i] == null) { continue; } #region Mesh if (objectTarget.chunks[i].mesh != null && !EditorCommon.IsMainAsset(objectTarget.chunks[i].mesh)) { var path = folder + "/" + string.Format("{0}_{1}_mesh.asset", objectTarget.gameObject.name, objectTarget.chunks[i].chunkName); path = FileUtil.GetProjectRelativePath(path); path = AssetDatabase.GenerateUniqueAssetPath(path); AssetDatabase.CreateAsset(Mesh.Instantiate(objectTarget.chunks[i].mesh), path); objectTarget.chunks[i].mesh = AssetDatabase.LoadAssetAtPath <Mesh>(path); } #endregion } } } else if (objectTarget.materialMode == VoxelChunksObject.MaterialMode.Individual) { if (objectTarget.chunks != null) { for (int i = 0; i < objectTarget.chunks.Length; i++) { if (objectTarget.chunks[i] == null) { continue; } #region Mesh if (objectTarget.chunks[i].mesh != null && !EditorCommon.IsMainAsset(objectTarget.chunks[i].mesh)) { var path = folder + "/" + string.Format("{0}_{1}_mesh.asset", objectTarget.gameObject.name, objectTarget.chunks[i].chunkName); path = FileUtil.GetProjectRelativePath(path); path = AssetDatabase.GenerateUniqueAssetPath(path); AssetDatabase.CreateAsset(Mesh.Instantiate(objectTarget.chunks[i].mesh), path); objectTarget.chunks[i].mesh = AssetDatabase.LoadAssetAtPath <Mesh>(path); } #endregion #region Material if (objectTarget.chunks[i].materials != null) { for (int index = 0; index < objectTarget.chunks[i].materials.Count; index++) { if (objectTarget.chunks[i].materials[index] == null || EditorCommon.IsMainAsset(objectTarget.chunks[i].materials[index])) { continue; } var path = folder + "/" + string.Format("{0}_{1}_mat{2}.mat", objectTarget.gameObject.name, objectTarget.chunks[i].chunkName, index); path = FileUtil.GetProjectRelativePath(path); path = AssetDatabase.GenerateUniqueAssetPath(path); AssetDatabase.CreateAsset(Material.Instantiate(objectTarget.chunks[i].materials[index]), path); objectTarget.chunks[i].materials[index] = AssetDatabase.LoadAssetAtPath <Material>(path); } } #endregion #region Texture if (objectTarget.chunks[i].atlasTexture != null && !EditorCommon.IsMainAsset(objectTarget.chunks[i].atlasTexture)) { var path = folder + "/" + string.Format("{0}_{1}_tex.png", objectTarget.gameObject.name, objectTarget.chunks[i].chunkName); path = EditorCommon.GenerateUniqueAssetFullPath(path); File.WriteAllBytes(path, objectTarget.chunks[i].atlasTexture.EncodeToPNG()); path = FileUtil.GetProjectRelativePath(path); AssetDatabase.ImportAsset(path); objectCore.SetTextureImporterSetting(path, objectTarget.chunks[i].atlasTexture); objectTarget.chunks[i].atlasTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(path); } #endregion } } } else { Assert.IsTrue(false); } objectCore.ReCreate(); InternalEditorUtility.RepaintAllViews(); }
private static void ResetAllSavedAssets(MenuCommand menuCommand) { var objectTarget = menuCommand.context as VoxelChunksObject; if (objectTarget == null) { return; } var objectCore = new VoxelChunksObjectCore(objectTarget); Undo.RecordObject(objectTarget, "Reset All Assets"); if (objectTarget.chunks != null) { Undo.RecordObjects(objectTarget.chunks, "Save All Unsaved Assets"); } #region Material if (objectTarget.materials != null) { for (int i = 0; i < objectTarget.materials.Count; i++) { if (!IsMainAsset(objectTarget.materials[i])) { objectTarget.materials[i] = null; } else { objectTarget.materials[i] = Instantiate <Material>(objectTarget.materials[i]); } } } #endregion #region Texture objectTarget.atlasTexture = null; #endregion if (objectTarget.chunks != null) { for (int i = 0; i < objectTarget.chunks.Length; i++) { if (objectTarget.chunks[i] == null) { continue; } objectTarget.chunks[i].mesh = null; #region Material if (objectTarget.chunks[i].materials != null) { for (int j = 0; j < objectTarget.chunks[i].materials.Count; j++) { if (!IsMainAsset(objectTarget.chunks[i].materials[j])) { objectTarget.chunks[i].materials[j] = null; } else { objectTarget.chunks[i].materials[j] = Instantiate <Material>(objectTarget.chunks[i].materials[j]); } } } #endregion objectTarget.chunks[i].atlasTexture = null; } } objectCore.ReCreate(); InternalEditorUtility.RepaintAllViews(); }