glados123123123123

Untitled

Sep 13th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. using System;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5.  
  6. [CustomEditor(typeof(EventTrigger))]
  7. public class EventTriggerEditor : Editor
  8. {
  9. ReorderableList commandList;
  10.  
  11. void OnEnable()
  12. {
  13. commandList = new ReorderableList(serializedObject,
  14. serializedObject.FindProperty("commands"),
  15. true, true, true, true);
  16.  
  17. commandList.elementHeightCallback = (index) =>
  18. {
  19. var element = commandList.serializedProperty.GetArrayElementAtIndex(index);
  20. return GetElementHeight((CommandType)element.FindPropertyRelative("commandType").enumValueIndex);
  21. };
  22.  
  23. commandList.drawElementCallback = (rect, index, isActive, isFocused) =>
  24. {
  25. var element = commandList.serializedProperty.GetArrayElementAtIndex(index);
  26. rect.y += 2;
  27. EditorGUI.PropertyField(new Rect(rect.x, rect.y, 60, EditorGUIUtility.singleLineHeight),
  28. element.FindPropertyRelative("commandType"), GUIContent.none);
  29.  
  30. CommandType commandType = (CommandType)element.FindPropertyRelative("commandType").enumValueIndex;
  31. SerializedProperty commandArguments = element.FindPropertyRelative("commandArguments");
  32.  
  33. switch (commandType)
  34. {
  35. case CommandType.Teleport:
  36. if (commandArguments.arraySize < 1)
  37. {
  38. commandArguments.arraySize = 1;
  39. }
  40. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  41. commandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  42. break;
  43. case CommandType.Write:
  44. if (commandArguments.arraySize < 2)
  45. {
  46. commandArguments.arraySize = 2;
  47. }
  48. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  49. commandArguments.GetArrayElementAtIndex(0), new GUIContent("Write Speed"));
  50. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  51. commandArguments.GetArrayElementAtIndex(1), new GUIContent("Content"));
  52. break;
  53. case CommandType.Move:
  54. if (commandArguments.arraySize < 1)
  55. {
  56. commandArguments.arraySize = 1;
  57. }
  58. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  59. commandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  60. break;
  61. case CommandType.SendEvent:
  62. if (commandArguments.arraySize < 3)
  63. {
  64. commandArguments.arraySize = 3;
  65. }
  66. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  67. commandArguments.GetArrayElementAtIndex(0), new GUIContent("TargetID"));
  68. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  69. commandArguments.GetArrayElementAtIndex(1), new GUIContent("Start Delay"));
  70.  
  71. SerializedProperty nestedCommands = element.FindPropertyRelative("nestedCommands");
  72.  
  73. for (int j = 0; j < nestedCommands.arraySize; j++)
  74. {
  75. commandList.elementHeightCallback = (index) =>
  76. {
  77. var nestedElement = commandList.serializedProperty.GetArrayElementAtIndex(index);
  78. return GetElementHeight((CommandType)nestedElement.FindPropertyRelative("commandType").enumValueIndex);
  79. };
  80.  
  81. var nestedElement = commandList.serializedProperty.GetArrayElementAtIndex(index);
  82.  
  83. CommandType nestedCommandType = (CommandType)nestedElement.FindPropertyRelative("commandType").enumValueIndex;
  84. SerializedProperty nestedCommandArguments = nestedElement.FindPropertyRelative("commandArguments");
  85.  
  86. switch (nestedCommandType)
  87. {
  88. case CommandType.Teleport:
  89. if (nestedCommandArguments.arraySize < 1)
  90. {
  91. nestedCommandArguments.arraySize = 1;
  92. }
  93. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  94. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  95. break;
  96. case CommandType.Write:
  97. if (nestedCommandArguments.arraySize < 2)
  98. {
  99. nestedCommandArguments.arraySize = 2;
  100. }
  101. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  102. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Write Speed"));
  103. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  104. nestedCommandArguments.GetArrayElementAtIndex(1), new GUIContent("Content"));
  105. break;
  106. case CommandType.Move:
  107. if (nestedCommandArguments.arraySize < 1)
  108. {
  109. nestedCommandArguments.arraySize = 1;
  110. }
  111. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  112. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  113. break;
  114. }
  115. }
  116.  
  117. if (GUI.Button(new Rect(rect.x + rect.width - 60, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "+"))
  118. {
  119. nestedCommands.arraySize++;
  120. }
  121.  
  122. if (nestedCommands.arraySize > 0 && GUI.Button(new Rect(rect.x + rect.width - 30, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "-"))
  123. {
  124. nestedCommands.arraySize--;
  125. }
  126. break;
  127. }
  128. };
  129. }
  130.  
  131. public override void OnInspectorGUI()
  132. {
  133. serializedObject.Update();
  134. commandList.DoLayoutList();
  135. serializedObject.ApplyModifiedProperties();
  136. }
  137.  
  138. float GetElementHeight(CommandType commandType)
  139. {
  140. float height = 0;
  141. switch (commandType)
  142. {
  143. case CommandType.Teleport:
  144. height = 1;
  145. break;
  146. case CommandType.Write:
  147. height = 2;
  148. break;
  149. case CommandType.Move:
  150. height = 1;
  151. break;
  152. case CommandType.SendEvent:
  153. height = 6;
  154. break;
  155. }
  156. if (commandType == CommandType.SendEvent)
  157. {
  158. height += 1;
  159. }
  160. return height * EditorGUIUtility.singleLineHeight + 2;
  161. }
  162.  
  163. }
  164.  
Add Comment
Please, Sign In to add comment