CustomStateControllerEditor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Utility;
  5. namespace Mono.UI.UIComs
  6. {
  7. [CustomEditor(typeof(CustomStateController))]
  8. public class CustomStateControllerEditor : UnityEditor.Editor
  9. {
  10. private CustomStateController uIStateController;
  11. private void OnEnable()
  12. {
  13. uIStateController = (CustomStateController)target;
  14. }
  15. public int SelectIndex
  16. {
  17. get { return _selectIndex; }
  18. set
  19. {
  20. if (value != _selectIndex && value >= 0)
  21. {
  22. _selectIndex = value;
  23. uIStateController.CurrIndex = _selectIndex;
  24. uIStateController.ChangeState(_selectIndex);
  25. Debug.Log("切换状态+" + uIStateController.data[_selectIndex].StateInfos[0].UIStateV4Datas.Count);
  26. }
  27. }
  28. }
  29. private int _selectIndex;
  30. private Object ObjectPrefab;
  31. bool isInited = false;
  32. public override void OnInspectorGUI()
  33. {
  34. SerializedProperty AllComsProperty = serializedObject.FindProperty("AllComs");
  35. SerializedProperty AllComsStateTypesProperty = serializedObject.FindProperty("AllComsStateTypes");
  36. if (uIStateController.AllComsStateTypes.Count < uIStateController.AllComs.Count)
  37. {
  38. uIStateController.AllComsStateTypes.Add(CustomStateController.UIStateType.Null);
  39. }
  40. GUILayout.BeginHorizontal(GUI.skin.box);
  41. _selectIndex = uIStateController.CurrIndex;
  42. for (var i = 0; i < uIStateController.data.Count; i++)
  43. {
  44. if (GUILayout.Toggle(SelectIndex == i, i.ToString()))
  45. {
  46. SelectIndex = i;
  47. }
  48. }
  49. EditorGUILayout.EndHorizontal();
  50. EditorGUILayout.ObjectField(ObjectPrefab, typeof(Object), false);
  51. SerializedProperty property;
  52. EditorGUILayout.LabelField("需要保存状态数据的组件列表:");
  53. var delList = new List<int>();
  54. for (var i = 0; i < uIStateController.AllComs.Count; i++)
  55. {
  56. GUILayout.BeginHorizontal();
  57. property = AllComsProperty.GetArrayElementAtIndex(i);
  58. property.objectReferenceValue = EditorGUILayout.ObjectField(property.objectReferenceValue, typeof(Object), true);
  59. uIStateController.AllComsStateTypes[i] = (CustomStateController.UIStateType)EditorGUILayout.EnumFlagsField(uIStateController.AllComsStateTypes[i]);
  60. if (GUILayout.Button("X"))
  61. {
  62. //将元素添加进删除list
  63. delList.Add(i);
  64. }
  65. GUILayout.EndHorizontal();
  66. }
  67. //遍历删除list,将其删除掉
  68. foreach (var i in delList)
  69. {
  70. AllComsProperty.DeleteArrayElementAtIndex(i);
  71. AllComsStateTypesProperty.DeleteArrayElementAtIndex(i);
  72. }
  73. // EditorGUILayout.LabelField("当前保存的状态数据列表:");
  74. // for (var i = 0; i < uIStateController.data.Count; i++)
  75. // {
  76. // property = AllComDatasProperty.GetArrayElementAtIndex(i);
  77. // EditorGUILayout.PropertyField(property);
  78. // }
  79. EditorGUILayout.BeginHorizontal();
  80. if (GUILayout.Button("添加当前状态数据"))
  81. {
  82. uIStateController.AddData();
  83. SelectIndex = uIStateController.data.Count - 1;
  84. Debug.Log("添加当前状态数据");
  85. EditorUtility.SetDirty(uIStateController.gameObject);
  86. }
  87. if (GUILayout.Button("保存当前状态数据"))
  88. {
  89. uIStateController.SaveData();
  90. EditorUtility.SetDirty(uIStateController.gameObject);
  91. Debug.Log("保存当前状态数据");
  92. }
  93. if (GUILayout.Button("删除当前状态数据"))
  94. {
  95. uIStateController.RemoveData(SelectIndex);
  96. SelectIndex = uIStateController.data.Count - 1;
  97. EditorUtility.SetDirty(uIStateController.gameObject);
  98. Debug.Log("删除当前状态数据");
  99. }
  100. EditorGUILayout.EndHorizontal();
  101. var eventType = Event.current.type;
  102. //在Inspector 窗口上创建区域,向区域拖拽资源对象,获取到拖拽到区域的对象
  103. if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
  104. {
  105. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  106. if (eventType == EventType.DragPerform)
  107. {
  108. DragAndDrop.AcceptDrag();
  109. foreach (var o in DragAndDrop.objectReferences)
  110. {
  111. AddObject(AllComsProperty, o);
  112. }
  113. }
  114. Event.current.Use();
  115. }
  116. serializedObject.ApplyModifiedProperties();
  117. serializedObject.UpdateIfRequiredOrScript();
  118. }
  119. private void AddObject(SerializedProperty dataProperty, Object obj)
  120. {
  121. int index = dataProperty.arraySize;
  122. dataProperty.InsertArrayElementAtIndex(index);
  123. var element = dataProperty.GetArrayElementAtIndex(index);
  124. element.objectReferenceValue = obj;
  125. }
  126. private void AddStateData(CustomStateController.UIStateData data)
  127. {
  128. uIStateController.data.Add(data);
  129. }
  130. }
  131. }