暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventTriggerEditor.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. namespace UnityEditor.EventSystems
  5. {
  6. [CustomEditor(typeof(EventTrigger), true)]
  7. public class EventTriggerEditor : Editor
  8. {
  9. SerializedProperty m_DelegatesProperty;
  10. GUIContent m_IconToolbarMinus;
  11. GUIContent m_EventIDName;
  12. GUIContent[] m_EventTypes;
  13. GUIContent m_AddButonContent;
  14. protected virtual void OnEnable()
  15. {
  16. m_DelegatesProperty = serializedObject.FindProperty("m_Delegates");
  17. m_AddButonContent = EditorGUIUtility.TrTextContent("Add New Event Type");
  18. m_EventIDName = new GUIContent("");
  19. // Have to create a copy since otherwise the tooltip will be overwritten.
  20. m_IconToolbarMinus = new GUIContent(EditorGUIUtility.IconContent("Toolbar Minus"));
  21. m_IconToolbarMinus.tooltip = "Remove all events in this list.";
  22. string[] eventNames = Enum.GetNames(typeof(EventTriggerType));
  23. m_EventTypes = new GUIContent[eventNames.Length];
  24. for (int i = 0; i < eventNames.Length; ++i)
  25. {
  26. m_EventTypes[i] = new GUIContent(eventNames[i]);
  27. }
  28. }
  29. public override void OnInspectorGUI()
  30. {
  31. serializedObject.Update();
  32. int toBeRemovedEntry = -1;
  33. EditorGUILayout.Space();
  34. Vector2 removeButtonSize = GUIStyle.none.CalcSize(m_IconToolbarMinus);
  35. for (int i = 0; i < m_DelegatesProperty.arraySize; ++i)
  36. {
  37. SerializedProperty delegateProperty = m_DelegatesProperty.GetArrayElementAtIndex(i);
  38. SerializedProperty eventProperty = delegateProperty.FindPropertyRelative("eventID");
  39. SerializedProperty callbacksProperty = delegateProperty.FindPropertyRelative("callback");
  40. m_EventIDName.text = eventProperty.enumDisplayNames[eventProperty.enumValueIndex];
  41. EditorGUILayout.PropertyField(callbacksProperty, m_EventIDName);
  42. Rect callbackRect = GUILayoutUtility.GetLastRect();
  43. Rect removeButtonPos = new Rect(callbackRect.xMax - removeButtonSize.x - 8, callbackRect.y + 1, removeButtonSize.x, removeButtonSize.y);
  44. if (GUI.Button(removeButtonPos, m_IconToolbarMinus, GUIStyle.none))
  45. {
  46. toBeRemovedEntry = i;
  47. }
  48. EditorGUILayout.Space();
  49. }
  50. if (toBeRemovedEntry > -1)
  51. {
  52. RemoveEntry(toBeRemovedEntry);
  53. }
  54. Rect btPosition = GUILayoutUtility.GetRect(m_AddButonContent, GUI.skin.button);
  55. const float addButonWidth = 200f;
  56. btPosition.x = btPosition.x + (btPosition.width - addButonWidth) / 2;
  57. btPosition.width = addButonWidth;
  58. if (GUI.Button(btPosition, m_AddButonContent))
  59. {
  60. ShowAddTriggermenu();
  61. }
  62. serializedObject.ApplyModifiedProperties();
  63. }
  64. private void RemoveEntry(int toBeRemovedEntry)
  65. {
  66. m_DelegatesProperty.DeleteArrayElementAtIndex(toBeRemovedEntry);
  67. }
  68. void ShowAddTriggermenu()
  69. {
  70. // Now create the menu, add items and show it
  71. GenericMenu menu = new GenericMenu();
  72. for (int i = 0; i < m_EventTypes.Length; ++i)
  73. {
  74. bool active = true;
  75. // Check if we already have a Entry for the current eventType, if so, disable it
  76. for (int p = 0; p < m_DelegatesProperty.arraySize; ++p)
  77. {
  78. SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(p);
  79. SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
  80. if (eventProperty.enumValueIndex == i)
  81. {
  82. active = false;
  83. }
  84. }
  85. if (active)
  86. menu.AddItem(m_EventTypes[i], false, OnAddNewSelected, i);
  87. else
  88. menu.AddDisabledItem(m_EventTypes[i]);
  89. }
  90. menu.ShowAsContext();
  91. Event.current.Use();
  92. }
  93. private void OnAddNewSelected(object index)
  94. {
  95. int selected = (int)index;
  96. m_DelegatesProperty.arraySize += 1;
  97. SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(m_DelegatesProperty.arraySize - 1);
  98. SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
  99. eventProperty.enumValueIndex = selected;
  100. serializedObject.ApplyModifiedProperties();
  101. }
  102. }
  103. }