FPlayAnimEvent.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Security;
  4. namespace Flux
  5. {
  6. [FEvent("Skill/动作相关/播放技能动作")]
  7. public class FPlayAnimEvent : FEvent
  8. {
  9. [HideInInspector]
  10. [SerializeField]
  11. private string _animName;
  12. [HideInInspector]
  13. [SerializeField]
  14. private int _animSpeed = 100;
  15. public string AnimName
  16. {
  17. get { return _animName; }
  18. set { _animName = value; }
  19. }
  20. public int AnimSpeed
  21. {
  22. get { return _animSpeed; }
  23. set { _animSpeed = value; }
  24. }
  25. private Animator _animator = null;
  26. public Animator animator
  27. {
  28. get { return _animator; }
  29. }
  30. private float _previousTimeSinceTrigger = 0;
  31. private float _previousSpeed = 0;
  32. public FPlayAnimEvent()
  33. {
  34. _eventType = SkillActionFrameEventType.FE_PlaySkillAnim;
  35. }
  36. protected override void OnSetDefaultValues()
  37. {
  38. base.OnSetDefaultValues();
  39. if (_casterTrans != null)
  40. {
  41. _animator = _casterTrans.gameObject.GetComponent<Animator>();
  42. }
  43. }
  44. protected override void OnInit()
  45. {
  46. if(_animator!=null)
  47. _animator.enabled = false;
  48. }
  49. protected override void OnTrigger(float timeSinceTrigger)
  50. {
  51. if (_animator != null)
  52. {
  53. _animator.enabled = true;
  54. int id = GetId();
  55. #if UNITY_EDITOR
  56. if (_animator.enabled && (!_track.HasCache || Application.isPlaying))
  57. #else
  58. if( _animator.enabled )
  59. #endif
  60. {
  61. _animator.SetLayerWeight(0, 1);
  62. if (id == 0 || _track.Events[id - 1].End < Start)
  63. {
  64. _animator.Play(_animName, 0);
  65. }
  66. if (timeSinceTrigger > 0)
  67. {
  68. // - 0.001f because if you pass the length of the animation
  69. // it seems that it will go over it and miss the condition
  70. _animator.Update(timeSinceTrigger - 0.001f);
  71. }
  72. else
  73. _animator.Update(0f);
  74. }
  75. }
  76. _previousTimeSinceTrigger = 0;
  77. _previousSpeed = Sequence.Speed;
  78. }
  79. protected override void OnPause()
  80. {
  81. if (_animator != null)
  82. _animator.enabled = false;
  83. }
  84. protected override void OnResume()
  85. {
  86. if (_animator != null)
  87. _animator.enabled = true;
  88. }
  89. protected override void OnFinish()
  90. {
  91. if (_animator && (IsLastEvent || _track.GetEvent(GetId() + 1).Start != End))
  92. {
  93. _animator.SetLayerWeight(0, 0);
  94. _animator.enabled = false;
  95. }
  96. }
  97. protected override void OnStop()
  98. {
  99. int id = GetId();
  100. if (_animator && (id == 0 || _track.GetEvent(id - 1).End != Start))
  101. {
  102. _animator.Play("f_idle", 0);
  103. _animator.Update(0);
  104. }
  105. }
  106. protected override void OnUpdateEvent(float timeSinceTrigger)
  107. {
  108. float delta = timeSinceTrigger - _previousTimeSinceTrigger;
  109. if (_animator != null)
  110. {
  111. if (!_animator.enabled)
  112. _animator.enabled = true;
  113. if(delta > 0)
  114. {
  115. float moveSpeed = _animSpeed * 0.01f;
  116. _animator.SetFloat("moveSpeed", moveSpeed);
  117. _animator.Update(delta);
  118. _previousTimeSinceTrigger = timeSinceTrigger;
  119. }
  120. else if(delta < 0)
  121. {
  122. _animator.SetFloat("moveSpeed", -1);
  123. _animator.Update(-delta);
  124. _previousTimeSinceTrigger = timeSinceTrigger;
  125. }
  126. }
  127. }
  128. public override int GetMaxLength()
  129. {
  130. return base.GetMaxLength();
  131. }
  132. public override string Text
  133. {
  134. get { return "播放动作"; }
  135. set { }
  136. }
  137. public override SecurityElement SaveToXml()
  138. {
  139. SecurityElement node = base.SaveToXml();
  140. SecurityElement paramNode = WriteParamNode("animName", _animName, "string");
  141. if (paramNode != null)
  142. node.AddChild(paramNode);
  143. SecurityElement speedNode = WriteParamNode("animSpeed", _animSpeed.ToString(), "int");
  144. if (speedNode != null)
  145. node.AddChild(speedNode);
  146. return node;
  147. }
  148. public override void LoadFromXml(SecurityElement eventNode)
  149. {
  150. base.LoadFromXml(eventNode);
  151. _animName = GetSParam("animName");
  152. _animSpeed = GetNParam("animSpeed");
  153. if(_animSpeed == 0)
  154. {
  155. _animSpeed = 100;
  156. }
  157. }
  158. }
  159. }