Skip to content

Commit

Permalink
Add Triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Feb 13, 2024
1 parent c69d89d commit 1475fa2
Show file tree
Hide file tree
Showing 81 changed files with 3,146 additions and 247 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ You open tracker window in `Window -> Observable Tracker`. It enables watch `Obs

Observable Tracker is intended for debugging use only as enabling tracking and capturing stacktraces is useful but has a heavy performance impact. Recommended usage is to enable both tracking and stacktraces to find subscription leaks and to disable them both when done.

### `SerializableReactiveProperty<T>`
#### `SerializableReactiveProperty<T>`

`ReactiveProperty<T>` can not use on `[SerializeField]`. However you can use `SerializableReactiveProperty<T>` instead.

Expand Down Expand Up @@ -1040,6 +1040,12 @@ public class NewBehaviourScript : MonoBehaviour

![image](https://github.com/Cysharp/R3/assets/46207/31be9378-846e-4635-8cc6-0b6e3954e918)

#### Triggers

R3 can handle [MonoBehaviour messages](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html) with R3.Triggers:

These can also be handled more easily by directly subscribing to observables returned by extension methods on Component/GameObject. These methods inject ObservableTrigger automaticaly.

### Godot

Godot support is for Godot 4.x.
Expand Down
15 changes: 15 additions & 0 deletions src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
"name": "com.unity.ugui",
"expression": "",
"define": "R3_UGUI_SUPPORT"
},
{
"name": "com.unity.modules.physics",
"expression": "",
"define": "R3_PHYSICS_SUPPORT"
},
{
"name": "com.unity.modules.physics2d",
"expression": "",
"define": "R3_PHYSICS2D_SUPPORT"
},
{
"name": "com.unity.modules.particlesystem",
"expression": "",
"define": "R3_PARTICLESYSTEM_SUPPORT"
}
],
"noEngineReferences": false
Expand Down
8 changes: 8 additions & 0 deletions src/R3.Unity/Assets/R3.Unity/Runtime/Triggers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using UnityEngine;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableAnimatorTrigger : ObservableTriggerBase
{
Subject<int> onAnimatorIK;

/// <summary>Callback for setting up animation IK (inverse kinematics).</summary>
void OnAnimatorIK(int layerIndex)
{
if (onAnimatorIK != null) onAnimatorIK.OnNext(layerIndex);
}

/// <summary>Callback for setting up animation IK (inverse kinematics).</summary>
public Observable<int> OnAnimatorIKAsObservable()
{
return onAnimatorIK ?? (onAnimatorIK = new Subject<int>());
}

Subject<Unit> onAnimatorMove;

/// <summary>Callback for processing animation movements for modifying root motion.</summary>
void OnAnimatorMove()
{
if (onAnimatorMove != null) onAnimatorMove.OnNext(Unit.Default);
}

/// <summary>Callback for processing animation movements for modifying root motion.</summary>
public Observable<Unit> OnAnimatorMoveAsObservable()
{
return onAnimatorMove ?? (onAnimatorMove = new Subject<Unit>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onAnimatorIK != null)
{
onAnimatorIK.OnCompleted();
}
if (onAnimatorMove != null)
{
onAnimatorMove.OnCompleted();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if R3_UGUI_SUPPORT

using UnityEngine;
using UnityEngine.EventSystems;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableBeginDragTrigger : ObservableTriggerBase, IEventSystemHandler, IBeginDragHandler
{
Subject<PointerEventData> onBeginDrag;

void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
{
if (onBeginDrag != null) onBeginDrag.OnNext(eventData);
}

public Observable<PointerEventData> OnBeginDragAsObservable()
{
return onBeginDrag ?? (onBeginDrag = new Subject<PointerEventData>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onBeginDrag != null)
{
onBeginDrag.OnCompleted();
}
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if R3_UGUI_SUPPORT

using UnityEngine;
using UnityEngine.EventSystems;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableCancelTrigger : ObservableTriggerBase, IEventSystemHandler, ICancelHandler
{
Subject<BaseEventData> onCancel;

void ICancelHandler.OnCancel(BaseEventData eventData)
{
if (onCancel != null) onCancel.OnNext(eventData);
}

public Observable<BaseEventData> OnCancelAsObservable()
{
return onCancel ?? (onCancel = new Subject<BaseEventData>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onCancel != null)
{
onCancel.OnCompleted();
}
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableCanvasGroupChangedTrigger : ObservableTriggerBase
{
Subject<Unit> onCanvasGroupChanged;

// Callback that is sent if the canvas group is changed
void OnCanvasGroupChanged()
{
if (onCanvasGroupChanged != null) onCanvasGroupChanged.OnNext(Unit.Default);
}

/// <summary>Callback that is sent if the canvas group is changed.</summary>
public Observable<Unit> OnCanvasGroupChangedAsObservable()
{
return onCanvasGroupChanged ?? (onCanvasGroupChanged = new Subject<Unit>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onCanvasGroupChanged != null)
{
onCanvasGroupChanged.OnCompleted();
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if R3_PHYSICS2D_SUPPORT
using UnityEngine;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableCollision2DTrigger : ObservableTriggerBase
{
Subject<Collision2D> onCollisionEnter2D;

/// <summary>Sent when an incoming collider makes contact with this object's collider (2D physics only).</summary>
void OnCollisionEnter2D(Collision2D coll)
{
if (onCollisionEnter2D != null) onCollisionEnter2D.OnNext(coll);
}

/// <summary>Sent when an incoming collider makes contact with this object's collider (2D physics only).</summary>
public Observable<Collision2D> OnCollisionEnter2DAsObservable()
{
return onCollisionEnter2D ?? (onCollisionEnter2D = new Subject<Collision2D>());
}

Subject<Collision2D> onCollisionExit2D;

/// <summary>Sent when a collider on another object stops touching this object's collider (2D physics only).</summary>
void OnCollisionExit2D(Collision2D coll)
{
if (onCollisionExit2D != null) onCollisionExit2D.OnNext(coll);
}

/// <summary>Sent when a collider on another object stops touching this object's collider (2D physics only).</summary>
public Observable<Collision2D> OnCollisionExit2DAsObservable()
{
return onCollisionExit2D ?? (onCollisionExit2D = new Subject<Collision2D>());
}

Subject<Collision2D> onCollisionStay2D;

/// <summary>Sent each frame where a collider on another object is touching this object's collider (2D physics only).</summary>
void OnCollisionStay2D(Collision2D coll)
{
if (onCollisionStay2D != null) onCollisionStay2D.OnNext(coll);
}

/// <summary>Sent each frame where a collider on another object is touching this object's collider (2D physics only).</summary>
public Observable<Collision2D> OnCollisionStay2DAsObservable()
{
return onCollisionStay2D ?? (onCollisionStay2D = new Subject<Collision2D>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onCollisionEnter2D != null)
{
onCollisionEnter2D.OnCompleted();
}
if (onCollisionExit2D != null)
{
onCollisionExit2D.OnCompleted();
}
if (onCollisionStay2D != null)
{
onCollisionStay2D.OnCompleted();
}
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if R3_PHYSICS_SUPPORT
using UnityEngine;

namespace R3.Triggers
{
[DisallowMultipleComponent]
public class ObservableCollisionTrigger : ObservableTriggerBase
{
Subject<Collision> onCollisionEnter;

/// <summary>OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.</summary>
void OnCollisionEnter(Collision collision)
{
if (onCollisionEnter != null) onCollisionEnter.OnNext(collision);
}

/// <summary>OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.</summary>
public Observable<Collision> OnCollisionEnterAsObservable()
{
return onCollisionEnter ?? (onCollisionEnter = new Subject<Collision>());
}

Subject<Collision> onCollisionExit;

/// <summary>OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.</summary>
void OnCollisionExit(Collision collisionInfo)
{
if (onCollisionExit != null) onCollisionExit.OnNext(collisionInfo);
}

/// <summary>OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.</summary>
public Observable<Collision> OnCollisionExitAsObservable()
{
return onCollisionExit ?? (onCollisionExit = new Subject<Collision>());
}

Subject<Collision> onCollisionStay;

/// <summary>OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.</summary>
void OnCollisionStay(Collision collisionInfo)
{
if (onCollisionStay != null) onCollisionStay.OnNext(collisionInfo);
}

/// <summary>OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.</summary>
public Observable<Collision> OnCollisionStayAsObservable()
{
return onCollisionStay ?? (onCollisionStay = new Subject<Collision>());
}

protected override void RaiseOnCompletedOnDestroy()
{
if (onCollisionEnter != null)
{
onCollisionEnter.OnCompleted();
}
if (onCollisionExit != null)
{
onCollisionExit.OnCompleted();
}
if (onCollisionStay != null)
{
onCollisionStay.OnCompleted();
}
}
}
}
#endif
Loading

0 comments on commit 1475fa2

Please sign in to comment.