Unity 框架

MianInspector.cs 3.7KB

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