Unity DOTween教程之 Text文字飘字效果的实现

2025-11-11 05:46:16

1、Append(Tween tween):

Adds the given tween to the end of the Sequence.

在Sequence的最后添加一个tween。

2、Join(Tween tween):

Inserts the given tween at the same time position of the last tween added to the Sequence.

在Sequence的最后一个tween的开始处放置一个tween。

3、AppendInterval(float interval):

Adds the given interval to the end of the Sequence.

在Sequence的最后添加一段时间间隔。

1、打开Unity,新建一个空工程,并且导入DOTween插件(导入方法可参见下面百度经验),具体如下

Unity DOTween教程之 Text文字飘字效果的实现

2、在场景中添加一个 Text,适当调整好大小,位置适中,颜色红色,字体调整合理,具体如下图

Unity DOTween教程之 Text文字飘字效果的实现

3、在工程中新建一个脚本 TextMovedByDOTween,双击脚本或者右建 Open C# Project 打开脚本进行编辑,具体如下图

Unity DOTween教程之 Text文字飘字效果的实现

4、TextMovedByDOTween 脚本的具体代码和代码说明如下图

Unity DOTween教程之 Text文字飘字效果的实现

Unity DOTween教程之 Text文字飘字效果的实现

5、TextByDOTween 脚本具体内容如下:

using UnityEngine;

using UnityEngine.UI;

using DG.Tweening;

public class TextMovedByDOTween : MonoBehaviour {

    private Text text;

// Use this for initialization

void Start () {

        text = GameObject.Find("Text").GetComponent<Text>(); ;

}

// Update is called once per frame

void Update () {

        if (Input.GetKeyDown(KeyCode.A)) {

            TextMoved(text);

        }

}

    private void  TextMoved(Graphic graphic) {

        //获得Text的rectTransform,和颜色,并设置颜色微透明

        RectTransform rect = graphic.rectTransform;

        Color color = graphic.color;

        graphic.color = new Color(color.r,color.g, color.b, 0);

        //设置一个DOTween队列

        Sequence textMoveSequence = DOTween.Sequence();

        //设置Text移动和透明度的变化值

        Tweener textMove01 = rect.DOMoveY(rect.position.y + 50, 0.5f);

        Tweener textMove02 = rect.DOMoveY(rect.position.y + 100, 0.5f);

        Tweener textColor01 = graphic.DOColor(new Color(color.r, color.g, color.b, 1), 0.5f);

        Tweener textColor02 = graphic.DOColor(new Color(color.r, color.g, color.b, 0), 0.5f);

        //Append 追加一个队列,Join 添加一个队列

        //中间间隔一秒

        //Append 再追加一个队列,再Join 添加一个队列

        textMoveSequence.Append(textMove01);

        textMoveSequence.Join(textColor01);

        textMoveSequence.AppendInterval(1);

        textMoveSequence.Append(textMove02);

        textMoveSequence.Join(textColor02);

    }

}

6、脚本编译正确后,回到unity界面,在场景中添加 GameObject 空物体,把脚本挂载上去,具体如下图

Unity DOTween教程之 Text文字飘字效果的实现

7、把场景中的 Text 的颜色设置为透明,具体如下图

Unity DOTween教程之 Text文字飘字效果的实现

8、运行场景,按下 A 键,字体向上漂浮出现,向上漂浮消失,具体如下图

Unity DOTween教程之 Text文字飘字效果的实现

9、到此,《Unity DOTween教程之 Text文字飘字效果的实现》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