VoxelChunksObjectExplosionEditor.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using System;
  6. using System.IO;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace VoxelImporter
  10. {
  11. [CustomEditor(typeof(VoxelChunksObjectExplosion))]
  12. public class VoxelChunksObjectExplosionEditor : VoxelBaseExplosionEditor
  13. {
  14. public VoxelChunksObjectExplosion explosionObject { get; protected set; }
  15. public VoxelChunksObjectExplosionCore explosionObjectCore { get; protected set; }
  16. protected override void OnEnable()
  17. {
  18. base.OnEnable();
  19. explosionBase = explosionObject = target as VoxelChunksObjectExplosion;
  20. if (explosionObject == null) return;
  21. explosionCore = explosionObjectCore = new VoxelChunksObjectExplosionCore(explosionObject);
  22. if (explosionCore.voxelBase == null)
  23. {
  24. if (explosionObject.chunksExplosion != null)
  25. {
  26. for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
  27. {
  28. Undo.DestroyObjectImmediate(explosionObject.chunksExplosion[i]);
  29. }
  30. }
  31. Undo.DestroyObjectImmediate(explosionBase);
  32. return;
  33. }
  34. OnEnableInitializeSet();
  35. }
  36. protected override void Inspector_MeshMaterial()
  37. {
  38. #region Material
  39. if (explosionObjectCore.voxelObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
  40. {
  41. if (explosionObject.materials != null)
  42. {
  43. if (prefabEnable)
  44. {
  45. bool contains = true;
  46. for (int i = 0; i < explosionObject.materials.Count; i++)
  47. {
  48. if (explosionObject.materials[i] == null || !AssetDatabase.Contains(explosionObject.materials[i]))
  49. contains = false;
  50. }
  51. EditorGUILayout.LabelField("Material", contains ? EditorStyles.boldLabel : guiStyleRedBold);
  52. }
  53. else
  54. {
  55. EditorGUILayout.LabelField("Material", EditorStyles.boldLabel);
  56. }
  57. }
  58. else
  59. {
  60. EditorGUILayout.LabelField("Material", guiStyleMagentaBold);
  61. }
  62. {
  63. EditorGUI.indentLevel++;
  64. {
  65. if (explosionObject.materials != null)
  66. {
  67. for (int i = 0; i < explosionObject.materials.Count; i++)
  68. {
  69. EditorGUILayout.BeginHorizontal();
  70. {
  71. EditorGUI.BeginDisabledGroup(true);
  72. EditorGUILayout.ObjectField(explosionObject.materials[i], typeof(Material), false);
  73. EditorGUI.EndDisabledGroup();
  74. }
  75. if (explosionObject.materials[i] != null)
  76. {
  77. if (!IsMainAsset(explosionObject.materials[i]))
  78. {
  79. if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
  80. {
  81. #region Create Material
  82. string path = EditorUtility.SaveFilePanel("Save material", explosionCore.voxelBaseCore.GetDefaultPath(), string.Format("{0}_explosion_mat{1}.mat", explosionObject.gameObject.name, i), "mat");
  83. if (!string.IsNullOrEmpty(path))
  84. {
  85. if (path.IndexOf(Application.dataPath) < 0)
  86. {
  87. SaveInsideAssetsFolderDisplayDialog();
  88. }
  89. else
  90. {
  91. Undo.RecordObject(explosionObject, "Save Material");
  92. path = path.Replace(Application.dataPath, "Assets");
  93. AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.materials[i]), path);
  94. explosionObject.materials[i] = AssetDatabase.LoadAssetAtPath<Material>(path);
  95. explosionCore.Generate();
  96. }
  97. }
  98. #endregion
  99. }
  100. }
  101. {
  102. if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
  103. {
  104. #region Reset Material
  105. Undo.RecordObject(explosionObject, "Reset Material");
  106. if (!IsMainAsset(explosionObject.materials[i]))
  107. explosionObject.materials[i] = null;
  108. else
  109. explosionObject.materials[i] = Instantiate<Material>(explosionObject.materials[i]);
  110. explosionCore.Generate();
  111. #endregion
  112. }
  113. }
  114. }
  115. EditorGUILayout.EndHorizontal();
  116. }
  117. }
  118. }
  119. EditorGUI.indentLevel--;
  120. }
  121. }
  122. #endregion
  123. }
  124. [MenuItem("CONTEXT/VoxelChunksObjectExplosion/Save All Unsaved Assets")]
  125. private static void ContextSaveAllUnsavedAssets(MenuCommand menuCommand)
  126. {
  127. var explosionObject = menuCommand.context as VoxelChunksObjectExplosion;
  128. if (explosionObject == null) return;
  129. var explosionCore = new VoxelChunksObjectExplosionCore(explosionObject);
  130. var folder = EditorUtility.OpenFolderPanel("Save all", explosionCore.voxelBaseCore.GetDefaultPath(), null);
  131. if (string.IsNullOrEmpty(folder)) return;
  132. if (folder.IndexOf(Application.dataPath) < 0)
  133. {
  134. SaveInsideAssetsFolderDisplayDialog();
  135. return;
  136. }
  137. Undo.RecordObject(explosionObject, "Save All Unsaved Assets");
  138. if (explosionObject.chunksExplosion != null)
  139. Undo.RecordObjects(explosionObject.chunksExplosion, "Save All Unsaved Assets");
  140. if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Combine)
  141. {
  142. if (explosionObject.chunksExplosion != null)
  143. {
  144. for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
  145. {
  146. if (explosionObject.chunksExplosion[i] == null) continue;
  147. #region Mesh
  148. for (int j = 0; j < explosionObject.chunksExplosion[i].meshes.Count; j++)
  149. {
  150. if (explosionObject.chunksExplosion[i].meshes[j] != null && explosionObject.chunksExplosion[i].meshes[j].mesh != null && !IsMainAsset(explosionObject.chunksExplosion[i].meshes[j].mesh))
  151. {
  152. var chunkObject = explosionObject.chunksExplosion[i].GetComponent<VoxelChunksObjectChunk>();
  153. if (chunkObject == null) continue;
  154. var path = folder + "/" + string.Format("{0}_{1}_explosion_mesh{2}.asset", explosionObject.gameObject.name, chunkObject.chunkName, j);
  155. path = path.Replace(Application.dataPath, "Assets");
  156. AssetDatabase.CreateAsset(Mesh.Instantiate(explosionObject.chunksExplosion[i].meshes[j].mesh), path);
  157. explosionObject.chunksExplosion[i].meshes[j].mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
  158. }
  159. }
  160. #endregion
  161. }
  162. }
  163. #region Material
  164. if (explosionObject.materials != null)
  165. {
  166. for (int index = 0; index < explosionObject.materials.Count; index++)
  167. {
  168. if (explosionObject.materials[index] == null) continue;
  169. if (IsMainAsset(explosionObject.materials[index])) continue;
  170. var path = folder + "/" + string.Format("{0}_explosion_mat{1}.mat", explosionObject.gameObject.name, index);
  171. path = path.Replace(Application.dataPath, "Assets");
  172. AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.materials[index]), path);
  173. explosionObject.materials[index] = AssetDatabase.LoadAssetAtPath<Material>(path);
  174. }
  175. }
  176. #endregion
  177. }
  178. else if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Individual)
  179. {
  180. if (explosionObject.chunksExplosion != null)
  181. {
  182. for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
  183. {
  184. if (explosionObject.chunksExplosion[i] == null) continue;
  185. var chunkObject = explosionObject.chunksExplosion[i].GetComponent<VoxelChunksObjectChunk>();
  186. if (chunkObject == null) continue;
  187. #region Mesh
  188. for (int j = 0; j < explosionObject.chunksExplosion[i].meshes.Count; j++)
  189. {
  190. if (explosionObject.chunksExplosion[i].meshes[j] != null && explosionObject.chunksExplosion[i].meshes[j].mesh != null && !IsMainAsset(explosionObject.chunksExplosion[i].meshes[j].mesh))
  191. {
  192. var path = folder + "/" + string.Format("{0}_{1}_explosion_mesh{2}.asset", explosionObject.gameObject.name, chunkObject.chunkName, j);
  193. path = path.Replace(Application.dataPath, "Assets");
  194. AssetDatabase.CreateAsset(Mesh.Instantiate(explosionObject.chunksExplosion[i].meshes[j].mesh), path);
  195. explosionObject.chunksExplosion[i].meshes[j].mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
  196. }
  197. }
  198. #endregion
  199. #region Material
  200. if (explosionObject.chunksExplosion[i].materials != null)
  201. {
  202. for (int index = 0; index < explosionObject.chunksExplosion[i].materials.Count; index++)
  203. {
  204. if (explosionObject.chunksExplosion[i].materials[index] == null) continue;
  205. if (IsMainAsset(explosionObject.chunksExplosion[i].materials[index])) continue;
  206. var path = folder + "/" + string.Format("{0}_{1}_explosion_mat{2}.mat", explosionObject.gameObject.name, chunkObject.chunkName, index);
  207. path = path.Replace(Application.dataPath, "Assets");
  208. AssetDatabase.CreateAsset(Material.Instantiate(explosionObject.chunksExplosion[i].materials[index]), path);
  209. explosionObject.chunksExplosion[i].materials[index] = AssetDatabase.LoadAssetAtPath<Material>(path);
  210. }
  211. }
  212. #endregion
  213. }
  214. }
  215. }
  216. else
  217. {
  218. Assert.IsTrue(false);
  219. }
  220. explosionCore.Generate();
  221. InternalEditorUtility.RepaintAllViews();
  222. }
  223. [MenuItem("CONTEXT/VoxelChunksObjectExplosion/Reset All Assets")]
  224. private static void ResetAllSavedAssets(MenuCommand menuCommand)
  225. {
  226. var explosionObject = menuCommand.context as VoxelChunksObjectExplosion;
  227. if (explosionObject == null) return;
  228. var explosionCore = new VoxelChunksObjectExplosionCore(explosionObject);
  229. Undo.RecordObject(explosionObject, "Reset All Assets");
  230. if (explosionObject.chunksExplosion != null)
  231. Undo.RecordObjects(explosionObject.chunksExplosion, "Reset All Assets");
  232. #region Mesh
  233. if (explosionObject.chunksExplosion != null)
  234. {
  235. for (int i = 0; i < explosionObject.chunksExplosion.Length; i++)
  236. {
  237. if (explosionObject.chunksExplosion[i] == null) continue;
  238. explosionObject.chunksExplosion[i].meshes = null;
  239. if (explosionObject.chunksExplosion[i].materials == null) continue;
  240. for (int j = 0; j < explosionObject.chunksExplosion[i].materials.Count; j++)
  241. {
  242. if (explosionObject.chunksExplosion[i].materials[j] == null) continue;
  243. if (!IsMainAsset(explosionObject.chunksExplosion[i].materials[j]))
  244. explosionObject.chunksExplosion[i].materials[j] = null;
  245. else
  246. explosionObject.chunksExplosion[i].materials[j] = Instantiate<Material>(explosionObject.chunksExplosion[i].materials[j]);
  247. }
  248. }
  249. }
  250. #endregion
  251. #region Material
  252. if (explosionObject.materials != null)
  253. {
  254. for (int i = 0; i < explosionObject.materials.Count; i++)
  255. {
  256. if (explosionObject.materials[i] == null) continue;
  257. if (!IsMainAsset(explosionObject.materials[i]))
  258. explosionObject.materials[i] = null;
  259. else
  260. explosionObject.materials[i] = Instantiate<Material>(explosionObject.materials[i]);
  261. }
  262. }
  263. #endregion
  264. explosionCore.Generate();
  265. InternalEditorUtility.RepaintAllViews();
  266. }
  267. }
  268. }