文章目录
1.简单的动画
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class DOTWEENTEST : MonoBehaviour
{
public Vector3 myValue = new Vector3(0, 0, 0);
// public Transform cubetransform;
public RectTransform tasktransform;
//对float类型做出修改
public float flt = 0;
void Start()
{
//对Vector3类型数值做出变化
DOTween.To(() => myValue, x => myValue = x, new Vector3(0, 0, 0),1);
//也可对float类型做出数值变化(可以运用到颜色上)
DOTween.To(() => flt, x => flt = x, 10, 1);
}
void Update()
{
// tasktransform.position = myValue; //世界坐标
tasktransform.localPosition = myValue;
}
}
通过改变UI的Recttransform的数值来实现动画效果
将一个image放置在画板外
脚本挂载到场景任意游戏物体上
经过一秒,imgae从vector3(1255,0,0)移动到画板(0,0,0)位置。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class MyButton : MonoBehaviour
{
public RectTransform Rectpanel;
private bool isIn =false;
// Start is called before the first frame update
void Start()
{
GameObject.Find("Button").GetComponent<Button>().onClick.AddListener(OnClick);
Tweener tweener = Rectpanel.DOLocalMove(new Vector3(0, 0, 0), 0.3f);//默认动画被播放完成会被销毁
//Tweener对象保存这个动画的信息 每次调用do类型的方法都会创建一个tweer对象,这个对象是dotween来管理
tweener.SetAutoKill(false);//吧autokill自动销毁设置为false
tweener.Pause();//动画暂停播放
}
private void OnClick()
{
// Rectpanel.DOMove(new Vector3(0, 0, 0), 1);//让rectpanel从当前位置到0,0,0的位置 时间为1s(修改的世界坐标)
if (isIn == false)
{
//Rectpanel.DOPlay();//播放一次
Rectpanel.DOPlayForward();//动画前放
isIn = true;
}
else
{
Rectpanel.DOPlayBackwards();//动画倒放
isIn = false;
}
}
}
Dotween封装好了API供我们直接使用,更加方便。
通过button按钮来控制image的左右滑动动画。
from用法
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class formtween : MonoBehaviour
{
void Start()
{
//从当前(1,1,1)位置移动到(10,0,0)位置
//transform.DOMoveX(10, 3);
//从(10,0,0)位置移动到(1,1,1)位置
// transform.DOMoveX(10, 3).From();
//默认为false true是相对坐标 从(11,0,0)移动到(1,1,1)位置
transform.DOMoveX(10, 3).From(true);
}
}
动画曲线和事件函数
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class settings : MonoBehaviour
{
void Start()
{
Tweener tweener = transform.DOLocalMoveX(0, 3);
tweener.SetEase(Ease.OutBounce);//设置运动曲线
tweener.SetLoops(2);//动画循环播放2次
tweener.OnComplete(Finish);//动画播放完后触发事件
}
private void Finish()
{
Debug.Log("动画完成");
}
}
动画的生命周期以及生命周期函数
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class settings : MonoBehaviour
{
void Start()
{
Tweener tweener = transform.DOLocalMoveX(0, 3);
tweener.SetEase(Ease.OutBounce);//设置运动曲线
tweener.SetLoops(2);//动画循环播放2次
tweener.OnComplete(Finish);//动画播放完后触发事件
tweener.OnKill(Kill);//动画被杀死后触发事件
tweener.OnPlay(startPlay);//动画播放,暂停后重新播放也会被调用
tweener.OnPause(Nowpause);//动画暂停后触发事件
//动画回退时回调
// 以下情况会被调用
//使用DORestart重新播放时
//使用Rewind倒播动画完成时
//使用DOFlip翻转动画完成时
//使用DOPlayBackwards反向播放动画完成时
//transform.DOMove(Vector3.one, 2).OnRewind(() => { });
//只在第一次播放动画时调用,在play之前调用
//transform.DOMove(Vector3.one, 2).OnStart(() => { });
//完成单个循环周期时触发
//transform.DOMove(Vector3.one, 2).OnStepComplete(() => { });
//帧回调
//transform.DOMove(Vector3.one, 2).OnUpdate(() => { });
//在路径动画时,改变目标点时的回调,参数为当前目标点的下标
//transform.DOMove(Vector3.one, 2).OnWaypointChange((value) => { });
}
private void Finish()
{
Debug.Log("动画完成");
}
private void Kill()
{
Debug.Log("动画被杀死");
}
private void startPlay()
{
Debug.Log("动画开始播放");
}
private void Nowpause()
{
Debug.Log("动画暂时");
}
}
对话框文字动画
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MyText : MonoBehaviour
{
private Text text;
// Start is called before the first frame update
void Start()
{
text = this.GetComponent<Text>();
text.DOText("HELLO WORD", 2f);
}
// Update is called once per frame
void Update()
{
}
}
文字逐显
屏幕震动(游戏相机震动)
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shake : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// transform.DOShakePosition(1);//随机向xyz轴移动
transform.DOShakePosition(1, new Vector3(1, 1, 0)); //只在xy轴移动单位距离1
}
// Update is called once per frame
void Update()
{
}
}
颜色和透明度动画
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextColor : MonoBehaviour
{
private Text text;
// Start is called before the first frame update
void Start()
{
text = this.GetComponent<Text>();
text.DOColor(Color.blue, 2); //颜色向蓝色渐变
text.DOFade(1, 3);//透明通道由0到1
}
}
动画组件的可视化
挂载一个脚本就行了
DotweenPath路径编辑器
给要添加移动路径的物体加上dotween path组件
shift+ctrl可以在scene面板添加运动节点