using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEditor; using UnityEditorInternal; using UnityEngine; using VketCloudGUITools.Runtime; using VketCloudGUITools.Serialization; using VketCloudGUITools.Utilities; namespace VketCloudGUITools.Editor { [CustomEditor(typeof(VCButton))] public class VCButtonInspector : UnityEditor.Editor { private ReorderableList _guiActionsReordableList = null; VCGUIItemInspectorHelper _inspectorHelper = new VCGUIItemInspectorHelper(); VCStateToggleReordableList _toggleReordableList = null; SerializedProperty _toggleListProperty = null; SerializedProperty _guiActionsProperty = null; List subEditors = new List(); private bool showAdvancedOptions = false; private void OnEnable() { _inspectorHelper.OnEnable(serializedObject, target as IVCGUIItem); _toggleReordableList = new VCStateToggleReordableList(); _toggleListProperty = serializedObject.FindProperty("_toggleList"); _guiActionsProperty = serializedObject.FindProperty("GUIActions"); var button = target as VCButton; RebuildReordableList(button.GUIActions); } private void RebuildReordableList(List currentListItem) { // 更新の必要がないときはスキップ if (currentListItem != null && _guiActionsReordableList != null && _guiActionsReordableList.list == currentListItem) return; var button = target as VCButton; _guiActionsReordableList = new ReorderableList(button.GUIActions, typeof(GUIActionListItem)); _guiActionsReordableList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "GUI Actions"); _guiActionsReordableList.drawElementCallback = (rect, index, isActive, isFocus) => { if (subEditors.IsNotNullAndValidIndex(index) && subEditors[index] != null) { var subInspector = subEditors[index] as SubInspector; using (var scope = new GUI.ClipScope(rect)) { rect.x = 0f; rect.y = 0f; subInspector.OnSubInspectorGUI(rect); } } }; _guiActionsReordableList.elementHeightCallback = index => { if (subEditors.IsNotNullAndValidIndex(index) && subEditors[index] != null) { var subInspector = subEditors[index] as SubInspector; return subInspector.GetSubInspectorHeight(); } return EditorGUIUtility.singleLineHeight; }; _guiActionsReordableList.onAddCallback = list => { Undo.RecordObject(target, "Add GUIAction"); currentListItem.Add(null); }; _guiActionsReordableList.onRemoveCallback = list => { if (0 <= list.index && list.index < button.GUIActions.Count) { Undo.RecordObject(target, "Remove GUIAction"); button.GUIActions.RemoveAt(list.index); } }; } public override void OnInspectorGUI() { var button = target as VCButton; RebuildReordableList(button.GUIActions); serializedObject.Update(); EditorGUI.BeginDisabledGroup(true); _inspectorHelper.DrawPropertyScript(); EditorGUI.EndDisabledGroup(); EditorGUI.BeginDisabledGroup(true); _inspectorHelper.DrawPropertyVcTransform(); EditorGUI.EndDisabledGroup(); EditorGUI.BeginDisabledGroup(true); _inspectorHelper.DrawPropertyParentLayer(); EditorGUI.EndDisabledGroup(); EditorGUI.BeginDisabledGroup(true); _inspectorHelper.DrawPropertyVisible(); EditorGUI.EndDisabledGroup(); EditorGUI.BeginChangeCheck(); _toggleReordableList.OnInspectorGUI(serializedObject, _toggleListProperty); if (EditorGUI.EndChangeCheck()) { // Delay Callにより、SerializedObjectの反映後に適応される EditorApplication.delayCall += () => { var item = (IVCGUIItem)this.target; item.UpdateVisible(); }; } _inspectorHelper.DrawPropertyShow(); _inspectorHelper.DrawPropertyPos(); _inspectorHelper.DrawPropertySize(); _inspectorHelper.DrawPropertyZ(); bool isPlaying = EditorApplication.isPlaying; EditorGUI.BeginDisabledGroup(!isPlaying); if (!isPlaying) { EditorGUILayout.HelpBox("[Interact] is Runtime Only.", MessageType.Info); } if (GUILayout.Button("Interact", GUILayout.Height(EditorGUIUtility.singleLineHeight * 2f))) { button.OnClick(); } EditorGUI.EndDisabledGroup(); serializedObject.ApplyModifiedProperties(); showAdvancedOptions = EditorGUILayout.Foldout(showAdvancedOptions, "Advanced Options"); if (showAdvancedOptions) { DrawGuiActions(); } } private void DrawGuiActions() { var button = target as VCButton; if (button.GUIActions == null) button.GUIActions = new List(); if (subEditors == null) subEditors = new List(); if (subEditors.Count != button.GUIActions.Count) { subEditors.Clear(); for (int i = 0; i < button.GUIActions.Count; ++i) { subEditors.Add(null); } } for (int i = 0; i < button.GUIActions.Count; ++i) { if (button.GUIActions[i] == null) { button.GUIActions[i] = CreateInstance(); } var guiAction = button.GUIActions[i]; var temp = subEditors.SafeGetAt(i); CreateCachedEditor(guiAction, typeof(GUIActionListItemInspector), ref temp); subEditors.SetAtWithFill(i, temp, null); var subInspector = subEditors[i] as SubInspector; subInspector.HideScriptField = true; } if (_guiActionsReordableList != null) _guiActionsReordableList.DoLayoutList(); if (button.UVArea == null || button.UVArea.Count != 4) { if (button.UVAreaRate == null || button.UVAreaRate.Count != 4) { GUILayout.Label("UVArea"); if (GUILayout.Button("New")) { Undo.RecordObject(target, "Create UVArea"); button.UVArea = new List { 0, 0, button.DefaultImage.texture.width, button.DefaultImage.texture.height }; Repaint(); } } } else { GUILayout.Label("UVArea"); if (GUILayout.Button("Delete")) { Undo.RecordObject(target, "Delete UVArea"); button.UVArea = null; Repaint(); } else { EditorGUI.BeginChangeCheck(); var x = EditorGUILayout.IntField("X",button.UVArea[0]); var y = EditorGUILayout.IntField("Y",button.UVArea[1]); var width = EditorGUILayout.IntField("Width",button.UVArea[2]); var height = EditorGUILayout.IntField("Height",button.UVArea[3]); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change UVArea"); button.UVArea = new List { x, y, width, height }; } } } if (button.UVAreaRate == null || button.UVAreaRate.Count != 4) { if (button.UVArea == null || button.UVArea.Count != 4) { GUILayout.Label("UVAreaRate"); if (GUILayout.Button("New")) { Undo.RecordObject(target, "Create UVAreaRate"); button.UVAreaRate = new List { 0, 0, 1, 1 }; Repaint(); } } } else { GUILayout.Label("UVAreaRate"); if (GUILayout.Button("Delete")) { Undo.RecordObject(target, "Delete UVAreaRate"); button.UVAreaRate = null; Repaint(); } else { EditorGUI.BeginChangeCheck(); var x = EditorGUILayout.FloatField("X", button.UVAreaRate[0]); var y = EditorGUILayout.FloatField("Y", button.UVAreaRate[1]); var width = EditorGUILayout.FloatField("Width", button.UVAreaRate[2]); var height = EditorGUILayout.FloatField("Height", button.UVAreaRate[3]); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change UVAreaRate"); button.UVAreaRate = new List { x, y, width, height }; } } } GUILayout.Label("ClickAreaSize"); if (button.ClickAreaSize == null || button.ClickAreaSize.Count != 2) { if (GUILayout.Button("New")) { Undo.RecordObject(target, "Create ClickAreaSize"); button.ClickAreaSize = new List { button.DefaultImage.texture.width, button.DefaultImage.texture.height }; Repaint(); } } else { if (GUILayout.Button("Delete")) { Undo.RecordObject(target, "Delete ClickAreaSize"); button.ClickAreaSize = null; Repaint(); } else { EditorGUI.BeginChangeCheck(); var width = EditorGUILayout.IntField("Width",button.ClickAreaSize[0]); var height = EditorGUILayout.IntField("Height",button.ClickAreaSize[1]); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change ClickAreaSize"); button.ClickAreaSize = new List { width, height }; } } } if (button.DefaultImage == null) { EditorGUILayout.HelpBox( "The Image component attached to this GameObject does not have a Source Image set.", MessageType.Warning); } GUILayout.Label("LTRB(9-Slice)"); if (button.LTRB == null || button.LTRB.Count != 4) { if (GUILayout.Button("New")) { Undo.RecordObject(target, "Create LTRB"); button.LTRB = new List{ 0, 0, 0, 0}; Repaint(); } } else { if (GUILayout.Button("Delete")) { Undo.RecordObject(target, "Delete LTRB"); button.LTRB = null; Repaint(); } else { EditorGUI.BeginChangeCheck(); var left = EditorGUILayout.IntField("Left",button.LTRB[0]); var top = EditorGUILayout.IntField("Top",button.LTRB[1]); var right = EditorGUILayout.IntField("Right",button.LTRB[2]); var bottom = EditorGUILayout.IntField("Bottom",button.LTRB[3]); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change LTRB"); button.LTRB = new List { left, top, right, bottom }; } } } EditorGUI.BeginChangeCheck(); var clickAreaImage = (Sprite)EditorGUILayout.ObjectField("Click Area Image", button.ClickAreaImage, typeof(Sprite), false); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change ClickAreaImage"); button.ClickAreaImage = clickAreaImage; } GUILayout.Label("Flick"); EditorGUI.indentLevel++; EditorGUI.BeginChangeCheck(); var checkTime = EditorGUILayout.IntField("Check Time", button.Flick.CheckTime); var lengthThreshold = EditorGUILayout.IntField("Length Threshold", button.Flick.LengthThreshold); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change Flick"); button.Flick.CheckTime = checkTime; button.Flick.LengthThreshold = lengthThreshold; } EditorGUI.indentLevel--; GUILayout.Label("Color at Button Pressed"); if (button.OnKeyDownMulColor == null || button.OnKeyDownMulColor.Count != 4) { if (GUILayout.Button("New")) { Undo.RecordObject(target, "Create OnKeyDownMulColor"); button.OnKeyDownMulColor = new List { 1.0f, 1.0f, 1.0f, 1.0f }; Repaint(); } } else { if (GUILayout.Button("Delete")) { Undo.RecordObject(target, "Delete OnKeyDownMulColor"); button.OnKeyDownMulColor = null; Repaint(); } else { EditorGUI.BeginChangeCheck(); var newColor = EditorGUILayout.ColorField("Color", new Color(button.OnKeyDownMulColor[0], button.OnKeyDownMulColor[1], button.OnKeyDownMulColor[2], button.OnKeyDownMulColor[3])); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change OnKeyDownMulColor"); button.OnKeyDownMulColor = new List { newColor.r, newColor.g, newColor.b, newColor.a }; } } } } } }