EditorTimer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. namespace TurnTheGameOn.Timer
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using System.Collections;
  7. using UnityEditor;
  8. [CustomEditor(typeof(Timer))]
  9. public class GameManagerEditor : Editor
  10. {
  11. [MenuItem("GameObject/UI/Timer")]
  12. static void Create()
  13. {
  14. GameObject canvasCheck = Selection.activeGameObject;
  15. if (canvasCheck == null) {
  16. Canvas findCanvas = GameObject.FindObjectOfType<Canvas> ();
  17. if (findCanvas != null) {
  18. canvasCheck = findCanvas.gameObject;
  19. } else {
  20. canvasCheck = new GameObject ("Canvas", typeof(Canvas));
  21. canvasCheck.GetComponent<Canvas> ().renderMode = RenderMode.ScreenSpaceOverlay;
  22. canvasCheck.AddComponent <GraphicRaycaster>();
  23. }
  24. GameObject instance = Instantiate (Resources.Load ("Timer", typeof(GameObject))) as GameObject;
  25. instance.transform.SetParent(canvasCheck.transform);
  26. instance.name = "Timer";
  27. instance.transform.localPosition = new Vector3(0,0,0);
  28. EditorUtility.FocusProjectWindow ();
  29. Selection.activeObject = instance;
  30. canvasCheck = null;
  31. canvasCheck = GameObject.Find ("Event System");
  32. if(canvasCheck == null)
  33. {
  34. canvasCheck = new GameObject ("Event System", typeof(EventSystem));
  35. canvasCheck.AddComponent<StandaloneInputModule> ();
  36. }
  37. canvasCheck = null;
  38. instance = null;
  39. }
  40. else
  41. {
  42. Canvas findCanvas = GameObject.FindObjectOfType<Canvas> ();
  43. if (findCanvas != null && canvasCheck.GetComponent<Canvas>() == null)
  44. {
  45. canvasCheck = findCanvas.gameObject;
  46. }
  47. GameObject instance = Instantiate (Resources.Load ("Timer", typeof(GameObject))) as GameObject;
  48. instance.name = "Timer";
  49. instance.transform.SetParent(canvasCheck.transform);
  50. instance.transform.localPosition = new Vector3(0,0,0);
  51. EditorUtility.FocusProjectWindow ();
  52. Selection.activeObject = instance;
  53. canvasCheck = null;
  54. canvasCheck = GameObject.Find ("Event System");
  55. if(canvasCheck == null)
  56. {
  57. canvasCheck = new GameObject ("Event System", typeof(EventSystem));
  58. canvasCheck.AddComponent<StandaloneInputModule> ();
  59. }
  60. canvasCheck = null;
  61. instance = null;
  62. }
  63. }
  64. public override void OnInspectorGUI()
  65. {
  66. GUI.enabled = false; EditorGUILayout.ObjectField("Script:", MonoScript.FromMonoBehaviour((Timer)target), typeof(Timer), false); GUI.enabled = true;
  67. serializedObject.Update();
  68. Timer myTarget = (Timer)target;
  69. EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
  70. EditorGUILayout.LabelField ("Timer Options", EditorStyles.boldLabel);
  71. EditorGUILayout.BeginVertical("Box");
  72. //Timer State
  73. SerializedProperty timerState = serializedObject.FindProperty ("timerState");
  74. EditorGUI.BeginChangeCheck();
  75. EditorGUILayout.PropertyField(timerState, true);
  76. if(EditorGUI.EndChangeCheck())
  77. serializedObject.ApplyModifiedProperties();
  78. EditorGUI.BeginChangeCheck();
  79. myTarget.timerType = (Timer.TimerType) EditorGUILayout.EnumPopup("Timer Type", myTarget.timerType);
  80. if(EditorGUI.EndChangeCheck())
  81. serializedObject.ApplyModifiedProperties();
  82. if (myTarget.timerType == Timer.TimerType.CountUp)
  83. {
  84. //Set Start Time
  85. SerializedProperty setStartTimeForCountUp = serializedObject.FindProperty ("setStartTimeForCountUp");
  86. EditorGUI.BeginChangeCheck();
  87. EditorGUILayout.PropertyField(setStartTimeForCountUp, true);
  88. if(EditorGUI.EndChangeCheck())
  89. serializedObject.ApplyModifiedProperties();
  90. }
  91. if (myTarget.timerType == Timer.TimerType.CountDown || (myTarget.timerType == Timer.TimerType.CountUp && myTarget.setStartTimeForCountUp) || (myTarget.timerType == Timer.TimerType.CountUpInfinite && myTarget.setStartTimeForCountUp))
  92. {
  93. myTarget.startTime = EditorGUILayout.FloatField("Start Time", myTarget.startTime);
  94. }
  95. if (myTarget.timerType == Timer.TimerType.CountUp)
  96. {
  97. myTarget.finishTime = EditorGUILayout.FloatField("Finish Time", myTarget.finishTime);
  98. }
  99. myTarget.timerSpeed = EditorGUILayout.Slider ("Timer Speed", myTarget.timerSpeed, 0, 100);
  100. if (myTarget.timerType == Timer.TimerType.CountUpInfinite)
  101. {
  102. //Use System Time
  103. SerializedProperty useSystemTime = serializedObject.FindProperty ("useSystemTime");
  104. EditorGUI.BeginChangeCheck();
  105. EditorGUILayout.PropertyField(useSystemTime, true);
  106. if(EditorGUI.EndChangeCheck())
  107. serializedObject.ApplyModifiedProperties();
  108. }
  109. EditorGUILayout.EndVertical ();
  110. if (myTarget.timerType != Timer.TimerType.CountUpInfinite)
  111. {
  112. EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
  113. EditorGUILayout.LabelField ("Time's Up Event Options", EditorStyles.boldLabel);
  114. EditorGUILayout.BeginVertical ("Box");
  115. //Set Zero Timescale
  116. SerializedProperty setZeroTimescale = serializedObject.FindProperty ("setZeroTimescale");
  117. EditorGUI.BeginChangeCheck();
  118. EditorGUILayout.PropertyField(setZeroTimescale, true);
  119. if(EditorGUI.EndChangeCheck())
  120. serializedObject.ApplyModifiedProperties();
  121. //Times Up Event
  122. SerializedProperty timesUpEvent = serializedObject.FindProperty ("timesUpEvent");
  123. EditorGUI.BeginChangeCheck();
  124. EditorGUILayout.PropertyField(timesUpEvent, true);
  125. if(EditorGUI.EndChangeCheck())
  126. serializedObject.ApplyModifiedProperties();
  127. //Destroy On Finish Array
  128. SerializedProperty destroyOnFinish = serializedObject.FindProperty ("destroyOnFinish");
  129. EditorGUI.BeginChangeCheck ();
  130. EditorGUILayout.PropertyField (destroyOnFinish, true);
  131. if (EditorGUI.EndChangeCheck ())
  132. serializedObject.ApplyModifiedProperties ();
  133. //Load Scenes Options
  134. myTarget.loadSceneOn = (Timer.LoadSceneOn)EditorGUILayout.EnumPopup ("Load Scene On", myTarget.loadSceneOn);
  135. if (myTarget.loadSceneOn == Timer.LoadSceneOn.SpecificTime)
  136. {
  137. myTarget.loadSceneName = EditorGUILayout.TextField ("Scene Name", myTarget.loadSceneName);
  138. myTarget.timeToLoadScene = EditorGUILayout.FloatField ("Time to Load Scene", myTarget.timeToLoadScene);
  139. }
  140. if (myTarget.loadSceneOn == Timer.LoadSceneOn.TimesUp)
  141. {
  142. myTarget.loadSceneName = EditorGUILayout.TextField ("Scene Name", myTarget.loadSceneName);
  143. }
  144. EditorGUILayout.EndVertical ();
  145. }
  146. EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
  147. EditorGUILayout.LabelField ("UI Text Output", EditorStyles.boldLabel);
  148. EditorGUILayout.BeginVertical("Box");
  149. // Timer Text
  150. SerializedProperty textType = serializedObject.FindProperty ("textType");
  151. EditorGUI.BeginChangeCheck();
  152. EditorGUILayout.PropertyField(textType, true);
  153. if(EditorGUI.EndChangeCheck())
  154. serializedObject.ApplyModifiedProperties();
  155. if (myTarget.textType == Timer.TextType.DefaultText)
  156. {
  157. SerializedProperty timerTextDefault = serializedObject.FindProperty ("timerTextDefault");
  158. EditorGUI.BeginChangeCheck();
  159. EditorGUILayout.PropertyField(timerTextDefault, true);
  160. if(EditorGUI.EndChangeCheck())
  161. serializedObject.ApplyModifiedProperties();
  162. }
  163. else if (myTarget.textType == Timer.TextType.TextMeshProUGUI)
  164. {
  165. SerializedProperty timerTextTMPUGUI = serializedObject.FindProperty ("timerTextTMPUGUI");
  166. EditorGUI.BeginChangeCheck();
  167. EditorGUILayout.PropertyField(timerTextTMPUGUI, true);
  168. if(EditorGUI.EndChangeCheck())
  169. serializedObject.ApplyModifiedProperties();
  170. }
  171. if (myTarget.timerType == Timer.TimerType.CountUpInfinite) {
  172. //Display Options
  173. SerializedProperty displayOptions = serializedObject.FindProperty ("displayOptions");
  174. EditorGUI.BeginChangeCheck();
  175. EditorGUILayout.PropertyField(displayOptions, true);
  176. if(EditorGUI.EndChangeCheck())
  177. serializedObject.ApplyModifiedProperties();
  178. }
  179. if (myTarget.timerType != Timer.TimerType.CountUpInfinite)
  180. {
  181. //Time Format
  182. SerializedProperty timerFormat = serializedObject.FindProperty ("timerFormat");
  183. EditorGUI.BeginChangeCheck();
  184. EditorGUILayout.PropertyField(timerFormat, true);
  185. if(EditorGUI.EndChangeCheck())
  186. serializedObject.ApplyModifiedProperties();
  187. //
  188. SerializedProperty leadingZero = serializedObject.FindProperty ("leadingZero");
  189. EditorGUI.BeginChangeCheck();
  190. EditorGUILayout.PropertyField(leadingZero, true);
  191. if(EditorGUI.EndChangeCheck())
  192. serializedObject.ApplyModifiedProperties();
  193. }
  194. // EditorGUILayout.HelpBox ("public timer variables for reference: " +
  195. // "\nmillisecond " + myTarget.millisecond.ToString () +
  196. // "\nsecond " + myTarget.second.ToString () +
  197. // "\nminute " + myTarget.minute.ToString () +
  198. // "\nhour " + myTarget.hour.ToString () +
  199. // "\nday " + myTarget.day.ToString ()
  200. // , MessageType.None);
  201. EditorGUILayout.EndVertical ();
  202. ///
  203. ///
  204. ///
  205. // Help Box
  206. EditorGUILayout.LabelField ("", GUI.skin.horizontalSlider);
  207. if (GUI.changed) EditorUtility.SetDirty (target);
  208. EditorGUILayout.HelpBox ("This component requires an event system, if you don't have one in your scene select 'Game Object/UI/Event System'.", MessageType.Info);
  209. if (GUI.changed) EditorUtility.SetDirty (target);
  210. }
  211. }
  212. }