123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using TFramework;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEditorInternal;
- using System;
- [CustomEditor(typeof(Main))]
- public class MianInspector:TEditor
- {
- private SerializedProperty _doNotDestroy;
- private SerializedProperty _ManagerList;
- private ReorderableList _managerReorderable;
- private GUIContent dndGC = new GUIContent("跳场景不销毁");
- private GUIContent mListGC = new GUIContent("管理器注册表");
- List<BaseManager> monos = new List<BaseManager>();
- List<Type> magTypes;
- private void OnEnable()
- {
- _doNotDestroy = serializedObject.FindProperty("doNotDestroy");
- _ManagerList = serializedObject.FindProperty("m_ManagerList");
- _managerReorderable = new ReorderableList(serializedObject, _ManagerList);
- magTypes = GlobalTool.GetTypesInRunTimeAssemblies(type =>
- {
- return (type.IsSubclassOf(typeof(BaseManager)) ||
- (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(BaseManager)));
- });
- monos.AddRange(FindObjectsOfType<BaseManager>());
- GenericMenu menu = new GenericMenu();
-
- _managerReorderable.onAddCallback = (list) =>
- {
- magTypes.ForEach(p =>
- {
- menu.AddItem(new GUIContent(p.Name), (target as Main).m_ManagerList.Find(m => m.GetType() == p) != null, () =>
- {
- if ((target as Main).m_ManagerList.Find(m => m.GetType() == p) != null) return;
- serializedObject.Update();
- BaseManager magr = monos.Find(m => m.GetType() == p);
- if (magr == null)
- {
- magr = FindObjectsOfType<BaseManager>().Find(b => b.GetType() == p);
- if (magr == null)
- {
- magr = (target as Main).gameObject.AddComponent(p) as BaseManager;
- }
- }
- list.serializedProperty.arraySize++;
- list.index = list.serializedProperty.arraySize - 1;
- SerializedProperty marg = list.serializedProperty.GetArrayElementAtIndex(list.index);
- marg.objectReferenceValue = magr;
- serializedObject.ApplyModifiedProperties();
- });
- });
- menu.ShowAsContext();
- };
- _managerReorderable.drawHeaderCallback = rect =>
- {
- GUI.Label(rect, mListGC);
- };
- _managerReorderable.drawElementCallback = (rect, index, isActive, isFocused) =>
- {
- UnityEngine.Object magr = EditorGUI.ObjectField(rect, _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue, typeof(BaseManager), true);
- if(magr!= _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue)
- {
- if ((target as Main).m_ManagerList.Contains(magr as BaseManager)) return;
- _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue = magr;
- }
- //EditorGUI.PropertyField(rect, _ManagerList.GetArrayElementAtIndex(index));
- };
- }
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- EditorGUILayout.PropertyField(_doNotDestroy,dndGC);
- _managerReorderable.DoLayoutList();
- serializedObject.ApplyModifiedProperties();
- }
- }
|