using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditor.PostProcessing; using UnityEngine.UCMobile; using System; using System.Reflection; using System.Linq; [CustomEditor(typeof(UCMobile))] public class UCMobileEditor : Editor { GUIStyle header; Dictionary m_CustomEditors = new Dictionary(); public void OnEnable() { if (target == null) return; var assembly = typeof(UCMobileEditor).Assembly; var editorTypes = assembly.GetTypes() .Where(x => x.IsDefined(typeof(UCMobileEditorAttribute), false)); var customEditors = new Dictionary(); foreach (var editor in editorTypes) { var attr = (UCMobileEditorAttribute)editor.GetCustomAttributes(typeof(UCMobileEditorAttribute), false)[0]; var effectType = attr.type; var alwaysEnabled = attr.alwaysEnabled; var editorInst = (UCMobileModelEditor)Activator.CreateInstance(editor); editorInst.alwaysEnabled = alwaysEnabled; editorInst.profile = target as UCMobile; editorInst.inspector = this; customEditors.Add(effectType, editorInst); } var baseType = target.GetType(); var property = serializedObject.GetIterator(); while (property.Next(true)) { if (!property.hasChildren) continue; var type = baseType; var srcObject = UnityEngine.UCMobile.ReflectionUtils.GetFieldValueFromPath(serializedObject.targetObject, ref type, property.propertyPath); if (srcObject == null) continue; UCMobileModelEditor editor; if (customEditors.TryGetValue(type, out editor)) { var effect = (UCMobileModel)srcObject; if (editor.alwaysEnabled) effect.enabled = editor.alwaysEnabled; m_CustomEditors.Add(editor, effect); editor.target = effect; editor.serializedProperty = property.Copy(); editor.OnPreEnable(); } } } public override void OnInspectorGUI() { header = new GUIStyle("ShurikenModuleTitle") { font = (new GUIStyle("Label")).font, border = new RectOffset(15, 7, 4, 4), fixedHeight = 22, contentOffset = new Vector2(20f, -2f) }; foreach (var editor in m_CustomEditors) { EditorGUI.BeginChangeCheck(); EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); SerializedProperty IsEnable = editor.Key.serializedProperty.FindPropertyRelative("m_Enabled"); SerializedProperty IsUsed = editor.Key.serializedProperty.FindPropertyRelative("IsUsed"); var rect = GUILayoutUtility.GetRect(16f, 20f, header); GUI.Box(rect, editor.Key.title, header); var toggleRect = new Rect(rect.x + 4f, rect.y + 1f, 12f, 12f); IsUsed.boolValue = GUI.Toggle(toggleRect, IsUsed.boolValue, editor.Key.title); GUILayout.EndHorizontal(); var e = Event.current; if (e.type == EventType.MouseDown) { if (toggleRect.Contains(e.mousePosition)) { e.Use(); } else if (rect.Contains(e.mousePosition)) { IsEnable.boolValue = !IsEnable.boolValue; e.Use(); } } if (IsEnable.boolValue) { using (new EditorGUI.DisabledGroupScope(!IsEnable.boolValue)) { EditorGUILayout.Separator(); editor.Key.OnInspectorGUI(); EditorGUILayout.Separator(); } } if (EditorGUI.EndChangeCheck()) editor.Value.OnValidate(); editor.Key.serializedProperty.serializedObject.ApplyModifiedProperties(); } EditorGUILayout.Separator(); } }