1
0

ObservableStateMachineTrigger.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // over Unity5 added StateMachineBehaviour
  2. #if !(UNITY_4_7 || UNITY_4_6 || UNITY_4_5 || UNITY_4_4 || UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0 || UNITY_3_5 || UNITY_3_4 || UNITY_3_3 || UNITY_3_2 || UNITY_3_1 || UNITY_3_0_0 || UNITY_3_0 || UNITY_2_6_1 || UNITY_2_6)
  3. using System; // require keep for Windows Universal App
  4. using UnityEngine;
  5. namespace UniRx.Triggers
  6. {
  7. [DisallowMultipleComponent]
  8. public class ObservableStateMachineTrigger : StateMachineBehaviour
  9. {
  10. public class OnStateInfo
  11. {
  12. public Animator Animator { get; private set; }
  13. public AnimatorStateInfo StateInfo { get; private set; }
  14. public int LayerIndex { get; private set; }
  15. public OnStateInfo(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  16. {
  17. Animator = animator;
  18. StateInfo = stateInfo;
  19. LayerIndex = layerIndex;
  20. }
  21. }
  22. public class OnStateMachineInfo
  23. {
  24. public Animator Animator { get; private set; }
  25. public int StateMachinePathHash { get; private set; }
  26. public OnStateMachineInfo(Animator animator, int stateMachinePathHash)
  27. {
  28. Animator = animator;
  29. StateMachinePathHash = stateMachinePathHash;
  30. }
  31. }
  32. // OnStateExit
  33. Subject<OnStateInfo> onStateExit;
  34. public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  35. {
  36. if (onStateExit != null) onStateExit.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
  37. }
  38. public IObservable<OnStateInfo> OnStateExitAsObservable()
  39. {
  40. return onStateExit ?? (onStateExit = new Subject<OnStateInfo>());
  41. }
  42. // OnStateEnter
  43. Subject<OnStateInfo> onStateEnter;
  44. public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  45. {
  46. if (onStateEnter != null) onStateEnter.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
  47. }
  48. public IObservable<OnStateInfo> OnStateEnterAsObservable()
  49. {
  50. return onStateEnter ?? (onStateEnter = new Subject<OnStateInfo>());
  51. }
  52. // OnStateIK
  53. Subject<OnStateInfo> onStateIK;
  54. public override void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  55. {
  56. if(onStateIK !=null) onStateIK.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
  57. }
  58. public IObservable<OnStateInfo> OnStateIKAsObservable()
  59. {
  60. return onStateIK ?? (onStateIK = new Subject<OnStateInfo>());
  61. }
  62. // Does not implments OnStateMove.
  63. // ObservableStateMachine Trigger makes stop animating.
  64. // By defining OnAnimatorMove, you are signifying that you want to intercept the movement of the root object and apply it yourself.
  65. // http://fogbugz.unity3d.com/default.asp?700990_9jqaim4ev33i8e9h
  66. //// OnStateMove
  67. //Subject<OnStateInfo> onStateMove;
  68. //public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  69. //{
  70. // if (onStateMove != null) onStateMove.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
  71. //}
  72. //public IObservable<OnStateInfo> OnStateMoveAsObservable()
  73. //{
  74. // return onStateMove ?? (onStateMove = new Subject<OnStateInfo>());
  75. //}
  76. // OnStateUpdate
  77. Subject<OnStateInfo> onStateUpdate;
  78. public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  79. {
  80. if (onStateUpdate != null) onStateUpdate.OnNext(new OnStateInfo(animator, stateInfo, layerIndex));
  81. }
  82. public IObservable<OnStateInfo> OnStateUpdateAsObservable()
  83. {
  84. return onStateUpdate ?? (onStateUpdate = new Subject<OnStateInfo>());
  85. }
  86. // OnStateMachineEnter
  87. Subject<OnStateMachineInfo> onStateMachineEnter;
  88. public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
  89. {
  90. if (onStateMachineEnter != null) onStateMachineEnter.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash));
  91. }
  92. public IObservable<OnStateMachineInfo> OnStateMachineEnterAsObservable()
  93. {
  94. return onStateMachineEnter ?? (onStateMachineEnter = new Subject<OnStateMachineInfo>());
  95. }
  96. // OnStateMachineExit
  97. Subject<OnStateMachineInfo> onStateMachineExit;
  98. public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
  99. {
  100. if (onStateMachineExit != null) onStateMachineExit.OnNext(new OnStateMachineInfo(animator, stateMachinePathHash));
  101. }
  102. public IObservable<OnStateMachineInfo> OnStateMachineExitAsObservable()
  103. {
  104. return onStateMachineExit ?? (onStateMachineExit = new Subject<OnStateMachineInfo>());
  105. }
  106. }
  107. }
  108. #endif