下载地址:
http://dotween.demigiant.com/download.php
变化曲线效果参考:
http://robertpenner.com/easing/easing_demo.html
配置参考:
https://www.jianshu.com/p/252c18016b3f
封装:
using UnityEngine;
using DG.Tweening;
using System;
using UnityEngine.UI;
public class TweenUtil : MonoBehaviour
{
private Tweener tempTweener = null;
private Sequence tempSeq = null;
private Transform tempTrans = null;
///
/// 调用之前设置Transform
///
public TweenUtil SetTransform(Transform trans)
{
Clear();
tempTrans = trans;
return this;
}
///
/// 设置循环次数
///
public TweenUtil SetLoops(int count, int type)
{
if (tempSeq != null)
{
tempSeq.SetLoops(count, (LoopType)type);
}
else if (tempTweener != null)
{
tempTweener.SetLoops(count, (LoopType)type);
}
return this;
}
///
/// 设置缓冲类型
///
public TweenUtil SetEase(int ease)
{
if (tempSeq != null)
{
tempSeq.SetEase((Ease)ease);
}
else if (tempTweener != null)
{
tempTweener.SetEase((Ease)ease);
}
return this;
}
///
/// 设置相对变化
///
public TweenUtil SetRelative()
{
if (tempSeq != null)
{
tempSeq.SetRelative();
}
else if (tempTweener != null)
{
tempTweener.SetRelative();
}
return this;
}
///
/// 设置自动销毁
///
public TweenUtil SetAutoKill(bool b)
{
if (tempSeq != null)
{
tempSeq.SetAutoKill(b);
}
else if (tempTweener != null)
{
tempTweener.SetAutoKill(b);
}
return this;
}
///
/// 设置延迟
///
public TweenUtil SetDelay(float time)
{
if (tempSeq != null)
{
tempSeq.SetDelay(time);
}
else if (tempTweener != null)
{
tempTweener.SetDelay(time);
}
return this;
}
///
/// 设置为 true 为忽视 Unity的时间影响 设置为 false 为不忽视Unity的时间影响
///
public TweenUtil SetUpdate(bool b)
{
if (tempSeq != null)
{
tempSeq.SetUpdate(b);
}
else if (tempTweener != null)
{
tempTweener.SetUpdate(b);
}
return this;
}
///
/// 每完成一个动画步骤回掉
///
public void OnStepComplete(Action cb)
{
if (tempSeq != null)
{
tempSeq.OnStepComplete(delegate ()
{
if (cb != null)
{
cb();
}
});
}
else if (tempTweener != null)
{
tempTweener.OnStepComplete(delegate ()
{
if (cb != null)
{
cb();
}
});
}
}
///
/// 动画播放完成后调用
///
public void OnComplete(Action cb)
{
if (tempSeq != null)
{
tempSeq.onComplete = delegate ()
{
if (cb != null)
{
cb();
}
};
}
else if (tempTweener != null)
{
tempTweener.onComplete = delegate ()
{
if (cb != null)
{
cb();
}
};
}
Clear();
}
///
/// 动画播放完成后调用
///
public TweenUtil DOSequence()
{
tempSeq = DOTween.Sequence();
return this;
}
///
/// 设置tween参数
///
private void SetTween(Tweener temp)
{
if (tempSeq != null)
{
tempSeq.Append(temp);
}
else
{
tempTweener = temp;
}
}
///
/// 设置tween sequence
///
/// Sq.
private void SetSequence(Sequence sq)
{
tempSeq.Append(sq);
}
///
/// 清除当前动画变量
///
private void Clear()
{
tempSeq = null;
tempTrans = null;
tempTweener = null;
}
// **********************************************************************
/// transform移动
// **********************************************************************
public TweenUtil DOMoveX(float x, float time, bool b)
{
SetTween(tempTrans.DOMoveX(x, time, b));
return this;
}
public TweenUtil DOMoveY(float y, float time, bool b)
{
SetTween(tempTrans.DOMoveY(y, time, b));
return this;
}
public TweenUtil DOMoveZ(float z, float time, bool b)
{
SetTween(tempTrans.DOMoveZ(z, time, b));
return this;
}
public TweenUtil DOLocalMoveX(float x, float time, bool b)
{
SetTween(tempTrans.DOLocalMoveX(x, time, b));
return this;
}
public TweenUtil DOLocalMoveY(float y, float time, bool b)
{
SetTween(tempTrans.DOLocalMoveY(y, time, b));
return this;
}
public TweenUtil DOLocalMoveZ(float z, float time, bool b)
{
SetTween(tempTrans.DOLocalMoveZ(z, time, b));
return this;
}
public TweenUtil DoMove(float x, float y, float z, float time, bool b = false)
{
SetTween(tempTrans.DOMove(new Vector3(x, y, z), time, b));
return this;
}
public TweenUtil DOMove(Vector3 pos, float time, bool b = false)
{
SetTween(tempTrans.DOMove(pos, time, b));
return this;
}
public TweenUtil DOLocalMove(float x, float y, float z, float time, bool b = false)
{
SetTween(tempTrans.DOLocalMove(new Vector3(x, y, z), time, b));
return this;
}
public TweenUtil DOLocalMove(Vector3 pos, float time, bool b = false)
{
SetTween(tempTrans.DOLocalMove(pos, time, b));
return this;
}
public TweenUtil DoJump(float x, float y, float z, float time, bool b = false, float jumpPower = 1.0f, int jumps = 1)
{
SetSequence(tempTrans.DOJump(new Vector3(x, y, z), jumpPower, jumps, time, b));
return this;
}
public TweenUtil DoJump(Vector3 pos, float time, bool b = false, float jumpPower = 1.0f, int jumps = 1)
{
SetSequence(tempTrans.DOJump(pos, jumpPower, jumps, time, b));
return this;
}
// **********************************************************************
/// transform缩放
// **********************************************************************
public TweenUtil DoScale(float x, float y, float z, float time)
{
SetTween(tempTrans.DOScale(new Vector3(x, y , z), time));
return this;
}
public TweenUtil DoScale(Vector3 value, float time)
{
SetTween(tempTrans.DOScale(value, time));
return this;
}
public TweenUtil DOScaleX(float x, float time)
{
SetTween(tempTrans.DOScaleX(x, time));
return this;
}
public TweenUtil DOScaleY(float y, float time)
{
SetTween(tempTrans.DOScaleY(y, time));
return this;
}
public TweenUtil DOScaleZ(float z, float time)
{
SetTween(tempTrans.DOScaleZ(z, time));
return this;
}
// **********************************************************************
/// transform旋轉
// **********************************************************************
public TweenUtil DoRotate(float x, float y, float z, float time)
{
SetTween(tempTrans.DORotate(new Vector3(x, y, z), time));
return this;
}
public TweenUtil DoRotate(Vector3 value, float time)
{
SetTween(tempTrans.DORotate(value, time));
return this;
}
public TweenUtil DoLocalRotate(float x, float y, float z, float time)
{
SetTween(tempTrans.DOLocalRotate(new Vector3(x, y, z), time));
return this;
}
public TweenUtil DoLocalRotate(Vector3 value, float time)
{
SetTween(tempTrans.DOLocalRotate(value, time));
return this;
}
// **********************************************************************
/// image渐隐渐显
// **********************************************************************
public void DoLoopFade(bool b)
{
SpriteRenderer img = tempTrans.GetComponent();
Sequence s = DOTween.Sequence();
s.Append(img.material.DOFade(0f, 1f));
s.Append(img.material.DOFade(1, 1f));
s.SetLoops(-1);
s.SetUpdate(b);
}
public TweenUtil DoFade(float a, float time)
{
Image img = tempTrans.GetComponent();
SetTween(img.DOFade(a, time));
return this;
}
// **********************************************************************
/// transform观察
// **********************************************************************
public TweenUtil DOLookAt(float x, float y, float z, float time, int flags, float tx, float ty, float tz)
{
SetTween(tempTrans.DOLookAt(new Vector3(x, y, z), 1, (AxisConstraint)flags, new Vector3(tx, ty, tz)));
return this;
}
public TweenUtil DOLookAt(Vector3 pos, float time, int flags, Vector3 targetpos)
{
SetTween(tempTrans.DOLookAt(pos, 1, (AxisConstraint)flags, targetpos));
return this;
}
// **********************************************************************
/// transform--数字增加
// **********************************************************************
public TweenUtil DOToRiseNum(int initnum, int endnum, float time, Action cb)
{
int number = initnum;
SetTween
(
DOTween.To(
() => number,
x =>
{
number = x;
if (cb != null)
{
cb(number);
}
},
endnum,
time)
);
return this;
}
// **********************************************************************
/// transform--抖动
// **********************************************************************
public TweenUtil DOShakeScale(float time, float strength, int vibrato, float randomness)
{
SetTween(tempTrans.DOShakeScale(time, strength, vibrato, randomness));
return this;
}
public TweenUtil DOShakeRotation(float time, float strength, int vibrato, float randomness)
{
SetTween(tempTrans.DOShakeRotation(time, strength, vibrato, randomness));
return this;
}
public TweenUtil DOShakePosition(float time, float strength, int vibrato, float randomness, bool snapping)
{
SetTween(tempTrans.DOShakePosition(time, strength, vibrato, randomness, snapping));
return this;
}
}
负责创建TweenUtil绑定到对应预制件上,保证TweenUtil是唯一的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TweenManager : SingleTon
{
public TweenUtil SetTween(Transform trans)
{
TweenUtil util = trans.GetComponent();
if (util == null)
{
util = trans.gameObject.AddComponent();
}
util.SetTransform(trans);
return util;
}
}
一开始会执行一个效果,按下w会执行另一个效果。多次调用共享一个TweenUtil。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
TweenManager.ins.SetTween(transform).DOMove(Vector3.zero, 1, false).SetEase(30).SetDelay(1).DOMove(Vector3.one, 1, false).SetEase(30);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
TweenManager.ins.SetTween(transform).DoJump(Vector3.one, 1);
}
}
}