123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using UnityEngine;
- using UnityEngine.Assertions;
- using UnityEditor;
- using UnityEditorInternal;
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- namespace VoxelImporter
- {
- [CustomEditor(typeof(VoxelChunksObjectExplosion))]
- public class VoxelChunksObjectExplosionEditor : VoxelBaseExplosionEditor
- {
- public VoxelChunksObjectExplosion explosionObject { get; protected set; }
- public VoxelChunksObjectExplosionCore explosionObjectCore { get; protected set; }
- protected override void OnEnable()
- {
- base.OnEnable();
- explosionBase = explosionObject = target as VoxelChunksObjectExplosion;
- if (explosionObject == null) return;
- explosionCore = explosionObjectCore = new VoxelChunksObjectExplosionCore(explosionObject);
- if (explosionCore.voxelBase == null)
- {
- if (explosionObject.chunksExplosion != null)
- {
- for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
- {
- Undo.DestroyObjectImmediate(explosionObject.chunksExplosion[i]);
- }
- }
- Undo.DestroyObjectImmediate(explosionBase);
- return;
- }
- OnEnableInitializeSet();
- }
- protected override void Inspector_MeshMaterial()
- {
- #region Material
- if (explosionObjectCore.voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
- {
- if (explosionObject.materials != null)
- {
- if (prefabEnable)
- {
- bool contains = true;
- for (int i = 0; i < explosionObject.materials.Count; i++)
- {
- if (explosionObject.materials[i] == null || !AssetDatabase.Contains(explosionObject.materials[i]))
- contains = false;
- }
- EditorGUILayout.LabelField("Material", contains ? EditorStyles.boldLabel : guiStyleRedBold);
- }
- else
- {
- EditorGUILayout.LabelField("Material", EditorStyles.boldLabel);
- }
- }
- else
- {
- EditorGUILayout.LabelField("Material", guiStyleMagentaBold);
- }
- {
- EditorGUI.indentLevel++;
- {
- if (explosionObject.materials != null)
- {
- for (int i = 0; i < explosionObject.materials.Count; i++)
- {
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUI.BeginDisabledGroup(true);
- EditorGUILayout.ObjectField(explosionObject.materials[i], typeof(Material), false);
- EditorGUI.EndDisabledGroup();
- }
- if (explosionObject.materials[i] != null)
- {
- if (!IsMainAsset(explosionObject.materials[i]))
- {
- if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Create Material
- string path = EditorUtility.SaveFilePanel("Save material", explosionCore.voxelBaseCore.GetDefaultPath(), string.Format("{0}_explosion_mat{1}.mat", explosionObject.gameObject.name, i), "mat");
- if (!string.IsNullOrEmpty(path))
- {
- if (path.IndexOf(Application.dataPath) < 0)
- {
- SaveInsideAssetsFolderDisplayDialog();
- }
- else
- {
- Undo.RecordObject(explosionObject, "Save Material");
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.materials[i]), path);
- explosionObject.materials[i] = AssetDatabase.LoadAssetAtPath<Material>(path);
- explosionCore.Generate();
- }
- }
- #endregion
- }
- }
- {
- if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Reset Material
- Undo.RecordObject(explosionObject, "Reset Material");
- if (!IsMainAsset(explosionObject.materials[i]))
- explosionObject.materials[i] = null;
- else
- explosionObject.materials[i] = Instantiate<Material>(explosionObject.materials[i]);
- explosionCore.Generate();
- #endregion
- }
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- }
- }
- EditorGUI.indentLevel--;
- }
- }
- #endregion
- }
- [MenuItem("CONTEXT/VoxelChunksObjectExplosion/Save All Unsaved Assets")]
- private static void ContextSaveAllUnsavedAssets(MenuCommand menuCommand)
- {
- var explosionObject = menuCommand.context as VoxelChunksObjectExplosion;
- if (explosionObject == null) return;
- var explosionCore = new VoxelChunksObjectExplosionCore(explosionObject);
- var folder = EditorUtility.OpenFolderPanel("Save all", explosionCore.voxelBaseCore.GetDefaultPath(), null);
- if (string.IsNullOrEmpty(folder)) return;
- if (folder.IndexOf(Application.dataPath) < 0)
- {
- SaveInsideAssetsFolderDisplayDialog();
- return;
- }
- Undo.RecordObject(explosionObject, "Save All Unsaved Assets");
- if (explosionObject.chunksExplosion != null)
- Undo.RecordObjects(explosionObject.chunksExplosion, "Save All Unsaved Assets");
- if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
- {
- if (explosionObject.chunksExplosion != null)
- {
- for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
- {
- if (explosionObject.chunksExplosion[i] == null) continue;
- #region Mesh
- for (int j = 0; j < explosionObject.chunksExplosion[i].meshes.Count; j++)
- {
- if (explosionObject.chunksExplosion[i].meshes[j] != null && explosionObject.chunksExplosion[i].meshes[j].mesh != null && !IsMainAsset(explosionObject.chunksExplosion[i].meshes[j].mesh))
- {
- var chunkObject = explosionObject.chunksExplosion[i].GetComponent<VoxelChunksObjectChunk>();
- if (chunkObject == null) continue;
- var path = folder + "/" + string.Format("{0}_{1}_explosion_mesh{2}.asset", explosionObject.gameObject.name, chunkObject.chunkName, j);
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Mesh.Instantiate(explosionObject.chunksExplosion[i].meshes[j].mesh), path);
- explosionObject.chunksExplosion[i].meshes[j].mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
- }
- }
- #endregion
- }
- }
- #region Material
- if (explosionObject.materials != null)
- {
- for (int index = 0; index < explosionObject.materials.Count; index++)
- {
- if (explosionObject.materials[index] == null) continue;
- if (IsMainAsset(explosionObject.materials[index])) continue;
- var path = folder + "/" + string.Format("{0}_explosion_mat{1}.mat", explosionObject.gameObject.name, index);
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.materials[index]), path);
- explosionObject.materials[index] = AssetDatabase.LoadAssetAtPath<Material>(path);
- }
- }
- #endregion
- }
- else if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
- {
- if (explosionObject.chunksExplosion != null)
- {
- for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
- {
- if (explosionObject.chunksExplosion[i] == null) continue;
- var chunkObject = explosionObject.chunksExplosion[i].GetComponent<VoxelChunksObjectChunk>();
- if (chunkObject == null) continue;
- #region Mesh
- for (int j = 0; j < explosionObject.chunksExplosion[i].meshes.Count; j++)
- {
- if (explosionObject.chunksExplosion[i].meshes[j] != null && explosionObject.chunksExplosion[i].meshes[j].mesh != null && !IsMainAsset(explosionObject.chunksExplosion[i].meshes[j].mesh))
- {
- var path = folder + "/" + string.Format("{0}_{1}_explosion_mesh{2}.asset", explosionObject.gameObject.name, chunkObject.chunkName, j);
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Mesh.Instantiate(explosionObject.chunksExplosion[i].meshes[j].mesh), path);
- explosionObject.chunksExplosion[i].meshes[j].mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
- }
- }
- #endregion
- #region Material
- if (explosionObject.chunksExplosion[i].materials != null)
- {
- for (int index = 0; index < explosionObject.chunksExplosion[i].materials.Count; index++)
- {
- if (explosionObject.chunksExplosion[i].materials[index] == null) continue;
- if (IsMainAsset(explosionObject.chunksExplosion[i].materials[index])) continue;
- var path = folder + "/" + string.Format("{0}_{1}_explosion_mat{2}.mat", explosionObject.gameObject.name, chunkObject.chunkName, index);
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.chunksExplosion[i].materials[index]), path);
- explosionObject.chunksExplosion[i].materials[index] = AssetDatabase.LoadAssetAtPath<Material>(path);
- }
- }
- #endregion
- }
- }
- }
- else
- {
- Assert.IsTrue(false);
- }
- explosionCore.Generate();
- InternalEditorUtility.RepaintAllViews();
- }
- [MenuItem("CONTEXT/VoxelChunksObjectExplosion/Reset All Assets")]
- private static void ResetAllSavedAssets(MenuCommand menuCommand)
- {
- var explosionObject = menuCommand.context as VoxelChunksObjectExplosion;
- if (explosionObject == null) return;
- var explosionCore = new VoxelChunksObjectExplosionCore(explosionObject);
- Undo.RecordObject(explosionObject, "Reset All Assets");
- if (explosionObject.chunksExplosion != null)
- Undo.RecordObjects(explosionObject.chunksExplosion, "Reset All Assets");
- #region Mesh
- if (explosionObject.chunksExplosion != null)
- {
- for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
- {
- if (explosionObject.chunksExplosion[i] == null) continue;
- explosionObject.chunksExplosion[i].meshes = null;
- if (explosionObject.chunksExplosion[i].materials == null) continue;
- for (int j = 0; j < explosionObject.chunksExplosion[i].materials.Count; j++)
- {
- if (explosionObject.chunksExplosion[i].materials[j] == null) continue;
- if (!IsMainAsset(explosionObject.chunksExplosion[i].materials[j]))
- explosionObject.chunksExplosion[i].materials[j] = null;
- else
- explosionObject.chunksExplosion[i].materials[j] = Instantiate<Material>(explosionObject.chunksExplosion[i].materials[j]);
- }
- }
- }
- #endregion
- #region Material
- if (explosionObject.materials != null)
- {
- for (int i = 0; i < explosionObject.materials.Count; i++)
- {
- if (explosionObject.materials[i] == null) continue;
- if (!IsMainAsset(explosionObject.materials[i]))
- explosionObject.materials[i] = null;
- else
- explosionObject.materials[i] = Instantiate<Material>(explosionObject.materials[i]);
- }
- }
- #endregion
- explosionCore.Generate();
- InternalEditorUtility.RepaintAllViews();
- }
- }
- }
|