当前位置:   article > 正文

基于LeanTween的封装_leantween.value

leantween.value

灵感来自于cocos2d-x的动画类。

完整代码:

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. //动画基类
  5. abstract public class WQPAnimation
  6. {
  7. public WQPAnimation()
  8. {
  9. }
  10. public void play()
  11. {
  12. internalPlay();
  13. }
  14. public static void cancel(GameObject obj)
  15. {
  16. LeanTween.cancel (obj);
  17. }
  18. abstract protected void internalPlay();
  19. public Callback _finishCallback;
  20. public GameObject target;
  21. public void finishCallback()
  22. {
  23. if(_finishCallback != null)
  24. {
  25. _finishCallback();
  26. }
  27. }
  28. }
  29. //等待
  30. public class WQPADelay : WQPAnimation
  31. {
  32. private float delayTime;
  33. public WQPADelay(GameObject obj, float delay)
  34. {
  35. target = obj;
  36. delayTime = delay;
  37. }
  38. override protected void internalPlay()
  39. {
  40. LeanTween.delayedCall(target, delayTime, internalFinishCallback);
  41. }
  42. void internalFinishCallback(object obj)
  43. {
  44. finishCallback();
  45. }
  46. }
  47. //移动
  48. public class WQPAMoveTo : WQPAnimation
  49. {
  50. public WQPAMoveTo(GameObject obj, Vector3 v, float t)
  51. {
  52. if(obj == null)
  53. {
  54. obj = new GameObject();
  55. }
  56. target = obj;
  57. to = v;
  58. time = t;
  59. }
  60. private Vector3 to;
  61. private float speed = 1.0f;
  62. private float time;
  63. override protected void internalPlay()
  64. {
  65. LeanTween.moveLocal(target, to, time).setOnComplete(internalCallback);
  66. }
  67. void internalCallback(object obj)
  68. {
  69. finishCallback();
  70. }
  71. }
  72. //旋转
  73. public class WQPARotateTo : WQPAnimation
  74. {
  75. public WQPARotateTo(GameObject obj, Vector3 t)
  76. {
  77. if(obj == null)
  78. {
  79. obj = new GameObject();
  80. }
  81. target = obj;
  82. to = t;
  83. }
  84. private Vector3 to;
  85. override protected void internalPlay()
  86. {
  87. float time = 0.5f;
  88. LeanTween.rotate(target, to, time).setOnComplete(internalCallback);
  89. }
  90. void internalCallback(object obj)
  91. {
  92. finishCallback();
  93. }
  94. }
  95. //回调
  96. public class WQPACallback : WQPAnimation
  97. {
  98. private Callback callback;
  99. public WQPACallback(Callback call)
  100. {
  101. callback = call;
  102. }
  103. override protected void internalPlay()
  104. {
  105. callback ();
  106. finishCallback ();
  107. }
  108. }
  109. //value (透明度)
  110. public class WQPValue : WQPAnimation
  111. {
  112. private GameObject target;
  113. private Action<float> callback;
  114. private float from, to, time;
  115. public WQPValue(GameObject obj, Action<float> call, float from, float to, float time)
  116. {
  117. target = obj;
  118. callback = call;
  119. this.from = from;
  120. this.to = to;
  121. this.time = time;
  122. }
  123. override protected void internalPlay()
  124. {
  125. LeanTween.value (target, callback, from, to, time).setOnComplete(internalCallback);
  126. }
  127. void internalCallback(object obj)
  128. {
  129. finishCallback();
  130. }
  131. }
  132. //顺序
  133. public class WQPASequence : WQPAnimation
  134. {
  135. ArrayList animArray;
  136. int playIndex = 0;
  137. public WQPASequence()
  138. {
  139. animArray = new ArrayList();
  140. playIndex = 0;
  141. }
  142. public void addAnim(WQPAnimation anim)
  143. {
  144. animArray.Add(anim);
  145. }
  146. override protected void internalPlay()
  147. {
  148. playIndex = 0;
  149. playNext();
  150. }
  151. void playNext()
  152. {
  153. if(playIndex >= animArray.Count)
  154. {
  155. finishCallback();
  156. }
  157. else
  158. {
  159. WQPAnimation anim = (WQPAnimation)animArray[playIndex];
  160. playIndex ++;
  161. anim._finishCallback = internalFinishCallback;
  162. anim.play();
  163. }
  164. }
  165. void internalFinishCallback()
  166. {
  167. playNext();
  168. }
  169. }
  170. //同时
  171. public class WQPASpawn : WQPAnimation
  172. {
  173. ArrayList animArray;
  174. int playingAnimCount = 0;
  175. public WQPASpawn()
  176. {
  177. animArray = new ArrayList();
  178. playingAnimCount = 0;
  179. }
  180. public void addAnim(WQPAnimation anim)
  181. {
  182. animArray.Add(anim);
  183. }
  184. override protected void internalPlay()
  185. {
  186. playingAnimCount = animArray.Count;
  187. if(playingAnimCount == 0)
  188. {
  189. finishCallback();
  190. return;
  191. }
  192. for(int i = 0; i < animArray.Count; ++i)
  193. {
  194. WQPAnimation anim = (WQPAnimation)animArray[i];
  195. anim._finishCallback = internalFinishCallback;
  196. anim.play();
  197. }
  198. }
  199. void internalFinishCallback()
  200. {
  201. playingAnimCount --;
  202. if(playingAnimCount < 0)
  203. {
  204. Debug.LogError("Spawn : playing anim count < 0");
  205. }
  206. if(playingAnimCount == 0)
  207. {
  208. finishCallback();
  209. }
  210. }
  211. }
  212. //循环
  213. public class WQPARepeat : WQPAnimation
  214. {
  215. WQPAnimation m_anim;
  216. int repeatCount;
  217. public WQPARepeat(WQPAnimation anim, int c)
  218. {
  219. m_anim = anim;
  220. repeatCount = c;
  221. }
  222. override protected void internalPlay()
  223. {
  224. m_anim._finishCallback = internalFinishCallback;
  225. m_anim.play ();
  226. }
  227. void internalFinishCallback()
  228. {
  229. if(repeatCount < 0)
  230. {
  231. internalPlay();
  232. }
  233. else
  234. {
  235. repeatCount--;
  236. if(repeatCount > 0)
  237. {
  238. internalPlay();
  239. }
  240. }
  241. }
  242. }


