diff --git a/src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef b/src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef index b34072a9..795ba04b 100644 --- a/src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef +++ b/src/R3.Unity/Assets/R3.Unity/Runtime/R3.Unity.asmdef @@ -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 -} +} \ No newline at end of file diff --git a/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs b/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs new file mode 100644 index 00000000..49957c88 --- /dev/null +++ b/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs @@ -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 onStateExit; + + public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) + { + onStateExit?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex)); + } + + public Observable OnStateExitAsObservable() + { + return onStateExit ??= new Subject(); + } + + // OnStateEnter + Subject onStateEnter; + + public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) + { + onStateEnter?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex)); + } + + public Observable OnStateEnterAsObservable() + { + return onStateEnter ??= new Subject(); + } + + // OnStateIK + Subject onStateIK; + + public override void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) + { + if(onStateIK !=null) onStateIK.OnNext(new OnStateInfo(animator, stateInfo, layerIndex)); + } + + public Observable OnStateIKAsObservable() + { + return onStateIK ??= new Subject(); + } + + // OnStateUpdate + Subject onStateUpdate; + + public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) + { + onStateUpdate?.OnNext(new OnStateInfo(animator, stateInfo, layerIndex)); + } + + public Observable OnStateUpdateAsObservable() + { + return onStateUpdate ??= new Subject(); + } + + // OnStateMachineEnter + Subject onStateMachineEnter; + + public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash) + { + onStateMachineEnter?.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash)); + } + + public Observable OnStateMachineEnterAsObservable() + { + return onStateMachineEnter ??= new Subject(); + } + + // OnStateMachineExit + Subject onStateMachineExit; + + public override void OnStateMachineExit(Animator animator, int stateMachinePathHash) + { + onStateMachineExit?.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash)); + } + + public Observable OnStateMachineExitAsObservable() + { + return onStateMachineExit ??= new Subject(); + } + } +} +#endif diff --git a/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs.meta b/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs.meta new file mode 100644 index 00000000..4fa5543f --- /dev/null +++ b/src/R3.Unity/Assets/R3.Unity/Runtime/Triggers/ObservableStateMachineTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b6f6416fc103718499c3a5dae141b4c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: