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.

215 lines
6.6 KiB

  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace UnityStandardAssets.Utility
  8. {
  9. public class TimedObjectActivator : MonoBehaviour
  10. {
  11. public enum Action
  12. {
  13. Activate,
  14. Deactivate,
  15. Destroy,
  16. ReloadLevel,
  17. Call,
  18. }
  19. [Serializable]
  20. public class Entry
  21. {
  22. public GameObject target;
  23. public Action action;
  24. public float delay;
  25. }
  26. [Serializable]
  27. public class Entries
  28. {
  29. public Entry[] entries;
  30. }
  31. public Entries entries = new Entries();
  32. private void Awake()
  33. {
  34. foreach (Entry entry in entries.entries)
  35. {
  36. switch (entry.action)
  37. {
  38. case Action.Activate:
  39. StartCoroutine(Activate(entry));
  40. break;
  41. case Action.Deactivate:
  42. StartCoroutine(Deactivate(entry));
  43. break;
  44. case Action.Destroy:
  45. Destroy(entry.target, entry.delay);
  46. break;
  47. case Action.ReloadLevel:
  48. StartCoroutine(ReloadLevel(entry));
  49. break;
  50. }
  51. }
  52. }
  53. private IEnumerator Activate(Entry entry)
  54. {
  55. yield return new WaitForSeconds(entry.delay);
  56. entry.target.SetActive(true);
  57. }
  58. private IEnumerator Deactivate(Entry entry)
  59. {
  60. yield return new WaitForSeconds(entry.delay);
  61. entry.target.SetActive(false);
  62. }
  63. private IEnumerator ReloadLevel(Entry entry)
  64. {
  65. yield return new WaitForSeconds(entry.delay);
  66. Application.LoadLevel(Application.loadedLevel);
  67. }
  68. }
  69. }
  70. namespace UnityStandardAssets.Utility.Inspector
  71. {
  72. #if UNITY_EDITOR
  73. [CustomPropertyDrawer(typeof (TimedObjectActivator.Entries))]
  74. public class EntriesDrawer : PropertyDrawer
  75. {
  76. private const float k_LineHeight = 18;
  77. private const float k_Spacing = 4;
  78. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  79. {
  80. EditorGUI.BeginProperty(position, label, property);
  81. float x = position.x;
  82. float y = position.y;
  83. float width = position.width;
  84. // Draw label
  85. EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  86. // Don't make child fields be indented
  87. var indent = EditorGUI.indentLevel;
  88. EditorGUI.indentLevel = 0;
  89. var entries = property.FindPropertyRelative("entries");
  90. if (entries.arraySize > 0)
  91. {
  92. float actionWidth = .25f*width;
  93. float targetWidth = .6f*width;
  94. float delayWidth = .1f*width;
  95. float buttonWidth = .05f*width;
  96. for (int i = 0; i < entries.arraySize; ++i)
  97. {
  98. y += k_LineHeight + k_Spacing;
  99. var entry = entries.GetArrayElementAtIndex(i);
  100. float rowX = x;
  101. // Calculate rects
  102. Rect actionRect = new Rect(rowX, y, actionWidth, k_LineHeight);
  103. rowX += actionWidth;
  104. Rect targetRect = new Rect(rowX, y, targetWidth, k_LineHeight);
  105. rowX += targetWidth;
  106. Rect delayRect = new Rect(rowX, y, delayWidth, k_LineHeight);
  107. rowX += delayWidth;
  108. Rect buttonRect = new Rect(rowX, y, buttonWidth, k_LineHeight);
  109. rowX += buttonWidth;
  110. // Draw fields - passs GUIContent.none to each so they are drawn without labels
  111. if (entry.FindPropertyRelative("action").enumValueIndex !=
  112. (int) TimedObjectActivator.Action.ReloadLevel)
  113. {
  114. EditorGUI.PropertyField(actionRect, entry.FindPropertyRelative("action"), GUIContent.none);
  115. EditorGUI.PropertyField(targetRect, entry.FindPropertyRelative("target"), GUIContent.none);
  116. }
  117. else
  118. {
  119. actionRect.width = actionRect.width + targetRect.width;
  120. EditorGUI.PropertyField(actionRect, entry.FindPropertyRelative("action"), GUIContent.none);
  121. }
  122. EditorGUI.PropertyField(delayRect, entry.FindPropertyRelative("delay"), GUIContent.none);
  123. if (GUI.Button(buttonRect, "-"))
  124. {
  125. entries.DeleteArrayElementAtIndex(i);
  126. break;
  127. }
  128. }
  129. }
  130. // add & sort buttons
  131. y += k_LineHeight + k_Spacing;
  132. var addButtonRect = new Rect(position.x + position.width - 120, y, 60, k_LineHeight);
  133. if (GUI.Button(addButtonRect, "Add"))
  134. {
  135. entries.InsertArrayElementAtIndex(entries.arraySize);
  136. }
  137. var sortButtonRect = new Rect(position.x + position.width - 60, y, 60, k_LineHeight);
  138. if (GUI.Button(sortButtonRect, "Sort"))
  139. {
  140. bool changed = true;
  141. while (entries.arraySize > 1 && changed)
  142. {
  143. changed = false;
  144. for (int i = 0; i < entries.arraySize - 1; ++i)
  145. {
  146. var e1 = entries.GetArrayElementAtIndex(i);
  147. var e2 = entries.GetArrayElementAtIndex(i + 1);
  148. if (e1.FindPropertyRelative("delay").floatValue > e2.FindPropertyRelative("delay").floatValue)
  149. {
  150. entries.MoveArrayElement(i + 1, i);
  151. changed = true;
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. // Set indent back to what it was
  158. EditorGUI.indentLevel = indent;
  159. //
  160. EditorGUI.EndProperty();
  161. }
  162. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  163. {
  164. SerializedProperty entries = property.FindPropertyRelative("entries");
  165. float lineAndSpace = k_LineHeight + k_Spacing;
  166. return 40 + (entries.arraySize*lineAndSpace) + lineAndSpace;
  167. }
  168. }
  169. #endif
  170. }