Skip to content

Commit

Permalink
Merge pull request #103 from TORISOUP/add_ObservableStateMachineTrigger
Browse files Browse the repository at this point in the history
Add ObservableStateMachineTrigger to R3.Unity
  • Loading branch information
neuecc authored Feb 15, 2024
2 parents 9850c37 + fd48371 commit 9fed1ef
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
"name": "com.unity.modules.particlesystem",
"expression": "",
"define": "R3_PARTICLESYSTEM_SUPPORT"
},
{
"name": "com.unity.modules.animation",
"expression": "",
"define": "R3_ANIMATION_SUPPORT"
}
],
"noEngineReferences": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#if R3_ANIMATION_SUPPORT
using UnityEngine;

namespace R3.Triggers
{
public class ObservableStateMachineTrigger : StateMachineBehaviour
{
public class OnStateInfo
{
public Animator Animator { get; private set; }
public AnimatorStateInfo StateInfo { get; private set; }
public int LayerIndex { get; private set; }

public OnStateInfo(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Animator = animator;
StateInfo = stateInfo;
LayerIndex = layerIndex;
}
}

public class OnStateMachineInfo
{
public Animator Animator { get; private set; }
public int StateMachinePathHash { get; private set; }

public OnStateMachineInfo(Animator animator, int stateMachinePathHash)
{
Animator = animator;
StateMachinePathHash = stateMachinePathHash;
}
}

// OnStateExit
Subject<OnStateInfo> onStateExit;

public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
onStateExit?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
}

public Observable<OnStateInfo> OnStateExitAsObservable()
{
return onStateExit ??= new Subject<OnStateInfo>();
}

// OnStateEnter
Subject<OnStateInfo> onStateEnter;

public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
onStateEnter?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
}

public Observable<OnStateInfo> OnStateEnterAsObservable()
{
return onStateEnter ??= new Subject<OnStateInfo>();
}

// OnStateIK
Subject<OnStateInfo> onStateIK;

public override void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if(onStateIK !=null) onStateIK.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
}

public Observable<OnStateInfo> OnStateIKAsObservable()
{
return onStateIK ??= new Subject<OnStateInfo>();
}

// OnStateUpdate
Subject<OnStateInfo> onStateUpdate;

public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
onStateUpdate?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
}

public Observable<OnStateInfo> OnStateUpdateAsObservable()
{
return onStateUpdate ??= new Subject<OnStateInfo>();
}

// OnStateMachineEnter
Subject<OnStateMachineInfo> onStateMachineEnter;

public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
{
onStateMachineEnter?.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash));
}

public Observable<OnStateMachineInfo> OnStateMachineEnterAsObservable()
{
return onStateMachineEnter ??= new Subject<OnStateMachineInfo>();
}

// OnStateMachineExit
Subject<OnStateMachineInfo> onStateMachineExit;

public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
{
onStateMachineExit?.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash));
}

public Observable<OnStateMachineInfo> OnStateMachineExitAsObservable()
{
return onStateMachineExit ??= new Subject<OnStateMachineInfo>();
}
}
}
#endif

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

0 comments on commit 9fed1ef

Please sign in to comment.