123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- using UnityEngine;
- using UnityEngine.Assertions;
- using UnityEditor;
- using System;
- using System.Collections.Generic;
- using System.IO;
- #if UNITY_2018_3_OR_NEWER
- using UnityEditor.Experimental.SceneManagement;
- #endif
- namespace VoxelImporter
- {
- [CustomEditor(typeof(VoxelChunksObjectChunk))]
- public class VoxelChunksObjectChunkEditor : EditorCommon
- {
- public VoxelChunksObjectChunk chunkTarget { get; private set; }
- public VoxelChunksObject objectTarget { get; private set; }
- public VoxelChunksObjectChunkCore chunkCore { get; protected set; }
- public VoxelChunksObjectCore objectCore { get; protected set; }
- #region GuiStyle
- private GUIStyle guiStyleMagentaBold;
- private GUIStyle guiStyleRedBold;
- private GUIStyle guiStyleFoldoutBold;
- #endregion
- #region Prefab
- #if UNITY_2018_3_OR_NEWER
- protected PrefabAssetType prefabType { get { return PrefabUtility.GetPrefabAssetType(objectTarget.gameObject); } }
- protected bool prefabEnable { get { return prefabType == PrefabAssetType.Regular || isPrefabEditMode; } }
- protected bool isPrefab { get { return false; } }
- protected bool isPrefabEditMode { get { return PrefabStageUtility.GetCurrentPrefabStage() != null && PrefabStageUtility.GetCurrentPrefabStage().prefabContentsRoot != null; } }
- #else
- protected PrefabType prefabType { get { return PrefabUtility.GetPrefabType(objectTarget.gameObject); } }
- protected bool prefabEnable { get { var type = prefabType; return type == PrefabType.Prefab || type == PrefabType.PrefabInstance || type == PrefabType.DisconnectedPrefabInstance; } }
- protected bool isPrefab { get { return prefabType == PrefabType.Prefab; } }
- #endif
- #endregion
- void OnEnable()
- {
- chunkTarget = target as VoxelChunksObjectChunk;
- if (chunkTarget == null) return;
- chunkCore = new VoxelChunksObjectChunkCore(chunkTarget);
- if (chunkTarget.transform.parent == null) return;
- objectTarget = chunkTarget.transform.parent.GetComponent<VoxelChunksObject>();
- if (objectTarget == null) return;
- objectCore = new VoxelChunksObjectCore(objectTarget);
- chunkCore.Initialize();
- }
- public override void OnInspectorGUI()
- {
- if (chunkTarget == null || objectTarget == null)
- {
- DrawDefaultInspector();
- return;
- }
- #if UNITY_2018_3_OR_NEWER
- {
- if (prefabType == PrefabAssetType.Regular && !isPrefabEditMode)
- {
- EditorGUILayout.HelpBox("Prefab can only be edited in Prefab mode.", MessageType.Info);
- EditorGUI.BeginDisabledGroup(true);
- }
- }
- #endif
- #region GuiStyle
- if (guiStyleMagentaBold == null)
- guiStyleMagentaBold = new GUIStyle(EditorStyles.boldLabel);
- guiStyleMagentaBold.normal.textColor = Color.magenta;
- if (guiStyleRedBold == null)
- guiStyleRedBold = new GUIStyle(EditorStyles.boldLabel);
- guiStyleRedBold.normal.textColor = Color.red;
- if (guiStyleFoldoutBold == null)
- guiStyleFoldoutBold = new GUIStyle(EditorStyles.foldout);
- guiStyleFoldoutBold.fontStyle = FontStyle.Bold;
- #endregion
- serializedObject.Update();
- EditorGUI.BeginDisabledGroup(isPrefab);
- InspectorGUI();
- EditorGUI.EndDisabledGroup();
- serializedObject.ApplyModifiedProperties();
- #if UNITY_2018_3_OR_NEWER
- {
- if (prefabType == PrefabAssetType.Regular && !isPrefabEditMode)
- {
- EditorGUI.EndDisabledGroup();
- }
- }
- #endif
- }
- protected void InspectorGUI()
- {
- #region Simple
- {
- EditorGUI.BeginChangeCheck();
- var mode = GUILayout.Toolbar(objectTarget.advancedMode ? 1 : 0, VoxelBaseEditor.Edit_AdvancedModeStrings);
- if (EditorGUI.EndChangeCheck())
- {
- objectTarget.advancedMode = mode != 0 ? true : false;
- }
- }
- #endregion
- EditorGUILayout.Space();
- Action<UnityEngine.Object, string> TypeTitle = (o, title) =>
- {
- if (o == null)
- EditorGUILayout.LabelField(title, guiStyleMagentaBold);
- else if (prefabEnable && !AssetDatabase.Contains(o))
- EditorGUILayout.LabelField(title, guiStyleRedBold);
- else
- EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
- };
- {
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.Space();
- if (GUILayout.Button("Reset Transform"))
- {
- Undo.RecordObject(chunkTarget.transform, "Reset Transform");
- chunkTarget.transform.localPosition = chunkTarget.basicOffset;
- chunkTarget.transform.localRotation = Quaternion.identity;
- chunkTarget.transform.localScale = Vector3.one;
- }
- EditorGUILayout.Space();
- EditorGUILayout.EndHorizontal();
- }
- #region Object
- if (objectTarget.advancedMode)
- {
- chunkTarget.edit_objectFoldout = EditorGUILayout.Foldout(chunkTarget.edit_objectFoldout, "Object", guiStyleFoldoutBold);
- if (chunkTarget.edit_objectFoldout)
- {
- EditorGUILayout.BeginVertical(GUI.skin.box);
- #region Mesh
- {
- TypeTitle(chunkTarget.mesh, "Mesh");
- EditorGUI.indentLevel++;
- #region Mesh
- {
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUI.BeginDisabledGroup(true);
- EditorGUILayout.ObjectField(chunkTarget.mesh, typeof(Mesh), false);
- EditorGUI.EndDisabledGroup();
- }
- if (chunkTarget.mesh != null)
- {
- if (!IsMainAsset(chunkTarget.mesh))
- {
- if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Create Mesh
- string path = EditorUtility.SaveFilePanel("Save mesh", chunkCore.GetDefaultPath(), string.Format("{0}_{1}_mesh.asset", objectTarget.gameObject.name, chunkTarget.chunkName), "asset");
- if (!string.IsNullOrEmpty(path))
- {
- if (path.IndexOf(Application.dataPath) < 0)
- {
- SaveInsideAssetsFolderDisplayDialog();
- }
- else
- {
- Undo.RecordObject(objectTarget, "Save Mesh");
- Undo.RecordObject(chunkTarget, "Save Mesh");
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Mesh.Instantiate(chunkTarget.mesh), path);
- chunkTarget.mesh = AssetDatabase.LoadAssetAtPath<Mesh>(path);
- Refresh();
- }
- }
- #endregion
- }
- }
- {
- if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Reset Mesh
- Undo.RecordObject(objectTarget, "Reset Mesh");
- Undo.RecordObject(chunkTarget, "Reset Mesh");
- chunkTarget.mesh = null;
- Refresh();
- #endregion
- }
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- #region Vertex Count
- {
- EditorGUILayout.LabelField("Vertex Count", chunkTarget.mesh != null ? chunkTarget.mesh.vertexCount.ToString() : "");
- }
- #endregion
- EditorGUI.indentLevel--;
- }
- #endregion
- #region Material
- if (objectTarget.materialMode == VoxelChunksObject.MaterialMode.Individual)
- {
- {
- if (chunkTarget.materials == null || chunkTarget.materials.Count == 0)
- EditorGUILayout.LabelField("Material", guiStyleMagentaBold);
- else if (prefabEnable)
- {
- bool contains = true;
- for (int i = 0; i < chunkTarget.materials.Count; i++)
- {
- if (!AssetDatabase.Contains(chunkTarget.materials[i]))
- {
- contains = false;
- break;
- }
- }
- EditorGUILayout.LabelField("Material", contains ? EditorStyles.boldLabel : guiStyleRedBold);
- }
- else
- EditorGUILayout.LabelField("Material", EditorStyles.boldLabel);
- }
- EditorGUI.indentLevel++;
- #region Material
- for (int i = 0; i < chunkTarget.materials.Count; i++)
- {
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUI.BeginDisabledGroup(true);
- EditorGUILayout.ObjectField(chunkTarget.materials[i], typeof(Material), false);
- EditorGUI.EndDisabledGroup();
- }
- if (chunkTarget.materials[i] != null)
- {
- if (!IsMainAsset(chunkTarget.materials[i]))
- {
- if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Create Material
- string defaultName = string.Format("{0}_{1}_mat{2}.mat", objectTarget.gameObject.name, chunkTarget.chunkName, i);
- string path = EditorUtility.SaveFilePanel("Save material", chunkCore.GetDefaultPath(), defaultName, "mat");
- if (!string.IsNullOrEmpty(path))
- {
- if (path.IndexOf(Application.dataPath) < 0)
- {
- SaveInsideAssetsFolderDisplayDialog();
- }
- else
- {
- Undo.RecordObject(objectTarget, "Save Material");
- Undo.RecordObject(chunkTarget, "Save Material");
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.CreateAsset(Material.Instantiate(chunkTarget.materials[i]), path);
- chunkTarget.materials[i] = AssetDatabase.LoadAssetAtPath<Material>(path);
- Refresh();
- }
- }
- #endregion
- }
- }
- {
- if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Reset Material
- Undo.RecordObject(objectTarget, "Reset Material");
- Undo.RecordObject(chunkTarget, "Reset Material");
- if (!IsMainAsset(chunkTarget.materials[i]))
- chunkTarget.materials[i] = null;
- else
- chunkTarget.materials[i] = Instantiate<Material>(chunkTarget.materials[i]);
- Refresh();
- #endregion
- }
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- EditorGUI.indentLevel--;
- }
- #endregion
- #region Texture
- if (objectTarget.materialMode == VoxelChunksObject.MaterialMode.Individual)
- {
- TypeTitle(chunkTarget.atlasTexture, "Texture");
- EditorGUI.indentLevel++;
- #region Texture
- {
- EditorGUILayout.BeginHorizontal();
- {
- EditorGUI.BeginDisabledGroup(true);
- EditorGUILayout.ObjectField(chunkTarget.atlasTexture, typeof(Texture2D), false);
- EditorGUI.EndDisabledGroup();
- }
- if (chunkTarget.atlasTexture != null)
- {
- if (!IsMainAsset(chunkTarget.atlasTexture))
- {
- if (GUILayout.Button("Save", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Create Texture
- string defaultName = string.Format("{0}_{1}_tex.png", objectTarget.gameObject.name, chunkTarget.chunkName);
- string path = EditorUtility.SaveFilePanel("Save atlas texture", chunkCore.GetDefaultPath(), defaultName, "png");
- if (!string.IsNullOrEmpty(path))
- {
- if (path.IndexOf(Application.dataPath) < 0)
- {
- SaveInsideAssetsFolderDisplayDialog();
- }
- else
- {
- Undo.RecordObject(objectTarget, "Save Atlas Texture");
- Undo.RecordObject(chunkTarget, "Save Atlas Texture");
- File.WriteAllBytes(path, chunkTarget.atlasTexture.EncodeToPNG());
- path = path.Replace(Application.dataPath, "Assets");
- AssetDatabase.ImportAsset(path);
- objectCore.SetTextureImporterSetting(path, chunkTarget.atlasTexture);
- chunkTarget.atlasTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
- Refresh();
- }
- }
- #endregion
- }
- }
- {
- if (GUILayout.Button("Reset", GUILayout.Width(48), GUILayout.Height(16)))
- {
- #region Reset Texture
- Undo.RecordObject(objectTarget, "Reset Atlas Texture");
- Undo.RecordObject(chunkTarget, "Reset Atlas Texture");
- chunkTarget.atlasTexture = null;
- Refresh();
- #endregion
- }
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- #endregion
- #region Texture Size
- {
- EditorGUILayout.LabelField("Texture Size", chunkTarget.atlasTexture != null ? string.Format("{0} x {1}", chunkTarget.atlasTexture.width, chunkTarget.atlasTexture.height) : "");
- }
- #endregion
- EditorGUI.indentLevel--;
- }
- #endregion
- EditorGUILayout.EndVertical();
- }
- }
- else
- {
- EditorGUILayout.Space();
- }
- #endregion
- #region Refresh
- if (GUILayout.Button("Refresh"))
- {
- Undo.RecordObject(objectTarget, "Inspector");
- Undo.RecordObject(chunkTarget, "Inspector");
- Refresh();
- }
- #endregion
- }
- protected void Refresh()
- {
- objectCore.ReCreate();
- }
- }
- }
|