using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; using VketCloudGUITools.Runtime; namespace VketCloudGUITools.Editor { [CustomEditor(typeof(GUIActionListItem))] public class GUIActionListItemInspector : SubInspector { UnityEditor.Editor subEditor = null; private static Dictionary InstanceTypeToInspectorType = null; public static void BuildInstanceTypeToInspectorType() { if (InstanceTypeToInspectorType != null) return; // GUIActionとCustomEditorの対応表を生成する var instanceTypes = typeof(GUIAction).GetSubclasses().Append(typeof(GUIAction)); var inspectorTypes = typeof(GUIActionInspector).GetSubclasses().Append(typeof(GUIActionInspector)); InstanceTypeToInspectorType = new Dictionary(); foreach (var instanceType in instanceTypes) { foreach (var inspectorType in inspectorTypes) { var customAttributes = inspectorType.GetCustomAttributesData(); foreach (var attr in customAttributes) { if (attr.AttributeType == typeof(CustomEditor)) { var targetInstanceType = (Type)attr.ConstructorArguments[0].Value; if (targetInstanceType == instanceType) { InstanceTypeToInspectorType.Add(targetInstanceType, inspectorType); } } } } } } public override void OnSubInspectorGUI(Rect position) { BuildInstanceTypeToInspectorType(); var actionList = target as GUIActionListItem; var oldType = actionList.GUIActionType; base.OnSubInspectorGUI(position); var newType = actionList.GUIActionType; if (newType != oldType) { var newAction = CreateGUIAction(newType); if (newAction != null) { Undo.RecordObject(actionList, "GUI Action List Item"); actionList.GUIAction = newAction; } } Type subInspectorType = null; if (actionList.GUIAction == null || !InstanceTypeToInspectorType.TryGetValue(actionList.GUIAction.GetType(), out subInspectorType)) { subInspectorType = typeof(GUIActionInspector); } CreateCachedEditor(actionList.GUIAction, subInspectorType, ref subEditor); var subInspector = subEditor as SubInspector; if (subInspector != null) { subInspector.HideScriptField = true; var width = EditorGUIUtility.currentViewWidth; var subHeight = subInspector.GetSubInspectorHeight(); Rect subPosition = position; subPosition.yMin += position.height - subHeight; using (new GUI.ClipScope(subPosition)) { subPosition.x = 0f; subPosition.y = 0f; subInspector.OnSubInspectorGUI(subPosition); } } } public override float GetSubInspectorHeight() { float height = base.GetSubInspectorHeight(); var subInspector = subEditor as SubInspector; if (subInspector != null) { height += subInspector.GetSubInspectorHeight(); } return height; } private Runtime.GUIAction CreateGUIAction(GUIAction.FuncType typeIndex) { switch (typeIndex) { case GUIAction.FuncType.Func: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.Flag: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.Index: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.String: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.S2DI: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.SetShowGUI: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.SetShowLayer: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.calljs: return ScriptableObject.CreateInstance(); case GUIAction.FuncType.callscript: { var action = ScriptableObject.CreateInstance(); action.ActionName = "callscript"; return action; } case GUIAction.FuncType.Master: case GUIAction.FuncType.World: case GUIAction.FuncType.Voice: case GUIAction.FuncType.SE: case GUIAction.FuncType.SystemSE: case GUIAction.FuncType.Video: { var action = ScriptableObject.CreateInstance(); action.FuncName = typeIndex.ToString(); return action; } case GUIAction.FuncType.DebugLog: { var action = ScriptableObject.CreateInstance(); action.ActionName = "DebugLog"; return action; } } return null; } } }