Scene_Anime_UnitControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using CrossgateToolkit;
  4. using UnityEngine;
  5. public class Scene_UnitControl : MonoBehaviour
  6. {
  7. [Header("单位列表")]
  8. [Header("跑步单位")] public Prefeb_Unit Unit_Runner;
  9. [Header("攻击单位")] public Prefeb_Unit Unit_Attacker;
  10. [Header("防御单位")] public Prefeb_Unit Unit_Defender;
  11. [Header("特效单位")] public Prefeb_Unit Unit_Effect;
  12. [Header("动作单位")] public Prefeb_Unit Unit_Action;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. }
  21. private void Awake()
  22. {
  23. Util.Init();
  24. }
  25. public void Play()
  26. {
  27. RunnerRun();
  28. AttackerRun();
  29. StartCoroutine(PlayControl());
  30. }
  31. private void RunnerRun()
  32. {
  33. // 动画队列一般用于对动画流程没有严格要求的场景
  34. // 动画播放过程中,如果某个动画数据缺失或不存在,则会自动跳过该动画并自动播放下一动画
  35. // 如果需要控制动画的具体播放时机和顺序或控制其他变量行为,建议使用动画回调监听方式处理
  36. // 以队列方式播放特殊三段式跑步动画
  37. Unit_Runner.animePlayer.Serial = 105000;
  38. Unit_Runner.animePlayer.DirectionType = Anime.DirectionType.South;
  39. Unit_Runner.animePlayer.playOnce(Unit_Runner.animePlayer.DirectionType,Anime.ActionType.BeforeRun)
  40. .nextPlay(Unit_Runner.animePlayer.DirectionType,Anime.ActionType.Run,Anime.PlayType.Once)
  41. .nextPlay(Unit_Runner.animePlayer.DirectionType,Anime.ActionType.AfterRun,Anime.PlayType.Once)
  42. .nextPlay(Unit_Runner.animePlayer.DirectionType,Anime.ActionType.Idle,Anime.PlayType.Loop);
  43. }
  44. private void AttackerRun()
  45. {
  46. // 动画帧事件监听
  47. // 普通的攻击动画正常会有击中和攻击完成两种事件回调
  48. // 如:小石像怪,有多段击中效果,最后一段击中则为攻击完成
  49. // 又如:冰冻魔法,有多段击中效果,最后一段击中则为攻击完成
  50. // 但可以肯定有部分动画因图档版本问题或打包过程中出现动作帧缺失的情况,则有可能无法触发击中或是攻击完成事件
  51. // 这时可能需要通过使用动画播放完成事件回调来处理
  52. Unit_Attacker.animePlayer.Serial = 101200;
  53. Unit_Attacker.animePlayer.DirectionType = Anime.DirectionType.SouthEast;
  54. Unit_Defender.animePlayer.Serial = 105052;
  55. Unit_Defender.animePlayer.DirectionType = Anime.DirectionType.NorthWest;
  56. Unit_Attacker.animePlayer.playOnce(Unit_Attacker.animePlayer.DirectionType, Anime.ActionType.Attack, 1f,
  57. (effectType) =>
  58. {
  59. // 动画事件回调
  60. // 击中或攻击结束效果时,出现被击中动画
  61. // Unit_Effect.animePlayer.Serial = 110001;
  62. Unit_Effect.animePlayer.play(110001,Anime.DirectionType.North,Anime.ActionType.Idle,Anime.PlayType.OnceAndDestroy);
  63. Unit_Effect.RectTransform.position = Unit_Defender.RectTransform.position + new Vector3(0, 0.5f, 0);
  64. // 击中效果
  65. if (effectType == Anime.EffectType.Hit)
  66. {
  67. // 对于多段攻击可以延迟下一帧的帧率以达到子弹时间或是其他效果
  68. Unit_Attacker.animePlayer.DelayPlay(0.5f);
  69. // 可以在击中效果中处理受击方的受击动画效果,同样也可以控制帧率速度倍率
  70. Unit_Defender.animePlayer.playOnce(Unit_Defender.animePlayer.DirectionType, Anime.ActionType.Hurt,
  71. 0.2f);
  72. // 可以给受击方增加抖动处理等等,这里不再做演示
  73. }
  74. // 攻击完成
  75. if (effectType == Anime.EffectType.HitOver)
  76. {
  77. // 攻击结束阶段可以处理实际的伤害数值显示等
  78. Unit_Defender.animePlayer.playOnce(Unit_Defender.animePlayer.DirectionType, Anime.ActionType.Hurt);
  79. }
  80. }, (act) =>
  81. {
  82. // 动画播放完成回调
  83. // 此处可以处理动画播放完成后的逻辑处理
  84. Unit_Attacker.animePlayer.ActionType = Anime.ActionType.Idle;
  85. });
  86. }
  87. IEnumerator PlayControl()
  88. {
  89. // 通过协程以及动画播放完成监听的方式控制动画播放
  90. // 也可通过非协程的多级回调方式控制动画播放,看具体应用场景和编码习惯
  91. bool runAble = false;
  92. Unit_Action.animePlayer.Serial = 106270;
  93. Unit_Action.animePlayer.DirectionType = Anime.DirectionType.SouthWest;
  94. Unit_Action.animePlayer.playOnce(Anime.DirectionType.SouthWest, Anime.ActionType.Rock, 1f, null,
  95. (act) => runAble = true);
  96. yield return new WaitUntil(() => runAble);
  97. yield return new WaitForSeconds(0.5f);
  98. runAble = false;
  99. Unit_Action.animePlayer.playOnce(Anime.DirectionType.SouthWest, Anime.ActionType.Scissors, 1f, null,
  100. (act) => runAble = true);
  101. yield return new WaitUntil(() => runAble);
  102. yield return new WaitForSeconds(0.5f);
  103. Unit_Action.animePlayer.playOnce(Anime.DirectionType.SouthWest, Anime.ActionType.Paper, 1f, null,
  104. (act) => runAble = true);
  105. }
  106. }