glados123123123123

Untitled

Sep 13th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 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. SerializedProperty nestedCommand = nestedCommands.GetArrayElementAtIndex(j);
  76. SerializedProperty nestedCommandType = nestedCommand.FindPropertyRelative("commandType");
  77. SerializedProperty nestedCommandArguments = nestedCommand.FindPropertyRelative("commandArguments");
  78.  
  79. rect.y += EditorGUIUtility.singleLineHeight * 2;
  80. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  81. nestedCommandType, GUIContent.none);
  82.  
  83. CommandType nestedCommandTypeValue = (CommandType)nestedCommandType.enumValueIndex;
  84.  
  85. switch (nestedCommandTypeValue)
  86. {
  87. case CommandType.Teleport:
  88. if (nestedCommandArguments.arraySize < 1)
  89. {
  90. nestedCommandArguments.arraySize = 1;
  91. }
  92. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  93. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  94. break;
  95. case CommandType.Write:
  96. if (nestedCommandArguments.arraySize < 2)
  97. {
  98. nestedCommandArguments.arraySize = 2;
  99. }
  100. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  101. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Write Speed"));
  102. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight * 2, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  103. nestedCommandArguments.GetArrayElementAtIndex(1), new GUIContent("Content"));
  104. break;
  105. case CommandType.Move:
  106. if (nestedCommandArguments.arraySize < 1)
  107. {
  108. nestedCommandArguments.arraySize = 1;
  109. }
  110. EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight),
  111. nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target"));
  112. break;
  113. }
  114. }
  115.  
  116. if (GUI.Button(new Rect(rect.x + rect.width - 60, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "+"))
  117. {
  118. nestedCommands.arraySize++;
  119. }
  120.  
  121. if (nestedCommands.arraySize > 0 && GUI.Button(new Rect(rect.x + rect.width - 30, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "-"))
  122. {
  123. nestedCommands.arraySize--;
  124. }
  125. break;
  126. }
  127. };
  128. }
  129.  
  130. public override void OnInspectorGUI()
  131. {
  132. serializedObject.Update();
  133. commandList.DoLayoutList();
  134. serializedObject.ApplyModifiedProperties();
  135. }
  136.  
  137. float GetElementHeight(CommandType commandType)
  138. {
  139. float height = 0;
  140. switch (commandType)
  141. {
  142. case CommandType.Teleport:
  143. height = 1;
  144. break;
  145. case CommandType.Write:
  146. height = 2;
  147. break;
  148. case CommandType.Move:
  149. height = 1;
  150. break;
  151. case CommandType.SendEvent:
  152. height = 6;
  153. break;
  154. }
  155. if (commandType == CommandType.SendEvent)
  156. {
  157. height += 1;
  158. }
  159. return height * EditorGUIUtility.singleLineHeight + 2;
  160. }
  161.  
  162. }
  163.  
Add Comment
Please, Sign In to add comment