Tween的移动类型有很多种,比如匀速运动、加速运动、减速运动,等等。如果你拿捏不准你需要用什么移动类形式。
/easing/easing_demo.html 你可以在这里预览一下那种移动类型更佳适合你。
using UnityEngine;
using ;
using ; // 别忘记引入
using ;
public class TestDoTween : MonoBehaviour {
int number = 0;
// Use this for initialization
void Start ()
{
//FunctionOne();
//FunctionTwo();
//FunctionSequence();
FunctionSet();
}
// 创建 DOTween 实例 方法
#region 方法一 类方法
private void FunctionOne()
{
// 创建一个 Tweener 是自身坐标 一秒内 移动到 坐标 Vector3(5, 5, 5) 位置
Tween tween = (() => , r => = r, new Vector3(5, 5, 5), 1);
// 创建一个 Tweener 对象, 另 number的值在 5 秒内变化到 100
Tween t = (() => number, x => number = x, 100, 5);
// 给执行 t 变化时,每帧回调一次 UpdateTween 方法
( () => UpdateTween(number));
}
private void UpdateTween(int num)
{
(num); // 变化过程中, 每帧回调该方法
}
#endregion
#region 方法二 直接调用
private void FunctionTwo()
{
//十秒内X,Y,Z 局部坐标(localPosition)移动到 10,10,10 位置
//(new Vector3(10, 10, 10), 10);
//十秒内 X,Y,Z 方向的局部旋转(localPosition),转动到 30,30,30
//(new Vector3(30, 30, 30), 10);
//十秒内X,Y,Z坐标移动到 自身坐标 加 new Vector3( 10,10,10) 位置 原始 坐标 3,3,3,移动后 13,13,13
//(new Vector3(10, 10, 10), 10);
//十秒内X,Y,Z自身旋转到 30,30,30(有父物体的相对于父物体)
//(new Vector3(30, 30, 30), 10);
//十秒内 自身X,Y,Z方向的比例 加 3,3,3如原始比例 2,1,1 变化后5,4,4
//(new Vector3(10, 10, 10), 10);
//执行该方法,变化立即结束,且完成移动
//();
//在变化过程中执行该方法,则物体慢慢的变回原样,如果变化已经完成,该方法无效
//();
// 变化过程中执行该方法,则物体变化到 第二秒 时该物体的位置、比例等
//(2);
//十秒内 弹跳 3次
//(new Vector3(10, 10, 10), 3, 10);
//停止掉当前的变化
//();
// 十秒内 弹跳 3次, 局部坐标最终变化为 10, 0, 10
//(new Vector3(10, 10, 10), 3, 10);
// 5 秒内, 局部坐标变化到 10,10,10
//(new Vector3(10, 10, 10), 5);
// 10 秒内 X 局部坐标变换到 5
//(5, 10);
// 10 秒内 Y 局部坐标变化到 5
//(5, 10);
//10 秒内 Z 局部坐标变化到 5
//(5, 10);
//();
//5 秒内 局部旋转变化到 10,10, 10
//(new Vector3(10, 10, 10), 5);
// 自身朝向 坐标(10,10,10)
//(new Vector3(10, 10, 10), 5);
// 5 秒内 移动到 坐标 (10,10,10)
//(new Vector3(10, 10, 10), 5);
//10 秒内 X 局部坐标变化到 5
//(5, 10);
//10 秒内 Y 局部坐标变化到 5
//(5, 10);
//10 秒内 Z 局部坐标变化到 5
//(5, 10);
//
//();
//执行该方法停止 变化
//();
//();
//变化结束前调用该方法,物体回到原始位置
//();
//执行 (); 物体回到原始位置
//执行 下面方法则再次变化
//();
//冲压机,在 5 秒内在原始坐标和下面坐标之间,来回冲压
//(new Vector3(10, 10, 10), 5);
//冲压机,在 5 秒内在原始旋转和下面角度之间,来回冲压变化
//(new Vector3(50, 50, 50), 5);
//冲压机,在 5 秒内在原始比例和下面比例之间,来回冲压变化
//(new Vector3(5, 5, 5), 5);
//在变化结束之前,执行该方法,则重新开始变化
//();
//变化过程中执行该方法,回到原始
//();
// 10 秒内 旋转角度 到 (50,50,50)
//(new Vector3(50, 50, 50), 5);
// 10 秒内 比例变化到 (5,5,5)
//(new Vector3(5, 5, 5), 5);
// 10 秒内 X 比例变化到 5
//(5, 10);
// 10 秒内 Y 比例变化到 5
//(5, 10);
// 10 秒内 Z 比例变化到 5
//(5, 10);
// 10 秒内 物体 X,Y,Z 坐标在 自身-5 到 自身加 5 之间震动
//(10, new Vector3(10, 10, 10));
// 10 秒内, 物体 X,Y,Z 旋转角度在 自身-5 到 自身加 5 之间震动
//(10, new Vector3(10, 10, 10));
// 10 秒内, 物体 X,Y,Z 比例在 自身-5 到 自身加 5 之间震动
//(10, new Vector3(10, 10, 10));
//在变化过程中执行该方法,停止、开始、停止、开始
//();
// 执行该方法,坐标立即变化为 0,5,0, 从 0,5,0 两秒移动到初始位置
//(new Vector3(0, 5, 0), 2).From();
// 执行该方法,移动到相对于原始位置 6,0,2 的位置
// 如原始位置 3,2,1。 移动后位置为 3+6,2+0,2+2 即 9,2,4
//(new Vector3(6, 0, 2), 2).SetRelative();
}
#endregion
#region DOTween回调方法 以 On 开头
private void CallBack()
{
// 创建一个 Tweener 对象, 另 number的值在 5 秒内变化到 100
Tween t = (() => number, x => number = x, 100, 5);
// DOTween 可以连续调用 N 次方法如下
// 执行变化的过程中可以回调的方法
// 下面是不带参数的回调方法
(OnStartTween).OnKill(OnKill).OnPause(OnPause).OnPlay(OnPlay).OnRewind(OnRewind);
(OnStepComplete).OnUpdate(UpdateTweenEd).OnComplete(OnComplete);
// 带参数的回调方法如下
(() => UpdateTweenED(number));
}
private void UpdateTweenED(int num)
{
("num " + num);
}
private void OnComplete()
{
("OnComplete"); // 完成变化时回调
}
private void OnKill()
{
("OnKill"); // 执行结束后自动杀死,回调一次
}
private void OnPlay()
{
("OnPlay"); // 开始执行调用一次, 该方法在 OnStart 方法后调用
}
private void OnPause()
{
("OnPause"); // 暂停回调
}
private void OnRewind()
{
("OnRewind"); //倒回回调
}
private void OnStartTween()
{
("StartTween"); // 开始执行最先回调, 该方法在 OnPlay 方法前调用
}
private void OnStepComplete()
{
("OnStepComplete"); // 如果循环的,每循环完成调用一次。 不是循环的则完成执行
}
private void UpdateTweenEd()
{
(number); // 变化过程中, 每帧回调该方法
}
private void OnWayPointChange()
{
("OnWayPointChange"); // 当路点发生变化时回调,在执行 DOPath 回调
}
#endregion
#region Set 方法
private void FunctionSet()
{
Vector3 pos = ;
// 设置了循环类型 和缓冲类型
Tween tween = (() => pos, r => pos = r, new Vector3(5, 5, 5), 1).SetLoops(-1, ).SetEase();
//复制一个 Tween 对象的 id, ease, loops, delay, timeScale, callbacks, etc 到另一个 Tween 对象
// t 复制 tween 的循环类型和缓冲类型
Tween t = (() => , r => = r, new Vector3(15, 15, 15), 2).SetAs(tween);
// SetAutoKill 设置自动销毁
// SetDelay 设置延迟
// SetEase 设置缓冲类型
// SetId 设置ID 可以只用 int、string、object等类型的值
// SetLoops 设置循环类型
// SetRecyclable 设置为可回收,可循环使用的
// SetRelative 设置相对变化
// SetSpeedBased
// SetTarget
// 设置 Update 的值 告诉 Tween对象 是否忽视 Unity的 的 timeScale ,即是否受Unity 时间的影响
// SetUpdate(, true) 设置为 true 为忽视 Unity的时间影响
// 设置为 false 为不忽视Unity的时间影响
// SetUpdate
(20, 5).SetAutoKill(true).SetDelay(3).SetEase()
.SetId("superTween").SetLoops( -1, ).SetRecyclable()
.SetRelative().SetSpeedBased().SetTarget(transform).SetUpdate(, true);
}
#endregion
#region Sequence 队列
private void FunctionSequence()
{
float duration = 5; // 时间
Sequence s = ();
// 添加 一个相对于原始位置 水平方向移动, 时间 3 秒,缓冲类型 为
((6, 3).SetRelative().SetEase());
//插入一个旋转, 设置循环类型为 来去 方式
// and will loop forward and backward twice
(0, (new Vector3(0, 45, 0), duration / 2).SetEase().SetLoops(100, ));
// Add a color tween that will start at half the duration and last until the end
(duration / 2, <Renderer>().(, duration / 2));
// Set the whole Sequence to loop infinitely forward and backwards
(-1, );
}
#endregion
#region 设置冷却转圈的 Image
public Image dotweenLogo, circleOutline;
private void FunctionImageCollDown()
{
circleOutline = GetComponent<Image>();
if (circleOutline == null)
return;
// 随机设置 颜色
(RandomColor(), 1.5f).SetEase().Pause();
//设置冷却转圈 附带设置 转一圈完成的回调方法(回调方法直接实现,不用调用其他的方法)
(0, 1.5f).SetEase().SetLoops(-1, )
.OnStepComplete(() =>
{
= !;
(RandomColor(), 1.5f).SetEase();
})
.Pause();
dotweenLogo = GetComponent<Image>();
if (dotweenLogo == null)
return;
// 这是Image 的褪色,即 慢慢变为 看不见
(0, 1.5f).SetAutoKill(false).Pause();
}
private Color RandomColor()
{
return new Color((0f, 1f), (0f, 1f), (0f, 1f), 1);
}
#endregion
#region 设置 Text 的跑马字,一秒显示 N 个字
public Text text, relativeText, scrambledText;
private void FunctionText()
{
text = GetComponent<Text>();
// Animate the first text...
("This text will replace the existing one", 2).SetEase().SetAutoKill(false).Pause();
relativeText = GetComponent<Text>();
// Animate the second (relative) text...
(" - This text will be added to the existing one", 2).SetRelative().SetEase().SetAutoKill(false).Pause();
scrambledText = GetComponent<Text>();
// Animate the third (scrambled) text...
("This text will appear from scrambled chars", 2, true).SetEase().SetAutoKill(false).Pause();
}
#endregion
#region Slider
public Slider slider;
private void FunctionSlider()
{
slider = GetComponent<Slider>();
(1, 1.5f).SetEase().SetLoops(-1, ).Pause();
}
#endregion
#region
private void FunctionDOTween()
{
(); // 开始所有 Tween 对象
(); // 所有 Tween对象 从新开始
}
#endregion
}