当前位置:   article > 正文

Unity3d DOTween 插件 详细使用方法_unity dotween update

unity dotween update

Tween的移动类型有很多种,比如匀速运动、加速运动、减速运动,等等。如果你拿捏不准你需要用什么移动类形式。

http://www.robertpenner.com/easing/easing_demo.html 你可以在这里预览一下那种移动类型更佳适合你。

  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening; //  别忘记引入
  4. using UnityEngine.UI;
  5. public class TestDoTween : MonoBehaviour {
  6. int number = 0;
  7. // Use this for initialization
  8. void Start ()
  9. {
  10. //FunctionOne();
  11. //FunctionTwo();
  12. //FunctionSequence();
  13. FunctionSet();
  14. }
  15. // 创建 DOTween 实例 方法
  16. #region 方法一 类方法
  17. private void FunctionOne()
  18. {
  19. // 创建一个 Tweener 是自身坐标 一秒内 移动到 坐标 Vector3(5, 5, 5) 位置
  20. Tween tween = DOTween.To(() => transform.position, r => transform.position = r, new Vector3(5, 5, 5), 1);
  21. // 创建一个 Tweener 对象, 另 number的值在 5 秒内变化到 100
  22. Tween t = DOTween.To(() => number, x => number = x, 100, 5);
  23. // 给执行 t 变化时,每帧回调一次 UpdateTween 方法
  24. t.OnUpdate( () => UpdateTween(number));
  25. }
  26. private void UpdateTween(int num)
  27. {
  28. Debug.Log(num); // 变化过程中, 每帧回调该方法
  29. }
  30. #endregion
  31. #region 方法二 直接调用
  32. private void FunctionTwo()
  33. {
  34. //十秒内X,Y,Z 局部坐标(localPosition)移动到 10,10,10 位置
  35. //transform.DOBlendableLocalMoveBy(new Vector3(10, 10, 10), 10);
  36. //十秒内 X,Y,Z 方向的局部旋转(localPosition),转动到 30,30,30
  37. //transform.DOBlendableLocalRotateBy(new Vector3(30, 30, 30), 10);
  38. //十秒内X,Y,Z坐标移动到 自身坐标 加 new Vector3( 10,10,10) 位置 原始 坐标 3,3,3,移动后 13,13,13
  39. //transform.DOBlendableMoveBy(new Vector3(10, 10, 10), 10);
  40. //十秒内X,Y,Z自身旋转到 30,30,30(有父物体的相对于父物体)
  41. //transform.DOBlendableRotateBy(new Vector3(30, 30, 30), 10);
  42. //十秒内 自身X,Y,Z方向的比例 加 3,3,3如原始比例 2,1,1 变化后5,4,4
  43. //transform.DOBlendableScaleBy(new Vector3(10, 10, 10), 10);
  44. //执行该方法,变化立即结束,且完成移动
  45. //transform.DOComplete();
  46. //在变化过程中执行该方法,则物体慢慢的变回原样,如果变化已经完成,该方法无效
  47. //transform.DOFlip();
  48. // 变化过程中执行该方法,则物体变化到 第二秒 时该物体的位置、比例等
  49. //transform.DOGoto(2);
  50. //十秒内 弹跳 3次
  51. //transform.DOJump(new Vector3(10, 10, 10), 3, 10);
  52. //停止掉当前的变化
  53. //transform.DOKill();
  54. // 十秒内 弹跳 3次, 局部坐标最终变化为 10, 0, 10
  55. //transform.DOLocalJump(new Vector3(10, 10, 10), 3, 10);
  56. // 5 秒内, 局部坐标变化到 10,10,10
  57. //transform.DOLocalMove(new Vector3(10, 10, 10), 5);
  58. // 10 秒内 X 局部坐标变换到 5
  59. //transform.DOLocalMoveX(5, 10);
  60. // 10 秒内 Y 局部坐标变化到 5
  61. //transform.DOLocalMoveY(5, 10);
  62. //10 秒内 Z 局部坐标变化到 5
  63. //transform.DOLocalMoveZ(5, 10);
  64. //transform.DOLocalPath();
  65. //5 秒内 局部旋转变化到 10,10, 10
  66. //transform.DOLocalRotate(new Vector3(10, 10, 10), 5);
  67. // 自身朝向 坐标(10,10,10)
  68. //transform.DOLookAt(new Vector3(10, 10, 10), 5);
  69. // 5 秒内 移动到 坐标 (10,10,10)
  70. //transform.DOMove(new Vector3(10, 10, 10), 5);
  71. //10 秒内 X 局部坐标变化到 5
  72. //transform.DOMoveX(5, 10);
  73. //10 秒内 Y 局部坐标变化到 5
  74. //transform.DOMoveY(5, 10);
  75. //10 秒内 Z 局部坐标变化到 5
  76. //transform.DOMoveZ(5, 10);
  77. //
  78. //transform.DOPath();
  79. //执行该方法停止 变化
  80. //transform.DOPause();
  81. //transform.DOPlay();
  82. //变化结束前调用该方法,物体回到原始位置
  83. //transform.DOPlayBackwards();
  84. //执行 transform.DOPlayBackwards(); 物体回到原始位置
  85. //执行 下面方法则再次变化
  86. //transform.DOPlayForward();
  87. //冲压机,在 5 秒内在原始坐标和下面坐标之间,来回冲压
  88. //transform.DOPunchPosition(new Vector3(10, 10, 10), 5);
  89. //冲压机,在 5 秒内在原始旋转和下面角度之间,来回冲压变化
  90. //transform.DOPunchRotation(new Vector3(50, 50, 50), 5);
  91. //冲压机,在 5 秒内在原始比例和下面比例之间,来回冲压变化
  92. //transform.DOPunchScale(new Vector3(5, 5, 5), 5);
  93. //在变化结束之前,执行该方法,则重新开始变化
  94. //transform.DORestart();
  95. //变化过程中执行该方法,回到原始
  96. //transform.DORewind();
  97. // 10 秒内 旋转角度 到 (50,50,50)
  98. //transform.DORotate(new Vector3(50, 50, 50), 5);
  99. // 10 秒内 比例变化到 (5,5,5)
  100. //transform.DOScale(new Vector3(5, 5, 5), 5);
  101. // 10 秒内 X 比例变化到 5
  102. //transform.DOScaleX(5, 10);
  103. // 10 秒内 Y 比例变化到 5
  104. //transform.DOScaleY(5, 10);
  105. // 10 秒内 Z 比例变化到 5
  106. //transform.DOScaleZ(5, 10);
  107. // 10 秒内 物体 X,Y,Z 坐标在 自身-5 到 自身加 5 之间震动
  108. //transform.DOShakePosition(10, new Vector3(10, 10, 10));
  109. // 10 秒内, 物体 X,Y,Z 旋转角度在 自身-5 到 自身加 5 之间震动
  110. //transform.DOShakeRotation(10, new Vector3(10, 10, 10));
  111. // 10 秒内, 物体 X,Y,Z 比例在 自身-5 到 自身加 5 之间震动
  112. //transform.DOShakeScale(10, new Vector3(10, 10, 10));
  113. //在变化过程中执行该方法,停止、开始、停止、开始
  114. //transform.DOTogglePause();
  115. // 执行该方法,坐标立即变化为 0,5,0, 从 0,5,0 两秒移动到初始位置
  116. //transform.DOMove(new Vector3(0, 5, 0), 2).From();
  117. // 执行该方法,移动到相对于原始位置 6,0,2 的位置
  118. // 如原始位置 3,2,1。 移动后位置为 3+6,2+0,2+2 即 9,2,4
  119. //transform.DOMove(new Vector3(6, 0, 2), 2).SetRelative();
  120. }
  121. #endregion
  122. #region DOTween回调方法 以 On 开头
  123. private void CallBack()
  124. {
  125. // 创建一个 Tweener 对象, 另 number的值在 5 秒内变化到 100
  126. Tween t = DOTween.To(() => number, x => number = x, 100, 5);
  127. // DOTween 可以连续调用 N 次方法如下
  128. // 执行变化的过程中可以回调的方法
  129. // 下面是不带参数的回调方法
  130. t.OnStart(OnStartTween).OnKill(OnKill).OnPause(OnPause).OnPlay(OnPlay).OnRewind(OnRewind);
  131. t.OnStepComplete(OnStepComplete).OnUpdate(UpdateTweenEd).OnComplete(OnComplete);
  132. // 带参数的回调方法如下
  133. t.OnUpdate(() => UpdateTweenED(number));
  134. }
  135. private void UpdateTweenED(int num)
  136. {
  137. Debug.Log("num " + num);
  138. }
  139. private void OnComplete()
  140. {
  141. Debug.Log("OnComplete"); // 完成变化时回调
  142. }
  143. private void OnKill()
  144. {
  145. Debug.Log("OnKill"); // 执行结束后自动杀死,回调一次
  146. }
  147. private void OnPlay()
  148. {
  149. Debug.Log("OnPlay"); // 开始执行调用一次, 该方法在 OnStart 方法后调用
  150. }
  151. private void OnPause()
  152. {
  153. Debug.Log("OnPause"); // 暂停回调
  154. }
  155. private void OnRewind()
  156. {
  157. Debug.Log("OnRewind"); //倒回回调
  158. }
  159. private void OnStartTween()
  160. {
  161. Debug.Log("StartTween"); // 开始执行最先回调, 该方法在 OnPlay 方法前调用
  162. }
  163. private void OnStepComplete()
  164. {
  165. Debug.Log("OnStepComplete"); // 如果循环的,每循环完成调用一次。 不是循环的则完成执行
  166. }
  167. private void UpdateTweenEd()
  168. {
  169. Debug.Log(number); // 变化过程中, 每帧回调该方法
  170. }
  171. private void OnWayPointChange()
  172. {
  173. Debug.Log("OnWayPointChange"); // 当路点发生变化时回调,在执行 DOPath 回调
  174. }
  175. #endregion
  176. #region Set 方法
  177. private void FunctionSet()
  178. {
  179. Vector3 pos = Vector3.zero;
  180. // 设置了循环类型 LoopType.Yoyo 和缓冲类型 Ease.InOutCirc
  181. Tween tween = DOTween.To(() => pos, r => pos = r, new Vector3(5, 5, 5), 1).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.InOutCirc);
  182. //复制一个 Tween 对象的 id, ease, loops, delay, timeScale, callbacks, etc 到另一个 Tween 对象
  183. // t 复制 tween 的循环类型和缓冲类型
  184. Tween t = DOTween.To(() => transform.position, r => transform.position = r, new Vector3(15, 15, 15), 2).SetAs(tween);
  185. // SetAutoKill 设置自动销毁
  186. // SetDelay 设置延迟
  187. // SetEase 设置缓冲类型
  188. // SetId 设置ID 可以只用 int、string、object等类型的值
  189. // SetLoops 设置循环类型
  190. // SetRecyclable 设置为可回收,可循环使用的
  191. // SetRelative 设置相对变化
  192. // SetSpeedBased
  193. // SetTarget
  194. // 设置 Update 的值 告诉 Tween对象 是否忽视 Unity的 的 timeScale ,即是否受Unity 时间的影响
  195. // SetUpdate(UpdateType.Normal, true) 设置为 true 为忽视 Unity的时间影响
  196. // 设置为 false 为不忽视Unity的时间影响
  197. // SetUpdate
  198. transform.DOMoveX(20, 5).SetAutoKill(true).SetDelay(3).SetEase(Ease.InOutCirc)
  199. .SetId("superTween").SetLoops( -1, LoopType.Yoyo).SetRecyclable()
  200. .SetRelative().SetSpeedBased().SetTarget(transform).SetUpdate(UpdateType.Normal, true);
  201. }
  202. #endregion
  203. #region Sequence 队列
  204. private void FunctionSequence()
  205. {
  206. float duration = 5; // 时间
  207. Sequence s = DOTween.Sequence();
  208. // 添加 一个相对于原始位置 水平方向移动, 时间 3 秒,缓冲类型 为 Ease.InOutQuad
  209. s.Append(transform.DOMoveX(6, 3).SetRelative().SetEase(Ease.InOutQuad));
  210. //插入一个旋转, 设置循环类型为 来去 方式
  211. // and will loop forward and backward twice
  212. s.Insert(0, transform.DORotate(new Vector3(0, 45, 0), duration / 2).SetEase(Ease.InQuad).SetLoops(100, LoopType.Yoyo));
  213. // Add a color tween that will start at half the duration and last until the end
  214. s.Insert(duration / 2, transform.GetComponent<Renderer>().material.DOColor(Color.yellow, duration / 2));
  215. // Set the whole Sequence to loop infinitely forward and backwards
  216. s.SetLoops(-1, LoopType.Yoyo);
  217. }
  218. #endregion
  219. #region 设置冷却转圈的 Image
  220. public Image dotweenLogo, circleOutline;
  221. private void FunctionImageCollDown()
  222. {
  223. circleOutline = GetComponent<Image>();
  224. if (circleOutline == null)
  225. return;
  226. // 随机设置 颜色
  227. circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear).Pause();
  228. //设置冷却转圈 附带设置 转一圈完成的回调方法(回调方法直接实现,不用调用其他的方法)
  229. circleOutline.DOFillAmount(0, 1.5f).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo)
  230. .OnStepComplete(() =>
  231. {
  232. circleOutline.fillClockwise = !circleOutline.fillClockwise;
  233. circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear);
  234. })
  235. .Pause();
  236. dotweenLogo = GetComponent<Image>();
  237. if (dotweenLogo == null)
  238. return;
  239. // 这是Image 的褪色,即 慢慢变为 看不见
  240. dotweenLogo.DOFade(0, 1.5f).SetAutoKill(false).Pause();
  241. }
  242. private Color RandomColor()
  243. {
  244. return new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1);
  245. }
  246. #endregion
  247. #region 设置 Text 的跑马字,一秒显示 N 个字
  248. public Text text, relativeText, scrambledText;
  249. private void FunctionText()
  250. {
  251. text = GetComponent<Text>();
  252. // Animate the first text...
  253. text.DOText("This text will replace the existing one", 2).SetEase(Ease.Linear).SetAutoKill(false).Pause();
  254. relativeText = GetComponent<Text>();
  255. // Animate the second (relative) text...
  256. relativeText.DOText(" - This text will be added to the existing one", 2).SetRelative().SetEase(Ease.Linear).SetAutoKill(false).Pause();
  257. scrambledText = GetComponent<Text>();
  258. // Animate the third (scrambled) text...
  259. scrambledText.DOText("This text will appear from scrambled chars", 2, true).SetEase(Ease.Linear).SetAutoKill(false).Pause();
  260. }
  261. #endregion
  262. #region Slider
  263. public Slider slider;
  264. private void FunctionSlider()
  265. {
  266. slider = GetComponent<Slider>();
  267. slider.DOValue(1, 1.5f).SetEase(Ease.InOutQuad).SetLoops(-1, LoopType.Yoyo).Pause();
  268. }
  269. #endregion
  270. #region
  271. private void FunctionDOTween()
  272. {
  273. DOTween.PlayAll(); // 开始所有 Tween 对象
  274. DOTween.RestartAll(); // 所有 Tween对象 从新开始
  275. }
  276. #endregion
  277. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/blog/article/detail/89935
推荐阅读
相关标签
  

闽ICP备14008679号