Unity 框架

MianInspector.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using TFramework;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using System;
  8. [CustomEditor(typeof(Main))]
  9. public class MianInspector:TEditor
  10. {
  11. private SerializedProperty _doNotDestroy;
  12. private SerializedProperty _ManagerList;
  13. private ReorderableList _managerReorderable;
  14. private GUIContent dndGC = new GUIContent("跳场景不销毁");
  15. private GUIContent mListGC = new GUIContent("管理器注册表");
  16. List<BaseManager> monos = new List<BaseManager>();
  17. List<Type> magTypes;
  18. private void OnEnable()
  19. {
  20. _doNotDestroy = serializedObject.FindProperty("doNotDestroy");
  21. _ManagerList = serializedObject.FindProperty("m_ManagerList");
  22. _managerReorderable = new ReorderableList(serializedObject, _ManagerList);
  23. magTypes = GlobalTool.GetTypesInRunTimeAssemblies(type =>
  24. {
  25. return (type.IsSubclassOf(typeof(BaseManager)) ||
  26. (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(BaseManager)));
  27. });
  28. monos.AddRange(FindObjectsOfType<BaseManager>());
  29. GenericMenu menu = new GenericMenu();
  30. _managerReorderable.onAddCallback = (list) =>
  31. {
  32. magTypes.ForEach(p =>
  33. {
  34. menu.AddItem(new GUIContent(p.Name), (target as Main).m_ManagerList.Find(m => m.GetType() == p) != null, () =>
  35. {
  36. if ((target as Main).m_ManagerList.Find(m => m.GetType() == p) != null) return;
  37. serializedObject.Update();
  38. BaseManager magr = monos.Find(m => m.GetType() == p);
  39. if (magr == null)
  40. {
  41. magr = FindObjectsOfType<BaseManager>().Find(b => b.GetType() == p);
  42. if (magr == null)
  43. {
  44. magr = (target as Main).gameObject.AddComponent(p) as BaseManager;
  45. }
  46. }
  47. list.serializedProperty.arraySize++;
  48. list.index = list.serializedProperty.arraySize - 1;
  49. SerializedProperty marg = list.serializedProperty.GetArrayElementAtIndex(list.index);
  50. marg.objectReferenceValue = magr;
  51. serializedObject.ApplyModifiedProperties();
  52. });
  53. });
  54. menu.ShowAsContext();
  55. };
  56. _managerReorderable.drawHeaderCallback = rect =>
  57. {
  58. GUI.Label(rect, mListGC);
  59. };
  60. _managerReorderable.drawElementCallback = (rect, index, isActive, isFocused) =>
  61. {
  62. UnityEngine.Object magr = EditorGUI.ObjectField(rect, _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue, typeof(BaseManager), true);
  63. if(magr!= _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue)
  64. {
  65. if ((target as Main).m_ManagerList.Contains(magr as BaseManager)) return;
  66. _ManagerList.GetArrayElementAtIndex(index).objectReferenceValue = magr;
  67. }
  68. //EditorGUI.PropertyField(rect, _ManagerList.GetArrayElementAtIndex(index));
  69. };
  70. }
  71. public override void OnInspectorGUI()
  72. {
  73. serializedObject.Update();
  74. EditorGUILayout.PropertyField(_doNotDestroy,dndGC);
  75. _managerReorderable.DoLayoutList();
  76. serializedObject.ApplyModifiedProperties();
  77. }
  78. }