RepeatCall.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. namespace repeatCallUtility
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. public class RepeatCallData
  11. {
  12. //public int DelayFrame;
  13. public int DelayFramer;
  14. //public float DelayTime;
  15. public float DelayTimer;
  16. public int RepeatFrame = -1;
  17. public int RepeatFramer;
  18. public float RepeatTime = -1;
  19. public float RepeatTimer;
  20. public Action Action;
  21. public float InvokeCount = Mathf.Infinity;
  22. public float InvokeCounter = Mathf.Infinity;
  23. //public float InvokeFrame = Mathf.Infinity;
  24. //public float InvokeFramer = Mathf.Infinity;
  25. //public float InvokeTime = Mathf.Infinity;
  26. //public float InvokeTimer = Mathf.Infinity;
  27. }
  28. public class RepeatCall : MonoBehaviour
  29. {
  30. #region Config
  31. public static RepeatCall Instance
  32. {
  33. get
  34. {
  35. if (instance == null)
  36. {
  37. GameObject go = new GameObject("RepeatCall");
  38. instance = go.AddComponent<RepeatCall>();
  39. DontDestroyOnLoad(go);
  40. }
  41. return instance;
  42. }
  43. set { instance = value; }
  44. }
  45. private static RepeatCall instance;
  46. private static float DeltaTime
  47. {
  48. get
  49. {
  50. if (Application.isPlaying)
  51. {
  52. return Time.deltaTime;
  53. }
  54. else //�༭��ģʽ��ʹ��
  55. {
  56. #if UNITY_EDITOR
  57. double deltaTime = EditorApplication.timeSinceStartup - LastTimeSinceStartup;
  58. LastTimeSinceStartup = EditorApplication.timeSinceStartup;
  59. return (float) deltaTime;
  60. #else
  61. throw new Exception();
  62. #endif
  63. }
  64. }
  65. }
  66. private static double LastTimeSinceStartup;
  67. private static Dictionary<Coroutine, RepeatCallData> RepeatCallDataDictionary = new Dictionary<Coroutine, RepeatCallData>();
  68. #if UNITY_EDITOR
  69. //�༭��ģʽ��ʹ��
  70. private static Dictionary<IEnumerator, EditorApplication.CallbackFunction> CallbackFunctionDictionary = new Dictionary<IEnumerator, EditorApplication.CallbackFunction>();
  71. private static Dictionary<IEnumerator, RepeatCallData> RepeatCallDataDictionaryForEditor = new Dictionary<IEnumerator, RepeatCallData>();
  72. #endif
  73. #endregion
  74. public static object Call(RepeatCallData repeatCallData)
  75. {
  76. //repeatCallData.DelayFramer = repeatCallData.DelayFrame;
  77. //repeatCallData.DelayTimer = repeatCallData.DelayTime;
  78. repeatCallData.RepeatFramer = repeatCallData.RepeatFrame;
  79. repeatCallData.RepeatTimer = repeatCallData.RepeatTime;
  80. repeatCallData.InvokeCounter = repeatCallData.InvokeCount;
  81. //repeatCallData.InvokeFramer = repeatCallData.InvokeFrame;
  82. //repeatCallData.InvokeTimer = repeatCallData.InvokeTime;
  83. if (Application.isPlaying)
  84. {
  85. Coroutine coroutine = Instance.StartCoroutine(repeatCall(repeatCallData));
  86. if (coroutine != null)
  87. {
  88. RepeatCallDataDictionary.Add(coroutine, repeatCallData);
  89. }
  90. return coroutine;
  91. }
  92. else
  93. {
  94. #if UNITY_EDITOR
  95. IEnumerator enumerator = repeatCall(repeatCallData);
  96. RepeatCallDataDictionaryForEditor.Add(enumerator, repeatCallData);
  97. EditorApplication.CallbackFunction callbackFunction = () => UpdateEnumerators(enumerator);
  98. CallbackFunctionDictionary.Add(enumerator, callbackFunction);
  99. callbackFunction.Invoke();
  100. EditorApplication.update += callbackFunction;
  101. LastTimeSinceStartup = EditorApplication.timeSinceStartup;
  102. return enumerator;
  103. #else
  104. throw new Exception();
  105. #endif
  106. }
  107. }
  108. public static void PauseRepeatCall(object obj)
  109. {
  110. if (Application.isPlaying)
  111. {
  112. Instance.StopCoroutine((Coroutine) obj);
  113. }
  114. else
  115. {
  116. #if UNITY_EDITOR
  117. EditorApplication.CallbackFunction callbackFunction = CallbackFunctionDictionary[(IEnumerator) obj];
  118. EditorApplication.update -= callbackFunction;
  119. #endif
  120. }
  121. }
  122. public static void ResumeRepeatCall(object obj)
  123. {
  124. if (Application.isPlaying)
  125. {
  126. RepeatCallData repeatCallData = RepeatCallDataDictionary[(Coroutine) obj];
  127. Call(repeatCallData);
  128. }
  129. else
  130. {
  131. #if UNITY_EDITOR
  132. RepeatCallData repeatCallData = RepeatCallDataDictionaryForEditor[(IEnumerator) obj];
  133. Call(repeatCallData);
  134. #endif
  135. }
  136. }
  137. private static void UpdateEnumerators(IEnumerator enumerator) //�༭��ģʽ��ʹ��
  138. {
  139. enumerator.MoveNext();
  140. }
  141. private static IEnumerator repeatCall(RepeatCallData repeatCallData)
  142. {
  143. //Debug.Log(repeatCallData.DelayFramer + " " + repeatCallData.DelayTimer);
  144. if (repeatCallData.DelayFramer > 0)
  145. {
  146. while (repeatCallData.DelayFramer > 0)
  147. {
  148. repeatCallData.DelayFramer--;
  149. yield return null;
  150. }
  151. if (InvokeRepeatCall(repeatCallData))
  152. {
  153. yield break;
  154. }
  155. }
  156. else if (repeatCallData.DelayTimer > 0)
  157. {
  158. yield return null; //����StartCoroutine��������ִ�е���һ��yield
  159. while (repeatCallData.DelayTimer > 0)
  160. {
  161. repeatCallData.DelayTimer -= DeltaTime;
  162. yield return null;
  163. }
  164. if (InvokeRepeatCall(repeatCallData))
  165. {
  166. yield break;
  167. }
  168. }
  169. else
  170. {
  171. if (InvokeRepeatCall(repeatCallData))
  172. {
  173. yield break;
  174. }
  175. else
  176. {
  177. yield return null;
  178. }
  179. if (repeatCallData.RepeatFramer > -1)
  180. {
  181. while (repeatCallData.RepeatFramer > 0)
  182. {
  183. repeatCallData.RepeatFramer--;
  184. yield return null;
  185. }
  186. repeatCallData.RepeatFramer = repeatCallData.RepeatFrame;
  187. if (InvokeRepeatCall(repeatCallData))
  188. {
  189. yield break;
  190. }
  191. }
  192. else if (repeatCallData.RepeatTimer > -1)
  193. {
  194. while (repeatCallData.RepeatTimer > 0)
  195. {
  196. repeatCallData.RepeatTimer -= DeltaTime;
  197. yield return null;
  198. }
  199. repeatCallData.RepeatTimer = repeatCallData.RepeatTime;
  200. if (InvokeRepeatCall(repeatCallData))
  201. {
  202. yield break;
  203. }
  204. }
  205. else
  206. {
  207. throw new Exception();
  208. }
  209. }
  210. }
  211. private static bool InvokeRepeatCall(RepeatCallData repeatCallData)
  212. {
  213. repeatCallData.Action.Invoke();
  214. if (--repeatCallData.InvokeCounter <= 0)
  215. {
  216. return true;
  217. }
  218. else
  219. {
  220. return false;
  221. }
  222. }
  223. }
  224. public class DelayCall
  225. {
  226. public static object Call(int delayFrame, Action action)
  227. {
  228. RepeatCallData repeatCallData = new RepeatCallData();
  229. repeatCallData.DelayFramer = delayFrame;
  230. repeatCallData.InvokeCount = 1;
  231. repeatCallData.Action = action;
  232. return RepeatCall.Call(repeatCallData);
  233. }
  234. public static object Call(float delayTime, Action action)
  235. {
  236. RepeatCallData repeatCallData = new RepeatCallData();
  237. repeatCallData.DelayTimer = delayTime;
  238. repeatCallData.InvokeCount = 1;
  239. repeatCallData.Action = action;
  240. return RepeatCall.Call(repeatCallData);
  241. }
  242. }
  243. public class WhileCall : MonoBehaviour
  244. {
  245. public static WhileCall Instance
  246. {
  247. get
  248. {
  249. if (instance == null)
  250. {
  251. GameObject go = new GameObject("WhileCall");
  252. instance = go.AddComponent<WhileCall>();
  253. DontDestroyOnLoad(go);
  254. }
  255. return instance;
  256. }
  257. set { instance = value; }
  258. }
  259. private static WhileCall instance;
  260. public static void Call(Action action, Func<bool> func)
  261. {
  262. Instance.StartCoroutine(whileCall(action, func));
  263. }
  264. private static IEnumerator whileCall(Action action, Func<bool> func)
  265. {
  266. while (!func.Invoke())
  267. {
  268. yield return null;
  269. }
  270. action.Invoke();
  271. }
  272. }
  273. public class EndOfFrameCall : MonoBehaviour
  274. {
  275. public static EndOfFrameCall Instance
  276. {
  277. get
  278. {
  279. if (instance == null)
  280. {
  281. GameObject go = new GameObject("EndOfFrameCall");
  282. instance = go.AddComponent<EndOfFrameCall>();
  283. DontDestroyOnLoad(go);
  284. }
  285. return instance;
  286. }
  287. set { instance = value; }
  288. }
  289. private static EndOfFrameCall instance;
  290. public static void Call(Action action)
  291. {
  292. Instance.StartCoroutine(call(action));
  293. }
  294. private static IEnumerator call(Action action)
  295. {
  296. yield return new WaitForEndOfFrame();
  297. action.Invoke();
  298. }
  299. }
  300. }