FPlayAnimationEvent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Flux
  4. {
  5. //[FEvent("Animation/Play Animation", typeof(FAnimationTrack))]
  6. public class FPlayAnimationEvent : FEvent
  7. {
  8. [HideInInspector]
  9. public AnimationClip _animationClip = null;
  10. [HideInInspector]
  11. [SerializeField]
  12. private bool _controlsAnimation = false;
  13. public bool ControlsAnimation { get { return _controlsAnimation; } set { _controlsAnimation = FUtility.IsAnimationEditable(_animationClip) && value; } }
  14. [HideInInspector]
  15. [Tooltip("How long is the transition blending?")]
  16. public int _blendLength;
  17. [HideInInspector]
  18. [Tooltip("What's the offset from where we play the animation?")]
  19. public int _startOffset;
  20. [HideInInspector]
  21. public int _stateHash;
  22. private Animator _animator = null;
  23. private FAnimationTrack _animTrack = null;
  24. // protected override void OnInit()
  25. // {
  26. // base.OnInit();
  27. // }
  28. protected override void OnTrigger( float timeSinceTrigger )
  29. {
  30. _animator = Owner.GetComponent<Animator>();
  31. _animTrack = (FAnimationTrack)_track;
  32. if( _animator.runtimeAnimatorController != _animTrack.AnimatorController )
  33. {
  34. _animator.runtimeAnimatorController = _animTrack.AnimatorController;
  35. }
  36. _animator.enabled = _animationClip != null;
  37. int id = GetId();
  38. #if UNITY_EDITOR
  39. if( _animator.enabled && (!_track.HasCache || Application.isPlaying) )
  40. #else
  41. if( _animator.enabled )
  42. #endif
  43. {
  44. // Debug.Log( "Turning on " + _animTrack.LayerName );
  45. _animator.SetLayerWeight(_animTrack.LayerId, 1);
  46. if( id == 0 || _track.Events[id-1].End < Start )
  47. {
  48. _animator.Play( _stateHash, _animTrack.LayerId, (_startOffset * Sequence.InverseFrameRate) / _animationClip.length );
  49. }
  50. if( timeSinceTrigger > 0 )
  51. {
  52. // - 0.001f because if you pass the length of the animation
  53. // it seems that it will go over it and miss the condition
  54. _animator.Update( timeSinceTrigger-0.001f );
  55. }
  56. else
  57. _animator.Update( 0f );
  58. }
  59. }
  60. protected override void OnUpdateEvent( float timeSinceTrigger )
  61. {
  62. if( !_animator.enabled )
  63. _animator.enabled = true;
  64. }
  65. protected override void OnFinish()
  66. {
  67. if( _animator && (IsLastEvent || _track.GetEvent(GetId()+1).Start != End))
  68. {
  69. _animator.enabled = false;
  70. _animator.SetLayerWeight(_animTrack.LayerId, 0);
  71. }
  72. }
  73. protected override void OnStop()
  74. {
  75. int id = GetId();
  76. if( _animator && (id == 0 || _track.GetEvent( id - 1 ).End != Start ) )
  77. {
  78. _animator.SetLayerWeight(_animTrack.LayerId, 0);
  79. _animator.enabled = false;
  80. }
  81. }
  82. protected override void OnPause()
  83. {
  84. _animator.enabled = false;
  85. }
  86. protected override void OnResume()
  87. {
  88. _animator.enabled = true;
  89. }
  90. public override int GetMaxLength()
  91. {
  92. if( FUtility.IsAnimationEditable( _animationClip ) || _animationClip.isLooping )
  93. return base.GetMaxLength();
  94. return Mathf.RoundToInt(_animationClip.length * _animationClip.frameRate - _startOffset);
  95. }
  96. public override string Text {
  97. get {
  98. return _animationClip == null ? "!Missing!" : _animationClip.name;
  99. }
  100. set {
  101. // cannot set
  102. }
  103. }
  104. public bool IsBlending()
  105. {
  106. int id = GetId();
  107. return id > 0 && _track != null && _track.Events[id-1].End == Start && ((FAnimationTrack)_track).AnimatorController != null && ((FPlayAnimationEvent)_track.Events[id-1])._animationClip != null && _animationClip != null ;
  108. }
  109. // public bool IsAnimationEditable()
  110. // {
  111. // return _animationClip == null || (((_animationClip.hideFlags & HideFlags.NotEditable) == 0) && !_animationClip.isLooping);
  112. // }
  113. public int GetMaxStartOffset()
  114. {
  115. if( _animationClip == null )
  116. return 0;
  117. return _animationClip.isLooping ? Length : Mathf.RoundToInt(_animationClip.length * _animationClip.frameRate) - Length;
  118. }
  119. }
  120. }