DoTween 控制UGUI tween动画
来源:互联网 发布:知乎 金冲及 编辑:程序博客网 时间:2024/06/11 03:00
NGUI有自带的tween动画,但是UGUI却没有,如果需要UGUI实现tween动画效果,需要使用插件DoTween.
下载地址:http://download.csdn.net/detail/u011484013/9877427
DoTween使用之前,需要添加引用 using DG.Tweening;
使用方式:
首先获取对象的RectTransform实例rectTransform
基本的移动:rectTransform.DOMove (new Vector2(100,100),1f);
DoMove移动的位置是屏幕的左下角,如果想要移动的位置和Inspector上面显示的左边一致,需要添加屏幕的偏移量:
centerPos = new Vector2 (Screen.width*0.5f,Screen.height*0.5f);
rectTransform.DOMove(new Vector3(Screen.width * 0.5f+100, Screen.height * 0.5f+100,0), 1f);
缩放:rectTransform.DOScale (new Vector3(2,2,1),1f);
旋转:rectTransform.DORotate (new Vector3(0,0,180),1f);
移动相关的基本事件:
OnComplete(TweenCallback callback) OnKill(TweenCallback callback) OnPlay(TweenCallback callback) OnPause(TweenCallback callback) OnRewind(TweenCallback callback) OnStart(TweenCallback callback) OnStepComplete(TweenCallback callback) OnUpdate(TweenCallback callback) OnWaypointChange(TweenCallback<int> callback)
使用方式:
tweener.OnComplete (delegate() { Debug.Log("tween animation 结束"); });
测试demo:
创建一个UGUI image对象,然后在该对象上面添加脚本DoTweenTest2.cs
code:
using UnityEngine;using System.Collections;using DG.Tweening;using UnityEngine.UI;public class DoTweenTest2 : MonoBehaviour { Vector2 centerPos; Image image; // Use this for initialization void Start () { centerPos = new Vector2 (Screen.width*0.5f,Screen.height*0.5f); image = transform.GetComponent<Image>(); } void OnGUI(){ if(GUILayout.Button("move to word pos(100,100)")){ //DoMove的坐标系是左下角为准,移动到100,100位置 image.rectTransform.DOMove (new Vector2(100,100),1f); } if(GUILayout.Button("move to anchor pos(100,100)")){ image.rectTransform.DOMove(new Vector3(Screen.width * 0.5f+100, Screen.height * 0.5f+100,0), 1f); } if(GUILayout.Button("add scale (2,2)")){//每点击一次,在原始缩放基础上放大(2,2) //当前sacle(1,1,1)1秒内添加到(3,3,1) image.rectTransform.DOBlendableScaleBy (new Vector2(2,2),1f); // image.rectTransform.DOScale (new Vector2(2,2),1f); } if(GUILayout.Button("scale to (2,2,1)")){ image.rectTransform.DOScale (new Vector3(2,2,1),1f); } if(GUILayout.Button("rotate 180 degree")){ //旋转到180度 image.rectTransform.DORotate (new Vector3(0,0,180),1f); } if(GUILayout.Button("test tweener event")){ Tweener tweener = image.rectTransform.DOMove(new Vector3(Screen.width * 0.5f+300, Screen.height * 0.5f-100,0), 1f); tweener.OnPlay (OnPlay); tweener.OnComplete (OnComplete);// tweener.OnComplete (delegate() {// Debug.Log("tween animation 结束");// }); } } void OnComplete(){ Debug.Log("tween animation 结束"); } void OnPlay(){ Debug.Log("tween animation 开始"); }}
常用事件脚本:
using UnityEngine;using System.Collections;using DG.Tweening;public class DoTween_event : MonoBehaviour { public RectTransform panelTranform; private bool isIn = false; public LoopType loopType; //循环的模式 void Start() { Tweener paneltweener = panelTranform.DOScale (new Vector3(2,2,1),1f); //多次调用DOTween动画会影响性能 //DOTween默认动画播放完毕时会自动销毁动画 //Tweener对象保存这个动画 paneltweener.SetAutoKill(false); //禁止销毁动画 paneltweener.Pause(); //停止播放动画 //paneltweener.SetDelay(0.5f); //设置动画延迟播放 //paneltweener.SetEase(Ease.INTERNAL_Zero); //设置动画运动的模式 //paneltweener.SetLoops(-1); //设置循环播放 (当参数为-1的时候循环播放;当参数>=0的时候则表示循环的次数;0的时候表示循环一次) paneltweener.SetLoops(-1,loopType); //设置循环播放并且设置动画循环的模式 //paneltweener.PlayForward(); //播放 //paneltweener.PlayBackwards(); //倒放 paneltweener.OnComplete(OnComplete); //动画播放完毕后执行,动画倒放时候不执行 paneltweener.OnStart(OnStart); //动画第一次播放时执行 paneltweener.OnUpdate(OnUpdate); //动画播放时不断执行 paneltweener.OnPlay(OnPlay); //动画开始播放时执行 //paneltweener.OnStepComplete(OnStepComplete);//动画播放且倒放都会执行一次,且执行顺序先于OnComplete //paneltweener.OnKill(OnKill); //动画删除的时候执行事件 //paneltweener.Kill(true); //删除动画且组件会直接到达指定位置 } private void OnKill() { Debug.Log("This is OnKill"); } private void OnStepComplete() { Debug.Log("This is OnStepComplete"); } private void OnPlay() { Debug.Log("This is OnPlay"); } private void OnUpdate() { Debug.Log("This is OnUpdate"); } private void OnStart() { Debug.Log("动画第一次播放"); } void OnComplete() { Debug.Log("动画播放完毕!"); } // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)){ if(!isIn) { //注释掉的方法是效果都是一样的 //panelTranform.DOPlay(); //动画只会播放一次 panelTranform.DOPlayForward(); //前进 //paneltweener.PlayForward(); //播放 isIn = true; } else { //paneltweener.PlayBackwards(); //倒放 panelTranform.DOPlayBackwards(); //倒放 isIn = false; } } }}
相关博客参考:
http://blog.csdn.net/lyh916/article/details/45888117
http://m.blog.csdn.net/begonia__z/article/details/51244304
- DoTween 控制UGUI tween动画
- UGUI动画控制Animator Controller&NGUI动画插件Tween
- Unity学习笔记11——UGUI动画插件DoTween
- UGUI 过渡动画插件,模仿NGUI的Tween (转载)
- DoTween动画
- UGUI DOTween渐隐渐现
- UGUI DOTween渐隐渐现
- ugui-Tween-Tool
- Android的Tween动画与控制动画的Interpolator
- Tween动画
- Tween动画
- tween动画
- tween动画
- Tween 动画
- Tween动画
- DoTween - 动画重复播放
- DoTween -- 缓动动画
- DoTween动画插件
- Linux Command Line and....ch9(安装软件程序)
- 写作建议
- Mysql Block Nested-Loop 与 ICP
- ReentrantLock源码
- 在子页面session过期无法跳转到父页面
- DoTween 控制UGUI tween动画
- 计算机图形学之光线跟踪算法的研究与实现2017年我的优秀毕业论文
- B2Ctt商城04 ftp 商品规格实现
- 一、学习股市常识
- GitHub学习-----github上删除自己的repository
- 从MongoDB同步数据到ElasticSearch 的五种方式
- CSS3的calc()使用
- 面试零碎知识点小计
- python 中文编码问题