AnimationData.cs 2.6 KB

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