Unity Dotween官方案例学习
本文只涉及一些案例,具体查看 DoTween 官方文档。
一、 Basics
public class Basics : MonoBehaviour
{
public Transform redCube, greenCube, blueCube, purpleCube; IEnumerator Start()
{
// Start after one second delay (to ignore Unity hiccups when activating Play mode in Editor)
yield return new WaitForSeconds(); // 2秒时间移动到 0,4,0
redCube.DOMove(new Vector3(,,), ); // 2秒时间从 0,4,0 移动到原始位置
greenCube.DOMove(new Vector3(,,), ).From(); // 2秒时间移动 0,4,0 相对位置
blueCube.DOMove(new Vector3(,,), ).SetRelative(); // 2秒时间移动 6,0,0 相对位置
purpleCube.DOMove(new Vector3(,,), ).SetRelative();
// 2秒内将颜色变为黄色,并且循环往复一直执行
purpleCube.GetComponent<Renderer>().material.DOColor(Color.yellow, ).SetLoops(-, LoopType.Yoyo);
}
}
该场景主要涉及对一些 Unity 组件(transform, material)属性的变换,当然我们也可以对其他一些组件(Audio, Camera, Light, Rigidbody, ...)进行操作。
二、Follow
public class Follow : MonoBehaviour
{
public Transform target; // Target to follow
Vector3 targetLastPos;
Tweener tween; void Start()
{
// 启动后先移动到目标位置,保存 Tweener 并且设置不自动销毁
tween = transform.DOMove(target.position, ).SetAutoKill(false);
// 存储上一目标位置
targetLastPos = target.position;
} void Update()
{
// 目标没有移动
if (targetLastPos == target.position) return;
// 修改目标位置,重新开始动画
tween.ChangeEndValue(target.position, true).Restart();
// 保存目标位置,用于下次比较
targetLastPos = target.position;
}
}
该场景实现目标跟随,当目标物移动的时候,跟随物体会相应移动。其中涉及到 Tweener 的设置与控制。
三、Materials
public class Materials : MonoBehaviour
{
public GameObject target;
public Color toColor; Tween colorTween, emissionTween, offsetTween; void Start()
{
// 获取材质组件
Material mat = target.GetComponent<Renderer>().material; // 改变材质颜色,动画默认状态为暂停
colorTween = mat.DOColor(toColor, ).SetLoops(-, LoopType.Yoyo).Pause(); // 修改材质光线发散颜色,注意属性 _EmissionColor
emissionTween = mat.DOColor(new Color(, , , ), "_EmissionColor", ).SetLoops(-, LoopType.Yoyo).Pause(); // 修改材质偏移,动画变化为线性,持续增加
offsetTween = mat.DOOffset(new Vector2(, ), ).SetEase(Ease.Linear).SetLoops(-, LoopType.Incremental).Pause();
} // Toggle methods (called by UI events) public void ToggleColor()
{
// 切换动画状态,播放或者暂停
colorTween.TogglePause();
} public void ToggleEmission()
{
emissionTween.TogglePause();
} public void ToggleOffset()
{
offsetTween.TogglePause();
}
}
该场景主要实现 material 的动画效果。其中 SetEase 方法设置动画的属性变化方式(线性,抛物线等,就是变化速度)。
四、Paths
public class Paths : MonoBehaviour
{
public Transform target;
public PathType pathType = PathType.CatmullRom;
// 路径
public Vector3[] waypoints = new[] {
new Vector3(, , ),
new Vector3(, , ),
new Vector3(, , ),
new Vector3(, , ),
new Vector3(-, , )
}; void Start()
{
// 按路径运动
// 使用 SetOptions 函数使路径封闭
// 使用 SetLookAt 函数使物体朝向路径本身
Tween t = target.DOPath(waypoints, , pathType)
.SetOptions(true)
.SetLookAt(0.001f);
// 线性变化且无限循环
t.SetEase(Ease.Linear).SetLoops(-);
}
}
该场景实现了物体按路径运动动画。
五、Sequences
public class Sequences : MonoBehaviour
{
public Transform cube;
public float duration = ; IEnumerator Start()
{
yield return new WaitForSeconds(); // 新建一个 Sequence
Sequence s = DOTween.Sequence();
// 添加一个动画,持续一个周期
s.Append(cube.DOMoveX(, duration).SetRelative().SetEase(Ease.InOutQuad));
// 添加一个动画,持续半个周期
s.Insert(, cube.DORotate(new Vector3(, , ), duration / ).SetEase(Ease.InQuad).SetLoops(, LoopType.Yoyo));
// 添加一个动画,半个周期时开始,切持续半个周期
s.Insert(duration / , cube.GetComponent<Renderer>().material.DOColor(Color.yellow, duration / ));
s.SetLoops(-, LoopType.Yoyo);
}
}
该场景实现了一个简单的 Sequences。其中 Append 方法是将动画加在末尾,而 Insert 方法是可以加到任意位置。
六、UGUI
public class UGUI : MonoBehaviour
{
public Image dotweenLogo, circleOutline;
public Text text, relativeText, scrambledText;
public Slider slider; void Start()
{
// Logo 图片渐渐消失动画
dotweenLogo.DOFade(, 1.5f).SetAutoKill(false).Pause(); // 图片颜色动画
circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear).Pause();
// 图片部分显示动画,结束后按相反方向
circleOutline.DOFillAmount(, 1.5f).SetEase(Ease.Linear).SetLoops(-, LoopType.Yoyo)
.OnStepComplete(()=> {
circleOutline.fillClockwise = !circleOutline.fillClockwise;
circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear);
})
.Pause(); // 文字动画
text.DOText("This text will replace the existing one", ).SetEase(Ease.Linear).SetAutoKill(false).Pause();
relativeText.DOText(" - This text will be added to the existing one", ).SetRelative().SetEase(Ease.Linear).SetAutoKill(false).Pause();
scrambledText.DOText("This text will appear from scrambled chars", , true, ScrambleMode.All).SetEase(Ease.Linear).SetAutoKill(false).Pause(); // 滑动条动画
slider.DOValue(, 1.5f).SetEase(Ease.InOutQuad).SetLoops(-, LoopType.Yoyo).Pause();
} // Called by PLAY button OnClick event. Starts all tweens
public void StartTweens()
{
DOTween.PlayAll();
} // Called by RESTART button OnClick event. Restarts all tweens
public void RestartTweens()
{
DOTween.RestartAll();
} // Returns a random color
Color RandomColor()
{
return new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), );
}
}
该场景主要实现了 UGUI 组件的动画。
Unity Dotween官方案例学习的更多相关文章
- Unity XLua 官方案例学习
1. Helloworld using UnityEngine; using XLua; public class Helloworld : MonoBehaviour { // Use this f ...
- Unity EasyTouch官方案例学习
一.代码检测手势事件 1. EasyTouch4.x 写法 首先要手动在 Hierarchy 窗口添加 EasyTouch 物体,以触摸(Touch)手势为例,代码如下: using UnityEng ...
- 8.3 ContosoMVCWeb官方案例学习
1. 分页案例学习 2. 排序搜索案例学习 3.使用Configuration.cs中的Seed方法 在数据库迁移过程中,使用update-database,会运行seed方法.seed方法能够将初始 ...
- Egret官方案例学习笔记
1.资源记载方式 (1)Egret引擎是2.0.5. (2)resource/resource.json文件是: { "resources": [ { "name&quo ...
- Unity XLua 官方教程学习
一.Lua 文件加载 1. 执行字符串 using UnityEngine; using XLua; public class ByString : MonoBehaviour { LuaEnv lu ...
- UE4的AI学习(2)——官方案例实例分析
官方给出的AI实例是实现一个跟随着玩家跑的AI,当玩家没有在AI视野里时,它会继续跑到最后看到玩家的地点,等待几秒后如果仍然看不到玩家,则跑回初始地点.官方的案例已经讲得比较详细,对于一些具体的函数调 ...
- Unity CommandBuffer的一些学习整理
1.前言 近期在整理CommandBuffer这块资料,之前的了解一直较为混乱. 算不上新东西了,但个人觉得有些时候要比加一个摄像机再转RT廉价一些,至少省了深度排序这些操作. 本文使用两个例子讲解C ...
- 通过angularJS官方案例快速入门
官方案例-angular-phonecat angularJS官方提供了一个官方案例给大家进行循序渐进的学习,但是如果之前没有接触过node.js以及git的同学这个案例拿着也无从下手-这里就介绍一下 ...
- Siki_Unity_2-4_UGUI_Unity5.1 UI 案例学习
Unity 2-4 UGUI Unity5.1 UI 案例学习 任务1-1:UGUI简介 什么是GUI: 游戏的开始菜单 RPG游戏的菜单栏.侧边栏和功能栏(比如背包系统.任务列表等) 设计用来控制移 ...
随机推荐
- wc 统计文件的行数,字数,字符
格式:wc 参数 文件 默认统计文件的行数,字数,字符. -l 统计有多少行数 -c 统计有多少个字节 -m 统计有多少个字符 -w 统计有多少个字数
- 隐藏linux软件及内核版本
在登陆linux主机本地(非xshell或crt)前,会显示系统的版本和内核: 那么我们如何隐藏呢?如下: 1.清空版本及内核信息: [root@bqh-01 ~]# cat /etc/issue C ...
- 如何在linux centos 环境下运行.exe文件
linux是不能运行window下的可执行文件的,必须借助于wine.百度了以下wine如下: Wine (“Wine Is Not an Emulator” 的递归缩写)是一个能够在多种 POS ...
- 有关java编辑器myeclipse编辑网站的一些设置(个人习惯)
一.界面显示设置 首先进入一个新的空间,里面的设置肯定都是默认的.点击上方导航栏的window-Perferences-Appearance可以去进行设置界面的显示,Theme中可以选择windows ...
- cocos2d-x2.2.3学习
cocos2d-x2.2.3抛弃了原先的vs模板,改为python创建项目,详细什么原因我不是非常清楚啊,可能更方便些吧. 毕竟用pythone能够一下子创建很多不同平台的项目,让项目移植更方便些.可 ...
- php实现链表的基本操作
<?php class node{ private $value; private $next; public function __construct($value=0,$next=null) ...
- 浏览器中上传Excel文件,服务器获取Excel字段。写入的数据库中。操作Excel的方式jxl和poi。
从Excel中获取字段,官方给我们提供了方法,地址https://poi.apache.org/components/spreadsheet/quick-guide.html#CellContents ...
- docker-compose运行Rails
1.新建空目录,名字可以叫Rails 2.新建Dockerfile并添加如下内容 FROM ruby:2.5 RUN apt-get update -qq && apt-get ins ...
- [转]OpenGL 使用 PBO 高速复制屏幕图像到内存或者纹理中
如果你想给游戏做个截图功能,或者想把屏幕图像弄成一个纹理,你就非常需要 PBO 了 通常情况下,你想把屏幕图像的像素数据读到内存需要用 glReadPixels 然后 pixels 参数传进去一块内存 ...
- day06--元组、字典、集合与关系运算
今日内容: 1.元组 2.字典 3.集合与关系运算 元组: 用途:记录多个值,当多个值没有改的需求,此时用元组更适合. 定义方式:在()内用逗号分隔开多个任意类型的值. 变量名=tuple('') 切 ...