AnimationData.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using ET;
  2. using Animancer;
  3. using UnityEngine;
  4. using System;
  5. using NLog.Targets.Wrappers;
  6. namespace Mono
  7. {
  8. public class AnimationData : MonoBehaviour
  9. {
  10. public AnimancerComponent Animancer;
  11. public enum AnimationType
  12. {
  13. Idle,
  14. Run,
  15. Dead,
  16. Damage,
  17. Skill0,
  18. Skill1,
  19. Skill2,
  20. Skill3,
  21. Skill4,
  22. Skill5,
  23. Skill6,
  24. SkillMax
  25. }
  26. [Serializable]
  27. public struct AniInfo
  28. {
  29. public AnimationType Type;
  30. public ClipTransition Clip;
  31. }
  32. public AniInfo[] AnimationClips = new AniInfo[1];
  33. private Action aniEndcb;
  34. private AnimancerState notifyState;
  35. public bool IsPauseStatus() { return !Animancer.Playable.IsGraphPlaying; }
  36. private void Awake ()
  37. {
  38. aniEndcb = null;
  39. foreach (var clip in AnimationClips)
  40. {
  41. if (clip.Type >= AnimationType.Skill0 && clip.Type < AnimationType.SkillMax)
  42. {
  43. clip.Clip.Events.OnEnd = SkillEndCB;
  44. }
  45. }
  46. }
  47. protected void SkillEndCB()
  48. {
  49. if(notifyState != null /*&& notifyState.IsPlaying*/)
  50. {
  51. notifyState.Events = null;
  52. notifyState = null;
  53. }
  54. Action tocall = aniEndcb;
  55. aniEndcb = null;
  56. tocall?.Invoke();
  57. }
  58. public ClipTransition GetClip(AnimationType type)
  59. {
  60. foreach (var clip in AnimationClips)
  61. {
  62. if (clip.Type == type)
  63. {
  64. return clip.Clip;
  65. }
  66. }
  67. return null;
  68. }
  69. public void PlayAnimation(AnimationType type, Action endcb = null)
  70. {
  71. if (!Animancer.Playable.IsGraphPlaying)
  72. {
  73. Log.Error( $"play ani({type}) @ froze status" );
  74. return;
  75. }
  76. Log.Debug($"{this.gameObject.name} toplay ani:{type}");
  77. aniEndcb = endcb;
  78. var clip = GetClip(type);
  79. if (clip != null)
  80. {
  81. notifyState = Animancer.Play(clip, 0.1f, FadeMode.FromStart);
  82. }
  83. else
  84. {
  85. Log.Error($"Not exist clip({type}) @{gameObject.name}");
  86. }
  87. }
  88. public void Pause()
  89. {
  90. Animancer.Playable.PauseGraph();
  91. }
  92. public void Resume()
  93. {
  94. Animancer.Playable.UnpauseGraph();
  95. }
  96. }
  97. }