AssertionComponent.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityTest
  6. {
  7. [Serializable]
  8. public class AssertionComponent : MonoBehaviour
  9. {
  10. [Flags]
  11. public enum CheckMethod
  12. {
  13. AfterPeriodOfTime = 1 << 0,
  14. Start = 1 << 1,
  15. Update = 1 << 2,
  16. FixedUpdate = 1 << 3,
  17. LateUpdate = 1 << 4,
  18. OnDestroy = 1 << 5,
  19. OnEnable = 1 << 6,
  20. OnDisable = 1 << 7,
  21. OnControllerColliderHit = 1 << 8,
  22. OnParticleCollision = 1 << 9,
  23. OnJointBreak = 1 << 10,
  24. OnBecameInvisible = 1 << 11,
  25. OnBecameVisible = 1 << 12,
  26. OnTriggerEnter = 1 << 13,
  27. OnTriggerExit = 1 << 14,
  28. OnTriggerStay = 1 << 15,
  29. OnCollisionEnter = 1 << 16,
  30. OnCollisionExit = 1 << 17,
  31. OnCollisionStay = 1 << 18,
  32. #if !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  33. OnTriggerEnter2D = 1 << 19,
  34. OnTriggerExit2D = 1 << 20,
  35. OnTriggerStay2D = 1 << 21,
  36. OnCollisionEnter2D = 1 << 22,
  37. OnCollisionExit2D = 1 << 23,
  38. OnCollisionStay2D = 1 << 24,
  39. #endif
  40. }
  41. [SerializeField] public float checkAfterTime = 1f;
  42. [SerializeField] public bool repeatCheckTime = true;
  43. [SerializeField] public float repeatEveryTime = 1f;
  44. [SerializeField] public int checkAfterFrames = 1;
  45. [SerializeField] public bool repeatCheckFrame = true;
  46. [SerializeField] public int repeatEveryFrame = 1;
  47. [SerializeField] public bool hasFailed;
  48. [SerializeField] public CheckMethod checkMethods = CheckMethod.Start;
  49. [SerializeField] private ActionBase m_ActionBase;
  50. [SerializeField] public int checksPerformed = 0;
  51. private int checkOnFrame = 0;
  52. public ActionBase Action
  53. {
  54. get { return m_ActionBase; }
  55. set
  56. {
  57. m_ActionBase = value;
  58. m_ActionBase.go = gameObject;
  59. m_ActionBase.thisPropertyPath = "";
  60. if (m_ActionBase is ComparerBase)
  61. (m_ActionBase as ComparerBase).otherPropertyPath = "";
  62. }
  63. }
  64. public void Awake ()
  65. {
  66. if (!Debug.isDebugBuild)
  67. Destroy (this);
  68. OnComponentCopy ();
  69. }
  70. #if UNITY_EDITOR
  71. public void OnValidate ()
  72. {
  73. OnComponentCopy ();
  74. }
  75. #endif
  76. private void OnComponentCopy ()
  77. {
  78. if (m_ActionBase == null) return;
  79. var oldAction = FindObjectsOfType (typeof (AssertionComponent)).Where (o => ((AssertionComponent) o).m_ActionBase == m_ActionBase && o != this);
  80. //if it's not a copy but a new component don't do anything
  81. if (!oldAction.Any ()) return;
  82. if (oldAction.Count () > 1)
  83. Debug.LogWarning ("More than one refence to comparer found. This shouldn't happen");
  84. m_ActionBase = ((AssertionComponent) oldAction.Single ()).m_ActionBase.CreateCopy ();
  85. }
  86. public void Start ()
  87. {
  88. CheckAssertionFor (CheckMethod.Start);
  89. if(IsCheckMethodSelected(CheckMethod.AfterPeriodOfTime))
  90. {
  91. StartCoroutine("CheckPeriodically");
  92. }
  93. if(IsCheckMethodSelected (CheckMethod.Update))
  94. {
  95. checkOnFrame = Time.frameCount + checkAfterFrames;
  96. }
  97. }
  98. public IEnumerator CheckPeriodically()
  99. {
  100. yield return new WaitForSeconds(checkAfterTime);
  101. CheckAssertionFor(CheckMethod.AfterPeriodOfTime);
  102. while (repeatCheckTime)
  103. {
  104. yield return new WaitForSeconds(repeatEveryTime);
  105. CheckAssertionFor(CheckMethod.AfterPeriodOfTime);
  106. }
  107. }
  108. public bool ShouldCheckOnFrame()
  109. {
  110. if (Time.frameCount > checkOnFrame)
  111. {
  112. if (repeatCheckFrame)
  113. checkOnFrame += repeatEveryFrame;
  114. return true;
  115. }
  116. return false;
  117. }
  118. public void OnDisable ()
  119. {
  120. CheckAssertionFor (CheckMethod.OnDisable);
  121. }
  122. public void OnEnable ()
  123. {
  124. CheckAssertionFor (CheckMethod.OnEnable);
  125. }
  126. public void OnDestroy ()
  127. {
  128. CheckAssertionFor (CheckMethod.OnDestroy);
  129. }
  130. public void Update ()
  131. {
  132. if (IsCheckMethodSelected(CheckMethod.Update) && ShouldCheckOnFrame ())
  133. {
  134. CheckAssertionFor (CheckMethod.Update);
  135. }
  136. }
  137. public void FixedUpdate ()
  138. {
  139. CheckAssertionFor(CheckMethod.FixedUpdate);
  140. }
  141. public void LateUpdate ()
  142. {
  143. CheckAssertionFor (CheckMethod.LateUpdate);
  144. }
  145. public void OnControllerColliderHit ()
  146. {
  147. CheckAssertionFor (CheckMethod.OnControllerColliderHit);
  148. }
  149. public void OnParticleCollision ()
  150. {
  151. CheckAssertionFor (CheckMethod.OnParticleCollision);
  152. }
  153. public void OnJointBreak ()
  154. {
  155. CheckAssertionFor (CheckMethod.OnJointBreak);
  156. }
  157. public void OnBecameInvisible ()
  158. {
  159. CheckAssertionFor (CheckMethod.OnBecameInvisible);
  160. }
  161. public void OnBecameVisible ()
  162. {
  163. CheckAssertionFor (CheckMethod.OnBecameVisible);
  164. }
  165. public void OnTriggerEnter ()
  166. {
  167. CheckAssertionFor (CheckMethod.OnTriggerEnter);
  168. }
  169. public void OnTriggerExit ()
  170. {
  171. CheckAssertionFor (CheckMethod.OnTriggerExit);
  172. }
  173. public void OnTriggerStay ()
  174. {
  175. CheckAssertionFor (CheckMethod.OnTriggerStay);
  176. }
  177. public void OnCollisionEnter ()
  178. {
  179. CheckAssertionFor (CheckMethod.OnCollisionEnter);
  180. }
  181. public void OnCollisionExit ()
  182. {
  183. CheckAssertionFor (CheckMethod.OnCollisionExit);
  184. }
  185. public void OnCollisionStay ()
  186. {
  187. CheckAssertionFor (CheckMethod.OnCollisionStay);
  188. }
  189. #if !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  190. public void OnTriggerEnter2D ()
  191. {
  192. CheckAssertionFor (CheckMethod.OnTriggerEnter2D);
  193. }
  194. public void OnTriggerExit2D ()
  195. {
  196. CheckAssertionFor (CheckMethod.OnTriggerExit2D);
  197. }
  198. public void OnTriggerStay2D ()
  199. {
  200. CheckAssertionFor (CheckMethod.OnTriggerStay2D);
  201. }
  202. public void OnCollisionEnter2D ()
  203. {
  204. CheckAssertionFor (CheckMethod.OnCollisionEnter2D);
  205. }
  206. public void OnCollisionExit2D ()
  207. {
  208. CheckAssertionFor (CheckMethod.OnCollisionExit2D);
  209. }
  210. public void OnCollisionStay2D ()
  211. {
  212. CheckAssertionFor (CheckMethod.OnCollisionStay2D);
  213. }
  214. #endif
  215. private void CheckAssertionFor (CheckMethod checkMethod)
  216. {
  217. if (IsCheckMethodSelected (checkMethod))
  218. {
  219. Assertions.CheckAssertions (this);
  220. }
  221. }
  222. public bool IsCheckMethodSelected (CheckMethod method)
  223. {
  224. return method == (checkMethods & method);
  225. }
  226. }
  227. }