LeanTween的下载可以网上搜索,这里我给一个我上面用的。

链接: http://pan.baidu.com/s/1o84KqRw 密码: jmqh


例子:

  1. //0.5秒渐现-显示1.5秒-消失0.5秒-间隔0.5秒-0.5秒渐现-显示1.5秒-消失0.5秒-间隔9.5秒
  2. void AnimBegin()
  3. {
  4. WQPASequence sequ = new WQPASequence ();
  5. WQPValue value1 = new WQPValue (gameObject, ObjValue, 0f, 1f, 0.5f);
  6. WQPADelay del1 = new WQPADelay (gameObject, 1.5f);
  7. WQPValue value2 = new WQPValue (gameObject, ObjValue, 1f, 0f, 0.5f);
  8. WQPADelay del2 = new WQPADelay (gameObject, 0.5f);
  9. WQPValue value3 = new WQPValue (gameObject, ObjValue, 0f, 1f, 0.5f);
  10. WQPADelay del3 = new WQPADelay (gameObject, 1.5f);
  11. WQPValue value4 = new WQPValue (gameObject, ObjValue, 1f, 0f, 0.5f);
  12. WQPADelay del4 = new WQPADelay (gameObject, 9.5f);
  13. sequ.addAnim (value1);
  14. sequ.addAnim (del1);
  15. sequ.addAnim (value2);
  16. sequ.addAnim (del2);
  17. sequ.addAnim (value3);
  18. sequ.addAnim (del3);
  19. sequ.addAnim (value4);
  20. sequ.addAnim (del4);
  21. WQPARepeat rep = new WQPARepeat (sequ, -1);
  22. rep.play ();
  23. }



是不是很方便。。。

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

闽ICP备14008679号