TimedObjectActivator.cs 6.7 KB

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