DoTween的使用

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