DoTween实现打字机功能

NGUI里面有自带的打字机的功能,但是现在我们最常用的是UGUI,那么打字机可以用DoTween插件来实现:代码很简单,如下:

using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class TyperExample : MonoBehaviour
{
    private Text text;


    void Awake()
    {
        text = GetComponent<Text>();
    }
	// Use this for initialization
	void Start ()
	{
	    text.DOText("原来你是我最想留住的幸运,原来我们的爱情曾经靠得那么近,那为我对抗世界的决定,那为我淋的雨,一幕幕都是你,一尘不染的真心", 10f).SetLoops(-1);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

挂载在Text上面即可,效果如下:

关注一下公众号,回复关键字“打字机”即可获取源码和DoTween pro插件:

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Unity中使用DOTween实现打字机效果,可以使用Tween的`Append`方法来逐个打印每个字符,并使用`Input.GetKeyDown(KeyCode.Escape)`来监听ESC键的按下。代码如下: ```c# using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class TypewriterEffect : MonoBehaviour { public float delay = 0.1f; // 每个字的打印速度 public Text textComponent; public string message; private bool isTyping = true; private Tween typingTween; private void Start() { typingTween = DOTween.Sequence(); for (int i = 0; i < message.Length; i++) { typingTween.Append(textComponent.DOText(message.Substring(0, i + 1), delay)); } typingTween.OnComplete(() => isTyping = false); } private void Update() { if (isTyping && Input.GetKeyDown(KeyCode.Escape)) { typingTween.Kill(); // 立即显示全部文字 textComponent.text = message; isTyping = false; } } } ``` 首先,在`Start`函数中创建Tween序列`typingTween`,使用`Append`方法逐个打印每个字符,并根据`delay`变量设置打印速度。使用`OnComplete`方法设置当Tween序列完成时执行的回调函数,并将`isTyping`标志位设置为`false`。 然后,在`Update`函数中监听ESC键的按下,如果按下了ESC键,则使用`Kill`方法立即停止Tween序列,并将`textComponent.text`设置为完整的字符串,即可立即显示全部文字。 需要注意的是,在使用Tween时需要使用`DOTween.Sequence()`方法创建Tween序列,并使用`Append`方法逐个添加Tween。另外,在使用Tween时需要注意Tween的生命周期,确保在需要时及时停止Tween

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值