当前位置:   article > 正文

Unity——UGUI序列帧动画_ue umg序列帧制作

ue umg序列帧制作

 序列帧动画原理是首先必须要有一个载体,一般是一个图片,然后申请一个数组或List用来存放序列帧,然后再根据需要遍历这个数组替换载体的图片源,这样就实现动画效果了。代码如下:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using System;
  6. namespace Engine
  7. {
  8. [RequireComponent(typeof(Image))]
  9. public class SimpleFrameAnim : MonoBehaviour
  10. {
  11. private Image ImageSource;
  12. private int mCurFrame = 0;
  13. private float mDelta = 0;
  14. public float FPS = 20;
  15. public List<Sprite> SpriteFrames;
  16. public bool IsPlaying = false;
  17. public bool Foward = true;
  18. public bool AutoPlay = false;
  19. public bool Loop = false;
  20. private Action<object> animPlayEndAction = null;
  21. private object caller = null;
  22. public int FrameCount
  23. {
  24. get
  25. {
  26. return SpriteFrames.Count;
  27. }
  28. }
  29. void Awake()
  30. {
  31. ImageSource = GetComponent<Image>();
  32. }
  33. void Start()
  34. {
  35. if (AutoPlay)
  36. {
  37. Play();
  38. }
  39. else
  40. {
  41. IsPlaying = false;
  42. }
  43. }
  44. private void SetSprite(int idx)
  45. {
  46. ImageSource.sprite = SpriteFrames[idx];
  47. //该部分为设置成原始图片大小,如果只需要显示Image设定好的图片大小,注释掉该行即可。
  48. ImageSource.SetNativeSize();
  49. }
  50. public void Play()
  51. {
  52. IsPlaying = true;
  53. Foward = true;
  54. }
  55. public void PlayReverse()
  56. {
  57. IsPlaying = true;
  58. Foward = false;
  59. }
  60. void Update()
  61. {
  62. if (!IsPlaying || 0 == FrameCount)
  63. {
  64. return;
  65. }
  66. mDelta += Time.deltaTime;
  67. if (mDelta > 1 / FPS)
  68. {
  69. mDelta = 0;
  70. if(Foward)
  71. {
  72. mCurFrame++;
  73. }
  74. else
  75. {
  76. mCurFrame--;
  77. }
  78. if (mCurFrame >= FrameCount)
  79. {
  80. if (Loop)
  81. {
  82. mCurFrame = 0;
  83. }
  84. else
  85. {
  86. IsPlaying = false;
  87. if (animPlayEndAction != null)
  88. animPlayEndAction.Invoke(caller);
  89. return;
  90. }
  91. }
  92. else if (mCurFrame<0)
  93. {
  94. if (Loop)
  95. {
  96. mCurFrame = FrameCount-1;
  97. }
  98. else
  99. {
  100. IsPlaying = false;
  101. if (animPlayEndAction != null)
  102. animPlayEndAction.Invoke(caller);
  103. return;
  104. }
  105. }
  106. SetSprite(mCurFrame);
  107. }
  108. }
  109. public void Pause()
  110. {
  111. IsPlaying = false;
  112. }
  113. public void Resume()
  114. {
  115. if (!IsPlaying)
  116. {
  117. IsPlaying = true;
  118. }
  119. }
  120. public void Stop()
  121. {
  122. mCurFrame = 0;
  123. SetSprite(mCurFrame);
  124. IsPlaying = false;
  125. }
  126. public void Rewind()
  127. {
  128. mCurFrame = 0;
  129. SetSprite(mCurFrame);
  130. Play();
  131. }
  132. public void AddAnimPlayEndListener(Action<object> aciton, object caller)
  133. {
  134. animPlayEndAction = aciton;
  135. this.caller = caller;
  136. }
  137. }
  138. }

相比于转载的文章加入了回调函数,可以在 Lua中获取该组件并添加回调方法。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/138250
推荐阅读
相关标签
  

闽ICP备14008679号