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游戏的菜单栏.侧边栏和功能栏(比如背包系统.任务列表等) 设计用来控制移 ...
随机推荐
- chmod chown llinux文件及目录的权限介绍
linux 文件或目录的读.写.执行权限说明: chmod :设置文件或目录权限. u:所有者 g:所在组 o:其他组 a:所有人(u.g.o的总和) chmod -R 文件1/文件2….. ...
- Windows XP添加硬盘后系统不能识别(没有任何反应)
解决方法: 1.右键我的电脑--管理--设备管理器--IDE ATA/ATAPI控制器,启用次要IDE通道和主要IDE通道,打开属性,在高级设置里,将设备类型设置为自动检测,重启. 2.硬盘格式为GP ...
- List特有迭代器--ListIterator的特殊功能
/** * >列表迭代器: * ListIterator listIterator():List集合特有的迭代器 * 该迭代器继承了Iterat ...
- vue+axios自己踩过的坑
axios的介绍就不用了吧,api有具体的介绍axios或者是axios中文: 主要讲的就是我自己在第一次使用axios中遇到的问题,及二次封装 先来说说二次封装,之前自己也是网上找了很多同学的封装, ...
- 使用TuShare下载历史逐笔成交数据并生成1分钟线
使用如下代码从TuShare下载沪深300每只股票的历史成交记录并按股票.日期保存到本地.主要是为了以后查询方便快速. #-*- coding: utf-8 -*- import numpy as n ...
- 有关java编辑器myeclipse编辑网站的一些设置(个人习惯)
一.界面显示设置 首先进入一个新的空间,里面的设置肯定都是默认的.点击上方导航栏的window-Perferences-Appearance可以去进行设置界面的显示,Theme中可以选择windows ...
- kudu基础入门
1.kudu介绍 1.1 背景介绍 在KUDU之前,大数据主要以两种方式存储: (1)静态数据: 以 HDFS 引擎作为存储引擎,适用于高吞吐量的离线大数据分析场景.这类存储的局限性是数据无法进行随机 ...
- 一些node模块的学习思考
12月14日清单 1 readline模块 var readline = require("readline"); // input 是必须的,output是可选的 rl = re ...
- uwsgi+django 配置
uwsgi+django 创建新的虚拟环境,且解决crm的环境依赖 在虚拟环境下安装uwsgi pip3 install uwsgi 学习uwsgi命令,如何启动python应用 启动python w ...
- 20165302 程上杰 Exp1 PC平台逆向破解
实验内容 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数. 注入一个自己制作的she ...