AnimatorComponent.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame;
  5. using XGame.Database;
  6. using XGame.Framework.Components;
  7. using XGame.Framework.Interfaces;
  8. using XGame.Framework.Time;
  9. namespace FL.Battle.Components
  10. {
  11. public interface IAnimatorContext
  12. {
  13. Animator Animator { get; }
  14. ITimeModule Time { get; }
  15. }
  16. public class AnimatorComponent : Component<IAnimatorContext>, IAnimator, IReset
  17. {
  18. /// <summary>
  19. /// Animator参数名
  20. /// int类型,数值参考EActionName
  21. /// </summary>
  22. private string STATE_VAL = "StateVal";
  23. private ITimer _actionTimer;
  24. /// <summary>
  25. /// 触发器的定时器
  26. /// </summary>
  27. private ITimer _triggerTimer;
  28. private Dictionary<EAnimationName, AnimationClip> _nameToClipMap;
  29. private float _baseSpeed = 1;
  30. protected override void OnEnable(object intent)
  31. {
  32. _baseSpeed = Context.Animator.speed;
  33. RefreshClips();
  34. }
  35. protected override void OnDisable()
  36. {
  37. _nameToClipMap?.Clear();
  38. Context.Animator.speed = _baseSpeed;
  39. ClearTimers();
  40. }
  41. protected override void OnDispose()
  42. {
  43. ClearTimers();
  44. }
  45. private void ClearTimers()
  46. {
  47. _actionTimer?.Cancel();
  48. _actionTimer = null;
  49. _triggerTimer?.Cancel();
  50. _triggerTimer = null;
  51. }
  52. public void Play(EAnimationName aniName)
  53. {
  54. Play(new AnimationPlayArgs { aniName = aniName });
  55. }
  56. public void Play(AnimationPlayArgs args)
  57. {
  58. var animator = Context.Animator;
  59. if (!_nameToClipMap.TryGetValue(args.aniName, out var clip))
  60. {
  61. Log.Error($"找不到动作配置. Animator:{animator.name} ActionName:{args.aniName}");
  62. return;
  63. }
  64. //if (_actionTimer != null)
  65. //{
  66. // Log.Debug($"Animator Name:{animator.name} Now:{animator.GetInteger(STATE_VAL)} Next:{args.aniName}");
  67. //}
  68. animator.SetInteger(STATE_VAL, (int)args.aniName);
  69. ClearTimers();
  70. var duration = Mathf.CeilToInt(clip.length * 1000);
  71. if (args.onTrigger != null && args.triggerFrame > 0 && args.triggerFrame < duration)
  72. {
  73. _triggerTimer = Context.Time.AddDelayTimer(args.triggerFrame, () =>
  74. {
  75. _triggerTimer = null;
  76. args.onTrigger.SafeInvoke();
  77. });
  78. }
  79. if (!clip.isLooping)
  80. { // 非循环动作才有回调
  81. //Log.Debug($"Animator Start name:{args.aniName} clip:{clip.name} duration:{duration} time:{Time.realtimeSinceStartup} rotation:{animator.transform.localRotation}");
  82. _actionTimer = Context.Time.AddDelayTimer(duration, () =>
  83. {
  84. //Log.Debug($"Animator End name:{args.aniName} clip:{clip.name} duration:{duration} time:{Time.realtimeSinceStartup} rotation:{animator.transform.localRotation}");
  85. _actionTimer = null;
  86. animator.SetInteger(STATE_VAL, (int)args.fallback);
  87. args.onCompleted.SafeInvoke();
  88. });
  89. }
  90. //state.Complete += (_) =>
  91. //{
  92. // callback?.Invoke();
  93. //};
  94. }
  95. private void RefreshClips()
  96. {
  97. if (_nameToClipMap == null)
  98. {
  99. _nameToClipMap = new Dictionary<EAnimationName, AnimationClip>();
  100. }
  101. var controller = Context.Animator.runtimeAnimatorController;
  102. if (controller != null && controller.animationClips != null)
  103. {
  104. foreach (var clip in controller.animationClips)
  105. {
  106. if (Enum.TryParse<EAnimationName>(clip.name, out var actionName))
  107. {
  108. _nameToClipMap.Add(actionName, clip);
  109. }
  110. else
  111. {
  112. Log.Error($"AnimationClip 命名错误. Animator:{Context.Animator.name} Clip:{clip.name}");
  113. }
  114. }
  115. }
  116. //foreach (var parameter in Context.Animator.parameters)
  117. //{
  118. // var id = parameter.nameHash;
  119. // Context.Animator.SetBool(id, true);
  120. //}
  121. }
  122. void IReset.Reset()
  123. {
  124. ClearTimers();
  125. }
  126. }
  127. }