using System; // require keep for Windows Universal App
using UnityEngine;
namespace UniRx.Triggers
{
// for GameObject
public static partial class ObservableTriggerExtensions
{
#region ObservableAnimatorTrigger
/// Callback for setting up animation IK (inverse kinematics).
public static IObservable OnAnimatorIKAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnAnimatorIKAsObservable();
}
/// Callback for processing animation movements for modifying root motion.
public static IObservable OnAnimatorMoveAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnAnimatorMoveAsObservable();
}
#endregion
#region ObservableCollision2DTrigger
/// Sent when an incoming collider makes contact with this object's collider (2D physics only).
public static IObservable OnCollisionEnter2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionEnter2DAsObservable();
}
/// Sent when a collider on another object stops touching this object's collider (2D physics only).
public static IObservable OnCollisionExit2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionExit2DAsObservable();
}
/// Sent each frame where a collider on another object is touching this object's collider (2D physics only).
public static IObservable OnCollisionStay2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionStay2DAsObservable();
}
#endregion
#region ObservableCollisionTrigger
/// OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
public static IObservable OnCollisionEnterAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionEnterAsObservable();
}
/// OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
public static IObservable OnCollisionExitAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionExitAsObservable();
}
/// OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
public static IObservable OnCollisionStayAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCollisionStayAsObservable();
}
#endregion
#region ObservableDestroyTrigger
/// This function is called when the MonoBehaviour will be destroyed.
public static IObservable OnDestroyAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Return(Unit.Default); // send destroy message
return GetOrAddComponent(gameObject).OnDestroyAsObservable();
}
#endregion
#region ObservableEnableTrigger
/// This function is called when the object becomes enabled and active.
public static IObservable OnEnableAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnEnableAsObservable();
}
/// This function is called when the behaviour becomes disabled () or inactive.
public static IObservable OnDisableAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnDisableAsObservable();
}
#endregion
#region ObservableFixedUpdateTrigger
/// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
public static IObservable FixedUpdateAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).FixedUpdateAsObservable();
}
#endregion
#region ObservableLateUpdateTrigger
/// LateUpdate is called every frame, if the Behaviour is enabled.
public static IObservable LateUpdateAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).LateUpdateAsObservable();
}
#endregion
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
#region ObservableMouseTrigger
/// OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
public static IObservable OnMouseDownAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseDownAsObservable();
}
/// OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
public static IObservable OnMouseDragAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseDragAsObservable();
}
/// OnMouseEnter is called when the mouse entered the GUIElement or Collider.
public static IObservable OnMouseEnterAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseEnterAsObservable();
}
/// OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider.
public static IObservable OnMouseExitAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseExitAsObservable();
}
/// OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.
public static IObservable OnMouseOverAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseOverAsObservable();
}
/// OnMouseUp is called when the user has released the mouse button.
public static IObservable OnMouseUpAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseUpAsObservable();
}
/// OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.
public static IObservable OnMouseUpAsButtonAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnMouseUpAsButtonAsObservable();
}
#endregion
#endif
#region ObservableTrigger2DTrigger
/// Sent when another object enters a trigger collider attached to this object (2D physics only).
public static IObservable OnTriggerEnter2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerEnter2DAsObservable();
}
/// Sent when another object leaves a trigger collider attached to this object (2D physics only).
public static IObservable OnTriggerExit2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerExit2DAsObservable();
}
/// Sent each frame where another object is within a trigger collider attached to this object (2D physics only).
public static IObservable OnTriggerStay2DAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerStay2DAsObservable();
}
#endregion
#region ObservableTriggerTrigger
/// OnTriggerEnter is called when the Collider other enters the trigger.
public static IObservable OnTriggerEnterAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerEnterAsObservable();
}
/// OnTriggerExit is called when the Collider other has stopped touching the trigger.
public static IObservable OnTriggerExitAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerExitAsObservable();
}
/// OnTriggerStay is called once per frame for every Collider other that is touching the trigger.
public static IObservable OnTriggerStayAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTriggerStayAsObservable();
}
#endregion
#region ObservableUpdateTrigger
/// Update is called every frame, if the MonoBehaviour is enabled.
public static IObservable UpdateAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).UpdateAsObservable();
}
#endregion
#region ObservableVisibleTrigger
/// OnBecameInvisible is called when the renderer is no longer visible by any camera.
public static IObservable OnBecameInvisibleAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnBecameInvisibleAsObservable();
}
/// OnBecameVisible is called when the renderer became visible by any camera.
public static IObservable OnBecameVisibleAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnBecameVisibleAsObservable();
}
#endregion
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
#region ObservableTransformChangedTrigger
/// Callback sent to the graphic before a Transform parent change occurs.
public static IObservable OnBeforeTransformParentChangedAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnBeforeTransformParentChangedAsObservable();
}
/// This function is called when the parent property of the transform of the GameObject has changed.
public static IObservable OnTransformParentChangedAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTransformParentChangedAsObservable();
}
/// This function is called when the list of children of the transform of the GameObject has changed.
public static IObservable OnTransformChildrenChangedAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnTransformChildrenChangedAsObservable();
}
#endregion
#region ObservableCanvasGroupChangedTrigger
/// Callback that is sent if the canvas group is changed.
public static IObservable OnCanvasGroupChangedAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnCanvasGroupChangedAsObservable();
}
#endregion
#region ObservableRectTransformTrigger
/// Callback that is sent if an associated RectTransform has it's dimensions changed.
public static IObservable OnRectTransformDimensionsChangeAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnRectTransformDimensionsChangeAsObservable();
}
/// Callback that is sent if an associated RectTransform is removed.
public static IObservable OnRectTransformRemovedAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnRectTransformRemovedAsObservable();
}
#endregion
#endif
#region ObservableParticleTrigger
/// OnParticleCollision is called when a particle hits a collider.
public static IObservable OnParticleCollisionAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnParticleCollisionAsObservable();
}
#if UNITY_5_4_OR_NEWER
/// OnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.
public static IObservable OnParticleTriggerAsObservable(this GameObject gameObject)
{
if (gameObject == null) return Observable.Empty();
return GetOrAddComponent(gameObject).OnParticleTriggerAsObservable();
}
#endif
#endregion
static T GetOrAddComponent(GameObject gameObject)
where T : Component
{
var component = gameObject.GetComponent();
if (component == null)
{
component = gameObject.AddComponent();
}
return component;
}
}
}