AnimationData.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. private void Awake ()
  34. {
  35. aniEndcb = null;
  36. foreach (var clip in AnimationClips)
  37. {
  38. if (clip.Type >= AnimationType.Skill0 && clip.Type < AnimationType.SkillMax)
  39. {
  40. clip.Clip.Events.OnEnd = SkillEndCB;
  41. }
  42. }
  43. }
  44. protected void SkillEndCB()
  45. {
  46. if(notifyState != null /*&& notifyState.IsPlaying*/)
  47. {
  48. notifyState.Events = null;
  49. notifyState = null;
  50. }
  51. Action tocall = aniEndcb;
  52. aniEndcb = null;
  53. tocall?.Invoke();
  54. }
  55. public ClipTransition GetClip(AnimationType type)
  56. {
  57. foreach (var clip in AnimationClips)
  58. {
  59. if (clip.Type == type)
  60. {
  61. return clip.Clip;
  62. }
  63. }
  64. return null;
  65. }
  66. public void PlayAnimation(AnimationType type, Action endcb = null)
  67. {
  68. //Log.Debug($"{this.gameObject.name} toplay ani:{type}");
  69. aniEndcb = endcb;
  70. var clip = GetClip(type);
  71. if (clip != null)
  72. {
  73. notifyState = Animancer.Play(clip, 0.1f, FadeMode.FromStart);
  74. }
  75. else
  76. {
  77. //Log.Error($"Not exist clip({type}) @{gameObject.name}");
  78. }
  79. }
  80. }
  81. }