TouchableButtonInspector.cs 6.8 KB

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