1、打开Unity,新建一个工程,具体如下图
2、在场景中添加 Image 和 Button,Image 一道 画布外(用于演示进入哟遏悉煲退出),按钮置于中下部,具体如下图
3、在工程中新建一个脚本,双击脚本进行编辑,具体如下图
4、脚本具体代码和代码说明如下图
5、脚本具体内容如下:using UnityEngine;using UnityEngine.UI;using DG.Tweening;publ足毂忍珩ic class DOTweener : MonoBehaviour { private RectTransform imageRectTransform; private bool isOut = true; // Use this for initialization void Start () { //给按钮绑定事件 GameObject.Find("Button").GetComponent<Button>().onClick.AddListener(OnClick); //赋值Image的RectTransform组件 imageRectTransform = GameObject.Find("Image").transform as RectTransform; //使用DOTween在 0.3秒内移动到Vector3(0,0,0)位置,并取得DOTween移动的返回值 Tweener tweener = imageRectTransform.DOLocalMove(new Vector3(0,0,0), 0.3f); //设置DOTween的动作不自动销毁 tweener.SetAutoKill(false); //先暂停动作,便于后面动作动画的前放和到放 tweener.Pause(); } private void OnClick() { if (isOut == true) { //向前播放,图片进入场景,并置isOut为false imageRectTransform.DOPlayForward(); isOut = false; } else { //向后播放,图片退出场景,并置isOut为true imageRectTransform.DOPlayBackwards(); isOut = true; } }}
6、脚本编译正确后,回到Unity界面,在场景中新建一个GameObject,把脚本挂载上去,具体如下图
7、运行场景,点击按钮, Image,一进一出的效果就实现了,具体如下图
8、到此,《Unity DOTween教程之 前放倒放实现UI进入退出》讲解结束,谢谢