PressableButtonInspector.cs 5.1 KB

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