VoxelObjectEditor.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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(VoxelObject))]
  12. public class VoxelObjectEditor : VoxelBaseEditor
  13. {
  14. public VoxelObject objectTarget { get; protected set; }
  15. public VoxelObjectCore objectCore { get; protected set; }
  16. public virtual Mesh mesh { get { return objectTarget.mesh; } set { objectTarget.mesh = value; } }
  17. public virtual List<Material> materials { get { return objectTarget.materials; } set { objectTarget.materials = value; } }
  18. public virtual Texture2D atlasTexture { get { return objectTarget.atlasTexture; } set { objectTarget.atlasTexture = value; } }
  19. protected override void OnEnable()
  20. {
  21. base.OnEnable();
  22. objectTarget = target as VoxelObject;
  23. if (objectTarget != null)
  24. {
  25. baseCore = objectCore = new VoxelObjectCore(objectTarget);
  26. OnEnableInitializeSet();
  27. }
  28. }
  29. protected override void InspectorGUI()
  30. {
  31. base.InspectorGUI();
  32. #if UNITY_2018_3_OR_NEWER
  33. {
  34. if (prefabType == PrefabAssetType.Regular && !baseCore.isPrefabEditMode)
  35. {
  36. EditorGUI.BeginDisabledGroup(true);
  37. }
  38. }
  39. #endif
  40. InspectorGUI_Import();
  41. InspectorGUI_Object();
  42. InspectorGUI_Refresh();
  43. #if UNITY_2018_3_OR_NEWER
  44. {
  45. if (prefabType == PrefabAssetType.Regular && !baseCore.isPrefabEditMode)
  46. {
  47. EditorGUI.EndDisabledGroup();
  48. }
  49. }
  50. #endif
  51. }
  52. protected virtual void InspectorGUI_Object()
  53. {
  54. #region Object
  55. if (!string.IsNullOrEmpty(baseTarget.voxelFilePath))
  56. {
  57. //Object
  58. baseTarget.edit_objectFoldout = EditorGUILayout.Foldout(baseTarget.edit_objectFoldout, "Object", guiStyleFoldoutBold);
  59. if (baseTarget.edit_objectFoldout)
  60. {
  61. EditorGUILayout.BeginVertical(GUI.skin.box);
  62. InspectorGUI_Object_Mesh();
  63. InspectorGUI_Object_Material();
  64. InspectorGUI_Object_Texture();
  65. EditorGUILayout.EndVertical();
  66. }
  67. }
  68. #endregion
  69. }
  70. protected void TypeTitle(UnityEngine.Object o, string title)
  71. {
  72. if (o == null)
  73. EditorGUILayout.LabelField(title, guiStyleMagentaBold);
  74. else if (prefabEnable && !AssetDatabase.Contains(o))
  75. EditorGUILayout.LabelField(title, guiStyleRedBold);
  76. else
  77. EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
  78. }
  79. protected virtual void InspectorGUI_Object_Mesh()
  80. {
  81. #region Mesh
  82. if (baseTarget.advancedMode)
  83. {
  84. TypeTitle(mesh, "Mesh");
  85. EditorGUI.indentLevel++;
  86. #region Mesh
  87. {
  88. EditorGUILayout.BeginHorizontal();
  89. {
  90. EditorGUI.BeginDisabledGroup(true);
  91. EditorGUILayout.ObjectField(mesh, typeof(Mesh), false);
  92. EditorGUI.EndDisabledGroup();
  93. }
  94. if (mesh != null)
  95. {
  96. if (!IsMainAsset(mesh))
  97. {
  98. if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
  99. {
  100. #region Create Mesh
  101. string path = EditorUtility.SaveFilePanel("Save mesh", objectCore.GetDefaultPath(), string.Format("{0}_mesh.asset", baseTarget.gameObject.name), "asset");
  102. if (!string.IsNullOrEmpty(path))
  103. {
  104. if (path.IndexOf(Application.dataPath) < 0)
  105. {
  106. SaveInsideAssetsFolderDisplayDialog();
  107. }
  108. else
  109. {
  110. UndoRecordObject("Save Mesh");
  111. path = path.Replace(Application.dataPath, "Assets");
  112. AssetDatabase.CreateAsset(Mesh.Instantiate(mesh), path);
  113. mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
  114. Refresh();
  115. }
  116. }
  117. #endregion
  118. }
  119. }
  120. {
  121. if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
  122. {
  123. #region Reset Mesh
  124. UndoRecordObject("Reset Mesh");
  125. mesh = null;
  126. Refresh();
  127. #endregion
  128. }
  129. }
  130. }
  131. EditorGUILayout.EndHorizontal();
  132. }
  133. #endregion
  134. InspectorGUI_Object_Mesh_Settings();
  135. #region Vertex Count
  136. {
  137. EditorGUILayout.LabelField("Vertex Count", mesh != null ? mesh.vertexCount.ToString() : "");
  138. }
  139. #endregion
  140. EditorGUI.indentLevel--;
  141. }
  142. #endregion
  143. }
  144. protected virtual void InspectorGUI_Object_Material()
  145. {
  146. #region Material
  147. {
  148. #region Title
  149. {
  150. if (materials == null || materials.Count == 0)
  151. EditorGUILayout.LabelField("Material", guiStyleMagentaBold);
  152. else if (prefabEnable)
  153. {
  154. bool contains = true;
  155. for (int i = 0; i < materials.Count; i++)
  156. {
  157. if (materials[i] == null || !AssetDatabase.Contains(materials[i]))
  158. {
  159. contains = false;
  160. break;
  161. }
  162. }
  163. EditorGUILayout.LabelField("Material", contains ? EditorStyles.boldLabel : guiStyleRedBold);
  164. }
  165. else
  166. EditorGUILayout.LabelField("Material", EditorStyles.boldLabel);
  167. }
  168. #endregion
  169. EditorGUI.indentLevel++;
  170. #region updateMeshRendererMaterials
  171. if (baseTarget.advancedMode)
  172. {
  173. EditorGUI.BeginChangeCheck();
  174. var updateMeshRendererMaterials = EditorGUILayout.ToggleLeft("Update the Mesh Renderer Materials", baseTarget.updateMeshRendererMaterials);
  175. if (EditorGUI.EndChangeCheck())
  176. {
  177. if (EditorUtility.DisplayDialog("Update the Mesh Renderer Materials", "It will be changed.\nAre you sure?", "ok", "cancel"))
  178. {
  179. UndoRecordObject("Inspector");
  180. baseTarget.updateMeshRendererMaterials = updateMeshRendererMaterials;
  181. baseCore.SetRendererCompornent();
  182. }
  183. }
  184. }
  185. #endregion
  186. if (materialList != null)
  187. {
  188. materialList.DoLayoutList();
  189. }
  190. InspectorGUI_ConfigureMaterial();
  191. EditorGUI.indentLevel--;
  192. }
  193. #endregion
  194. }
  195. protected virtual void InspectorGUI_Object_Texture()
  196. {
  197. #region Texture
  198. if (baseTarget.advancedMode)
  199. {
  200. TypeTitle(atlasTexture, "Texture");
  201. EditorGUI.indentLevel++;
  202. #region updateMaterialTexture
  203. {
  204. EditorGUI.BeginChangeCheck();
  205. var updateMaterialTexture = EditorGUILayout.ToggleLeft("Update the Material Texture", baseTarget.updateMaterialTexture);
  206. if (EditorGUI.EndChangeCheck())
  207. {
  208. if (EditorUtility.DisplayDialog("Update the Material Texture", "It will be changed.\nAre you sure?", "ok", "cancel"))
  209. {
  210. UndoRecordObject("Inspector");
  211. baseTarget.updateMaterialTexture = updateMaterialTexture;
  212. baseCore.SetRendererCompornent();
  213. }
  214. }
  215. }
  216. #endregion
  217. #region Texture
  218. {
  219. EditorGUILayout.BeginHorizontal();
  220. {
  221. EditorGUI.BeginDisabledGroup(true);
  222. EditorGUILayout.ObjectField(atlasTexture, typeof(Texture2D), false);
  223. EditorGUI.EndDisabledGroup();
  224. }
  225. if (atlasTexture != null)
  226. {
  227. if (!IsMainAsset(atlasTexture))
  228. {
  229. if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
  230. {
  231. #region Create Texture
  232. string path = EditorUtility.SaveFilePanel("Save atlas texture", objectCore.GetDefaultPath(), string.Format("{0}_tex.png", baseTarget.gameObject.name), "png");
  233. if (!string.IsNullOrEmpty(path))
  234. {
  235. if (path.IndexOf(Application.dataPath) < 0)
  236. {
  237. SaveInsideAssetsFolderDisplayDialog();
  238. }
  239. else
  240. {
  241. UndoRecordObject("Save Atlas Texture");
  242. var newTex = Texture2D.Instantiate(atlasTexture);
  243. File.WriteAllBytes(path, newTex.EncodeToPNG());
  244. path = path.Replace(Application.dataPath, "Assets");
  245. AssetDatabase.ImportAsset(path);
  246. objectCore.SetTextureImporterSetting(path, newTex);
  247. atlasTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  248. Refresh();
  249. }
  250. }
  251. #endregion
  252. }
  253. }
  254. {
  255. if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
  256. {
  257. #region Reset Texture
  258. UndoRecordObject("Reset Atlas Texture");
  259. atlasTexture = null;
  260. Refresh();
  261. #endregion
  262. }
  263. }
  264. }
  265. EditorGUILayout.EndHorizontal();
  266. }
  267. #endregion
  268. #region Generate Mip Maps
  269. if (!IsMainAsset(atlasTexture))
  270. {
  271. EditorGUI.BeginChangeCheck();
  272. var generateMipMaps = EditorGUILayout.Toggle("Generate Mip Maps", baseTarget.generateMipMaps);
  273. if (EditorGUI.EndChangeCheck())
  274. {
  275. UndoRecordObject("Inspector");
  276. baseTarget.generateMipMaps = generateMipMaps;
  277. Refresh();
  278. }
  279. }
  280. #endregion
  281. #region Texture Size
  282. {
  283. EditorGUILayout.LabelField("Texture Size", atlasTexture != null ? string.Format("{0} x {1}", atlasTexture.width, atlasTexture.height) : "");
  284. }
  285. #endregion
  286. EditorGUI.indentLevel--;
  287. }
  288. #endregion
  289. }
  290. protected override List<Material> GetMaterialListMaterials()
  291. {
  292. return materials;
  293. }
  294. protected virtual void SaveAllUnsavedAssets()
  295. {
  296. ContextSaveAllUnsavedAssets(new MenuCommand(baseTarget));
  297. }
  298. [MenuItem("CONTEXT/VoxelObject/Save All Unsaved Assets")]
  299. private static void ContextSaveAllUnsavedAssets(MenuCommand menuCommand)
  300. {
  301. var objectTarget = menuCommand.context as VoxelObject;
  302. if (objectTarget == null) return;
  303. var objectCore = new VoxelObjectCore(objectTarget);
  304. var folder = EditorUtility.OpenFolderPanel("Save all", objectCore.GetDefaultPath(), null);
  305. if (string.IsNullOrEmpty(folder)) return;
  306. if (folder.IndexOf(Application.dataPath) < 0)
  307. {
  308. SaveInsideAssetsFolderDisplayDialog();
  309. return;
  310. }
  311. Undo.RecordObject(objectTarget, "Save All Unsaved Assets");
  312. #region Mesh
  313. if (objectTarget.mesh != null && !IsMainAsset(objectTarget.mesh))
  314. {
  315. var path = folder + "/" + string.Format("{0}_mesh.asset", objectTarget.gameObject.name);
  316. path = path.Replace(Application.dataPath, "Assets");
  317. path = AssetDatabase.GenerateUniqueAssetPath(path);
  318. AssetDatabase.CreateAsset(Mesh.Instantiate(objectTarget.mesh), path);
  319. objectTarget.mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
  320. }
  321. #endregion
  322. #region Material
  323. if (objectTarget.materials != null)
  324. {
  325. for (int index = 0; index < objectTarget.materials.Count; index++)
  326. {
  327. if (objectTarget.materials[index] == null || IsMainAsset(objectTarget.materials[index])) continue;
  328. var path = folder + "/" + string.Format("{0}_mat{1}.mat", objectTarget.gameObject.name, index);
  329. path = path.Replace(Application.dataPath, "Assets");
  330. path = AssetDatabase.GenerateUniqueAssetPath(path);
  331. AssetDatabase.CreateAsset(Material.Instantiate(objectTarget.materials[index]), path);
  332. objectTarget.materials[index] = AssetDatabase.LoadAssetAtPath<Material>(path);
  333. }
  334. }
  335. #endregion
  336. #region Texture
  337. if (objectTarget.atlasTexture != null && !IsMainAsset(objectTarget.atlasTexture))
  338. {
  339. var path = folder + "/" + string.Format("{0}_tex.png", objectTarget.gameObject.name);
  340. {
  341. path = AssetDatabase.GenerateUniqueAssetPath(path.Replace(Application.dataPath, "Assets"));
  342. path = (Application.dataPath + path).Replace("AssetsAssets", "Assets");
  343. }
  344. File.WriteAllBytes(path, objectTarget.atlasTexture.EncodeToPNG());
  345. path = path.Replace(Application.dataPath, "Assets");
  346. AssetDatabase.ImportAsset(path);
  347. objectCore.SetTextureImporterSetting(path, objectTarget.atlasTexture);
  348. objectTarget.atlasTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  349. }
  350. #endregion
  351. objectCore.ReCreate();
  352. InternalEditorUtility.RepaintAllViews();
  353. }
  354. [MenuItem("CONTEXT/VoxelObject/Reset All Assets")]
  355. private static void ResetAllSavedAssets(MenuCommand menuCommand)
  356. {
  357. var objectTarget = menuCommand.context as VoxelObject;
  358. if (objectTarget == null) return;
  359. var objectCore = new VoxelObjectCore(objectTarget);
  360. Undo.RecordObject(objectTarget, "Reset All Assets");
  361. #region Mesh
  362. objectTarget.mesh = null;
  363. #endregion
  364. #region Material
  365. if (objectTarget.materials != null)
  366. {
  367. for (int i = 0; i < objectTarget.materials.Count; i++)
  368. {
  369. if (objectTarget.materials[i] == null) continue;
  370. if (!IsMainAsset(objectTarget.materials[i]))
  371. objectTarget.materials[i] = null;
  372. else
  373. objectTarget.materials[i] = Instantiate<Material>(objectTarget.materials[i]);
  374. }
  375. }
  376. #endregion
  377. #region Texture
  378. objectTarget.atlasTexture = null;
  379. #endregion
  380. objectCore.ReCreate();
  381. InternalEditorUtility.RepaintAllViews();
  382. }
  383. [MenuItem("CONTEXT/VoxelObject/Export COLLADA(dae) File", false, 10000)]
  384. private static void ExportDaeFile(MenuCommand menuCommand)
  385. {
  386. var objectTarget = menuCommand.context as VoxelObject;
  387. if (objectTarget == null) return;
  388. var objectCore = new VoxelObjectCore(objectTarget);
  389. string path = EditorUtility.SaveFilePanel("Export COLLADA(dae) File", objectCore.GetDefaultPath(), string.Format("{0}.dae", Path.GetFileNameWithoutExtension(objectTarget.voxelFilePath)), "dae");
  390. if (string.IsNullOrEmpty(path)) return;
  391. if (!objectCore.ExportDaeFile(path))
  392. {
  393. Debug.LogErrorFormat("<color=green>[Voxel Importer]</color> Export COLLADA(dae) File error. file:{0}", path);
  394. }
  395. }
  396. [MenuItem("CONTEXT/VoxelObject/Export COLLADA(dae) File", true)]
  397. private static bool IsValidateExportDaeFile(MenuCommand menuCommand)
  398. {
  399. var objectTarget = menuCommand.context as VoxelObject;
  400. if (objectTarget == null) return false;
  401. #if UNITY_2018_3_OR_NEWER
  402. return true;
  403. #else
  404. return PrefabUtility.GetPrefabType(objectTarget) != PrefabType.Prefab;
  405. #endif
  406. }
  407. [MenuItem("CONTEXT/VoxelObject/Remove All Voxel Importer Compornent", false, 10100)]
  408. private static void RemoveAllVoxelImporterCompornent(MenuCommand menuCommand)
  409. {
  410. var objectTarget = menuCommand.context as VoxelObject;
  411. if (objectTarget == null) return;
  412. Undo.DestroyObjectImmediate(objectTarget);
  413. }
  414. }
  415. }