ObservableTriggerExtensions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System; // require keep for Windows Universal App
  2. using UnityEngine;
  3. namespace UniRx.Triggers
  4. {
  5. // for GameObject
  6. public static partial class ObservableTriggerExtensions
  7. {
  8. #region ObservableAnimatorTrigger
  9. /// <summary>Callback for setting up animation IK (inverse kinematics).</summary>
  10. public static IObservable<int> OnAnimatorIKAsObservable(this GameObject gameObject)
  11. {
  12. if (gameObject == null) return Observable.Empty<int>();
  13. return GetOrAddComponent<ObservableAnimatorTrigger>(gameObject).OnAnimatorIKAsObservable();
  14. }
  15. /// <summary>Callback for processing animation movements for modifying root motion.</summary>
  16. public static IObservable<Unit> OnAnimatorMoveAsObservable(this GameObject gameObject)
  17. {
  18. if (gameObject == null) return Observable.Empty<Unit>();
  19. return GetOrAddComponent<ObservableAnimatorTrigger>(gameObject).OnAnimatorMoveAsObservable();
  20. }
  21. #endregion
  22. #region ObservableCollision2DTrigger
  23. /// <summary>Sent when an incoming collider makes contact with this object's collider (2D physics only).</summary>
  24. public static IObservable<Collision2D> OnCollisionEnter2DAsObservable(this GameObject gameObject)
  25. {
  26. if (gameObject == null) return Observable.Empty<Collision2D>();
  27. return GetOrAddComponent<ObservableCollision2DTrigger>(gameObject).OnCollisionEnter2DAsObservable();
  28. }
  29. /// <summary>Sent when a collider on another object stops touching this object's collider (2D physics only).</summary>
  30. public static IObservable<Collision2D> OnCollisionExit2DAsObservable(this GameObject gameObject)
  31. {
  32. if (gameObject == null) return Observable.Empty<Collision2D>();
  33. return GetOrAddComponent<ObservableCollision2DTrigger>(gameObject).OnCollisionExit2DAsObservable();
  34. }
  35. /// <summary>Sent each frame where a collider on another object is touching this object's collider (2D physics only).</summary>
  36. public static IObservable<Collision2D> OnCollisionStay2DAsObservable(this GameObject gameObject)
  37. {
  38. if (gameObject == null) return Observable.Empty<Collision2D>();
  39. return GetOrAddComponent<ObservableCollision2DTrigger>(gameObject).OnCollisionStay2DAsObservable();
  40. }
  41. #endregion
  42. #region ObservableCollisionTrigger
  43. /// <summary>OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.</summary>
  44. public static IObservable<Collision> OnCollisionEnterAsObservable(this GameObject gameObject)
  45. {
  46. if (gameObject == null) return Observable.Empty<Collision>();
  47. return GetOrAddComponent<ObservableCollisionTrigger>(gameObject).OnCollisionEnterAsObservable();
  48. }
  49. /// <summary>OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.</summary>
  50. public static IObservable<Collision> OnCollisionExitAsObservable(this GameObject gameObject)
  51. {
  52. if (gameObject == null) return Observable.Empty<Collision>();
  53. return GetOrAddComponent<ObservableCollisionTrigger>(gameObject).OnCollisionExitAsObservable();
  54. }
  55. /// <summary>OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.</summary>
  56. public static IObservable<Collision> OnCollisionStayAsObservable(this GameObject gameObject)
  57. {
  58. if (gameObject == null) return Observable.Empty<Collision>();
  59. return GetOrAddComponent<ObservableCollisionTrigger>(gameObject).OnCollisionStayAsObservable();
  60. }
  61. #endregion
  62. #region ObservableDestroyTrigger
  63. /// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
  64. public static IObservable<Unit> OnDestroyAsObservable(this GameObject gameObject)
  65. {
  66. if (gameObject == null) return Observable.Return(Unit.Default); // send destroy message
  67. return GetOrAddComponent<ObservableDestroyTrigger>(gameObject).OnDestroyAsObservable();
  68. }
  69. #endregion
  70. #region ObservableEnableTrigger
  71. /// <summary>This function is called when the object becomes enabled and active.</summary>
  72. public static IObservable<Unit> OnEnableAsObservable(this GameObject gameObject)
  73. {
  74. if (gameObject == null) return Observable.Empty<Unit>();
  75. return GetOrAddComponent<ObservableEnableTrigger>(gameObject).OnEnableAsObservable();
  76. }
  77. /// <summary>This function is called when the behaviour becomes disabled () or inactive.</summary>
  78. public static IObservable<Unit> OnDisableAsObservable(this GameObject gameObject)
  79. {
  80. if (gameObject == null) return Observable.Empty<Unit>();
  81. return GetOrAddComponent<ObservableEnableTrigger>(gameObject).OnDisableAsObservable();
  82. }
  83. #endregion
  84. #region ObservableFixedUpdateTrigger
  85. /// <summary>This function is called every fixed framerate frame, if the MonoBehaviour is enabled.</summary>
  86. public static IObservable<Unit> FixedUpdateAsObservable(this GameObject gameObject)
  87. {
  88. if (gameObject == null) return Observable.Empty<Unit>();
  89. return GetOrAddComponent<ObservableFixedUpdateTrigger>(gameObject).FixedUpdateAsObservable();
  90. }
  91. #endregion
  92. #region ObservableLateUpdateTrigger
  93. /// <summary>LateUpdate is called every frame, if the Behaviour is enabled.</summary>
  94. public static IObservable<Unit> LateUpdateAsObservable(this GameObject gameObject)
  95. {
  96. if (gameObject == null) return Observable.Empty<Unit>();
  97. return GetOrAddComponent<ObservableLateUpdateTrigger>(gameObject).LateUpdateAsObservable();
  98. }
  99. #endregion
  100. #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
  101. #region ObservableMouseTrigger
  102. /// <summary>OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.</summary>
  103. public static IObservable<Unit> OnMouseDownAsObservable(this GameObject gameObject)
  104. {
  105. if (gameObject == null) return Observable.Empty<Unit>();
  106. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseDownAsObservable();
  107. }
  108. /// <summary>OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.</summary>
  109. public static IObservable<Unit> OnMouseDragAsObservable(this GameObject gameObject)
  110. {
  111. if (gameObject == null) return Observable.Empty<Unit>();
  112. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseDragAsObservable();
  113. }
  114. /// <summary>OnMouseEnter is called when the mouse entered the GUIElement or Collider.</summary>
  115. public static IObservable<Unit> OnMouseEnterAsObservable(this GameObject gameObject)
  116. {
  117. if (gameObject == null) return Observable.Empty<Unit>();
  118. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseEnterAsObservable();
  119. }
  120. /// <summary>OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.</summary>
  121. public static IObservable<Unit> OnMouseExitAsObservable(this GameObject gameObject)
  122. {
  123. if (gameObject == null) return Observable.Empty<Unit>();
  124. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseExitAsObservable();
  125. }
  126. /// <summary>OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.</summary>
  127. public static IObservable<Unit> OnMouseOverAsObservable(this GameObject gameObject)
  128. {
  129. if (gameObject == null) return Observable.Empty<Unit>();
  130. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseOverAsObservable();
  131. }
  132. /// <summary>OnMouseUp is called when the user has released the mouse button.</summary>
  133. public static IObservable<Unit> OnMouseUpAsObservable(this GameObject gameObject)
  134. {
  135. if (gameObject == null) return Observable.Empty<Unit>();
  136. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseUpAsObservable();
  137. }
  138. /// <summary>OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.</summary>
  139. public static IObservable<Unit> OnMouseUpAsButtonAsObservable(this GameObject gameObject)
  140. {
  141. if (gameObject == null) return Observable.Empty<Unit>();
  142. return GetOrAddComponent<ObservableMouseTrigger>(gameObject).OnMouseUpAsButtonAsObservable();
  143. }
  144. #endregion
  145. #endif
  146. #region ObservableTrigger2DTrigger
  147. /// <summary>Sent when another object enters a trigger collider attached to this object (2D physics only).</summary>
  148. public static IObservable<Collider2D> OnTriggerEnter2DAsObservable(this GameObject gameObject)
  149. {
  150. if (gameObject == null) return Observable.Empty<Collider2D>();
  151. return GetOrAddComponent<ObservableTrigger2DTrigger>(gameObject).OnTriggerEnter2DAsObservable();
  152. }
  153. /// <summary>Sent when another object leaves a trigger collider attached to this object (2D physics only).</summary>
  154. public static IObservable<Collider2D> OnTriggerExit2DAsObservable(this GameObject gameObject)
  155. {
  156. if (gameObject == null) return Observable.Empty<Collider2D>();
  157. return GetOrAddComponent<ObservableTrigger2DTrigger>(gameObject).OnTriggerExit2DAsObservable();
  158. }
  159. /// <summary>Sent each frame where another object is within a trigger collider attached to this object (2D physics only).</summary>
  160. public static IObservable<Collider2D> OnTriggerStay2DAsObservable(this GameObject gameObject)
  161. {
  162. if (gameObject == null) return Observable.Empty<Collider2D>();
  163. return GetOrAddComponent<ObservableTrigger2DTrigger>(gameObject).OnTriggerStay2DAsObservable();
  164. }
  165. #endregion
  166. #region ObservableTriggerTrigger
  167. /// <summary>OnTriggerEnter is called when the Collider other enters the trigger.</summary>
  168. public static IObservable<Collider> OnTriggerEnterAsObservable(this GameObject gameObject)
  169. {
  170. if (gameObject == null) return Observable.Empty<Collider>();
  171. return GetOrAddComponent<ObservableTriggerTrigger>(gameObject).OnTriggerEnterAsObservable();
  172. }
  173. /// <summary>OnTriggerExit is called when the Collider other has stopped touching the trigger.</summary>
  174. public static IObservable<Collider> OnTriggerExitAsObservable(this GameObject gameObject)
  175. {
  176. if (gameObject == null) return Observable.Empty<Collider>();
  177. return GetOrAddComponent<ObservableTriggerTrigger>(gameObject).OnTriggerExitAsObservable();
  178. }
  179. /// <summary>OnTriggerStay is called once per frame for every Collider other that is touching the trigger.</summary>
  180. public static IObservable<Collider> OnTriggerStayAsObservable(this GameObject gameObject)
  181. {
  182. if (gameObject == null) return Observable.Empty<Collider>();
  183. return GetOrAddComponent<ObservableTriggerTrigger>(gameObject).OnTriggerStayAsObservable();
  184. }
  185. #endregion
  186. #region ObservableUpdateTrigger
  187. /// <summary>Update is called every frame, if the MonoBehaviour is enabled.</summary>
  188. public static IObservable<Unit> UpdateAsObservable(this GameObject gameObject)
  189. {
  190. if (gameObject == null) return Observable.Empty<Unit>();
  191. return GetOrAddComponent<ObservableUpdateTrigger>(gameObject).UpdateAsObservable();
  192. }
  193. #endregion
  194. #region ObservableVisibleTrigger
  195. /// <summary>OnBecameInvisible is called when the renderer is no longer visible by any camera.</summary>
  196. public static IObservable<Unit> OnBecameInvisibleAsObservable(this GameObject gameObject)
  197. {
  198. if (gameObject == null) return Observable.Empty<Unit>();
  199. return GetOrAddComponent<ObservableVisibleTrigger>(gameObject).OnBecameInvisibleAsObservable();
  200. }
  201. /// <summary>OnBecameVisible is called when the renderer became visible by any camera.</summary>
  202. public static IObservable<Unit> OnBecameVisibleAsObservable(this GameObject gameObject)
  203. {
  204. if (gameObject == null) return Observable.Empty<Unit>();
  205. return GetOrAddComponent<ObservableVisibleTrigger>(gameObject).OnBecameVisibleAsObservable();
  206. }
  207. #endregion
  208. #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
  209. #region ObservableTransformChangedTrigger
  210. /// <summary>Callback sent to the graphic before a Transform parent change occurs.</summary>
  211. public static IObservable<Unit> OnBeforeTransformParentChangedAsObservable(this GameObject gameObject)
  212. {
  213. if (gameObject == null) return Observable.Empty<Unit>();
  214. return GetOrAddComponent<ObservableTransformChangedTrigger>(gameObject).OnBeforeTransformParentChangedAsObservable();
  215. }
  216. /// <summary>This function is called when the parent property of the transform of the GameObject has changed.</summary>
  217. public static IObservable<Unit> OnTransformParentChangedAsObservable(this GameObject gameObject)
  218. {
  219. if (gameObject == null) return Observable.Empty<Unit>();
  220. return GetOrAddComponent<ObservableTransformChangedTrigger>(gameObject).OnTransformParentChangedAsObservable();
  221. }
  222. /// <summary>This function is called when the list of children of the transform of the GameObject has changed.</summary>
  223. public static IObservable<Unit> OnTransformChildrenChangedAsObservable(this GameObject gameObject)
  224. {
  225. if (gameObject == null) return Observable.Empty<Unit>();
  226. return GetOrAddComponent<ObservableTransformChangedTrigger>(gameObject).OnTransformChildrenChangedAsObservable();
  227. }
  228. #endregion
  229. #region ObservableCanvasGroupChangedTrigger
  230. /// <summary>Callback that is sent if the canvas group is changed.</summary>
  231. public static IObservable<Unit> OnCanvasGroupChangedAsObservable(this GameObject gameObject)
  232. {
  233. if (gameObject == null) return Observable.Empty<Unit>();
  234. return GetOrAddComponent<ObservableCanvasGroupChangedTrigger>(gameObject).OnCanvasGroupChangedAsObservable();
  235. }
  236. #endregion
  237. #region ObservableRectTransformTrigger
  238. /// <summary>Callback that is sent if an associated RectTransform has it's dimensions changed.</summary>
  239. public static IObservable<Unit> OnRectTransformDimensionsChangeAsObservable(this GameObject gameObject)
  240. {
  241. if (gameObject == null) return Observable.Empty<Unit>();
  242. return GetOrAddComponent<ObservableRectTransformTrigger>(gameObject).OnRectTransformDimensionsChangeAsObservable();
  243. }
  244. /// <summary>Callback that is sent if an associated RectTransform is removed.</summary>
  245. public static IObservable<Unit> OnRectTransformRemovedAsObservable(this GameObject gameObject)
  246. {
  247. if (gameObject == null) return Observable.Empty<Unit>();
  248. return GetOrAddComponent<ObservableRectTransformTrigger>(gameObject).OnRectTransformRemovedAsObservable();
  249. }
  250. #endregion
  251. #endif
  252. #region ObservableParticleTrigger
  253. /// <summary>OnParticleCollision is called when a particle hits a collider.</summary>
  254. public static IObservable<GameObject> OnParticleCollisionAsObservable(this GameObject gameObject)
  255. {
  256. if (gameObject == null) return Observable.Empty<GameObject>();
  257. return GetOrAddComponent<ObservableParticleTrigger>(gameObject).OnParticleCollisionAsObservable();
  258. }
  259. #if UNITY_5_4_OR_NEWER
  260. /// <summary>OnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.</summary>
  261. public static IObservable<Unit> OnParticleTriggerAsObservable(this GameObject gameObject)
  262. {
  263. if (gameObject == null) return Observable.Empty<Unit>();
  264. return GetOrAddComponent<ObservableParticleTrigger>(gameObject).OnParticleTriggerAsObservable();
  265. }
  266. #endif
  267. #endregion
  268. static T GetOrAddComponent<T>(GameObject gameObject)
  269. where T : Component
  270. {
  271. var component = gameObject.GetComponent<T>();
  272. if (component == null)
  273. {
  274. component = gameObject.AddComponent<T>();
  275. }
  276. return component;
  277. }
  278. }
  279. }