private static void CustomInspectorGUI(SerializedObject serializedObject, Object target, MonoInspector editor) { //这部分是使用了反编译Editor的代码 EditorGUI.BeginChangeCheck(); serializedObject.Update(); SerializedProperty iterator = serializedObject.GetIterator(); for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false) { using (new EditorGUI.DisabledScope("m_Script" == iterator.propertyPath)) { FieldInfo fieldInfos = target.GetType().GetField(iterator.name); if (fieldInfos == null) { EditorGUILayout.PropertyField(iterator, true); continue; } //检查ShowIf特性 if (CheckShowIf(iterator, target)) { //准备style if (sStyles == null) { sStyles = new Styles(); } if (iterator.isArray && iterator.propertyType == SerializedPropertyType.Generic) { //绘制Array、List<> ListField(iterator, editor); } else { EditorGUILayout.PropertyField(iterator, true); } } } } serializedObject.ApplyModifiedProperties(); EditorGUI.EndChangeCheck(); }
//internal static float GetHeaderAttributeHeight(SerializedProperty serializedProperty) //{ // Object target = serializedProperty.serializedObject.targetObject; // FieldInfo fieldInfo = target.GetType().GetField(serializedProperty.name); // if (fieldInfo == null) // { // return 0; // } // Type tButtonExAttribute = typeof(HeaderAttribute); // if (Attribute.IsDefined(fieldInfo, tButtonExAttribute, true)) // { // return fieldInfo.GetCustomAttributes(tButtonExAttribute, true).Length * 24f; // } // return 0; //} private static void ListField(SerializedProperty iterator, MonoInspector editor) { editor.nListId++; int dragId = editor.nListId + editor.nObjectId; GUILayout.BeginHorizontal(); GUILayout.Space(10); EditorGUILayout.PropertyField(iterator, new GUIContent(string.Format("[{0}] {1}", iterator.arraySize, iterator.displayName)), false); GUILayout.EndHorizontal(); Rect position = GUILayoutUtility.GetLastRect(); position.x = 2; position.width = 10; float heightDelta = position.height - 16; if (heightDelta > 0) { position.height = 16; position.y += heightDelta; } //上移16高度绘制“+”、“清空”按钮 GUILayout.Space(-18); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayoutPressButton(sStyles.iconPlus, sStyles.preButton, GUILayout.Width(16)) == ButtonPressType.Mouse0) { //添加一个元素 iterator.isExpanded = true; iterator.InsertArrayElementAtIndex(iterator.arraySize); Event.current.type = EventType.Used; editor.Repaint(); } Color currColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayoutPressButton(sStyles.iconTrash, sStyles.preButton, GUILayout.Width(16)) == ButtonPressType.Mouse0) { //清空列表 iterator.isExpanded = true; iterator.ClearArray(); MonoInspectorDrag.EndDrag(false); Event.current.type = EventType.Used; editor.Repaint(); } GUI.backgroundColor = currColor; GUILayout.EndHorizontal(); //显示折叠 if (!iterator.isExpanded || iterator.serializedObject == null || iterator.arraySize == 0) { return; } //为拖拽准备一个IList IList iList = null; int minListLen = int.MaxValue; Type type = editor.target.GetType(); FieldInfo fInfo = type.GetField(iterator.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); while (fInfo == null && type != typeof(Object)) { type = type.BaseType; if (type == null) { break; } fInfo = type.GetField(iterator.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); } for (int index = 0; index < editor.targets.Length; index++) { Object o = editor.targets[index]; IList li = fInfo.GetValue(o) as IList; int l = li == null ? 0 : li.Count; if (l >= minListLen) { continue; } iList = li; minListLen = l; } //绘制列表元素 Undo.RecordObject(iterator.serializedObject.targetObject, iterator.serializedObject.targetObject.name); if (Event.current.type == EventType.DragPerform || Event.current.type == EventType.DragExited || Event.current.type == EventType.DragUpdated) { for (int i = 0; i < iterator.arraySize; i++) { SerializedProperty spArrayElement = iterator.GetArrayElementAtIndex(i); EditorGUILayout.PropertyField(spArrayElement, spArrayElement.hasChildren); // Property } return; } EditorGUILayout.BeginVertical(sStyles.list); for (int i = 0; i < iterator.arraySize; i++) { SerializedProperty spArrayElement = iterator.GetArrayElementAtIndex(i); EditorGUILayout.BeginHorizontal(); float currLabelW = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth -= 25; bool wasEnabled = GUI.enabled; if (editor.targets.Length > 1) { GUI.enabled = false; } //按方块开始拖拽 ButtonPressType bpt = GUILayoutPressButton(GUIContent.none, sStyles.listItem); if (bpt == ButtonPressType.Mouse1) { MonoInspectorDrag.StartDrag(dragId, editor, iList, i); } GUI.enabled = wasEnabled; if (spArrayElement.hasVisibleChildren) { GUILayout.Label(i.ToString(), sStyles.button); GUILayout.Space(10); } EditorGUILayout.PropertyField(spArrayElement, spArrayElement.hasChildren); // Property if (GUILayout.Button(sStyles.iconPlus, sStyles.preButton, GUILayout.Width(16))) { iterator.InsertArrayElementAtIndex(i); EditorGUILayout.EndHorizontal(); break; } if (GUILayout.Button(sStyles.iconMinus, sStyles.preButton, GUILayout.Width(16))) { if (spArrayElement.propertyType == SerializedPropertyType.ObjectReference && spArrayElement.objectReferenceValue != null) { iterator.DeleteArrayElementAtIndex(i); } iterator.DeleteArrayElementAtIndex(i); EditorGUILayout.EndHorizontal(); break; } EditorGUIUtility.labelWidth = currLabelW; EditorGUILayout.EndHorizontal(); if (MonoInspectorDrag.Drag(dragId, iList, i).outcome == DragResultType.Accepted) { GUI.changed = true; } } EditorGUILayout.EndHorizontal(); if (iterator.isExpanded) { GUILayout.Space(3); GUILayout.Box(string.Empty, sStyles.endBackground, GUILayout.ExpandWidth(true), GUILayout.Height(2)); } }