ObservableTriggerExtensions.Component.cs 24 KB

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