Unity 打字机效果计时器实现和DOTween实现(笔记)


创建相关物体

  1. 创建Canvas,改成世界空间,坐标重置,scale改成0.01
    在这里插入图片描述
  2. 添加image,在image下添加text和button
    在这里插入图片描述

计时器实现

  1. 创建 脚本,挂在canvas下面,开始写脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextContro : MonoBehaviour {
    
    
	Text text0;
	string s = "鼠标轻点,按键轻点,电波激发一个个信息点,代码串联起一个个字符。此时此刻,你的话语就传达到我身边。高速发展现代科技,使世界各地的人们瞬息之间可以交流。";
	float t = 0;
	int index = 0;
	bool isPrint = false;
	// Use this for initialization
	void Start () {
    
    
		text0 = transform.GetChild(0).GetChild(0).GetComponent<Text>();
	}
	public void PrintText()
    {
    
    
		transform.GetChild(0).GetChild(1).GetComponent<AudioSource>().Play();
		isPrint = true;
    }
	// Update is called once per frame
	void Update () {
    
    
        if (isPrint)
        {
    
    
			t += Time.deltaTime;
			if (t > 18.0f/s.Length)
			{
    
    
				t = 0;
				text0.text += s[index];
				index++;
				if (index == s.Length)
				{
    
    
					isPrint = false;
				}
			}
		}
		
	}
}


  1. 使用软件把刚才那段文字转化成语音
  2. 在button按钮上添加事件拖拽并加上audio source组件,把语音拖进去
    在这里插入图片描述
    4.查看语音几秒读完,改下代码打字机的速度,打字机的速度要比语音块一秒多
    5.效果

uinty打字机计时器

DOTween实现

  • 使用DOTween实现就非常简单了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class TextContro : MonoBehaviour {
    
    
	Text text0;
	string s = "鼠标轻点,按键轻点,电波激发一个个信息点,代码串联起一个个字符。此时此刻,你的话语就传达到我身边。高速发展现代科技,使世界各地的人们瞬息之间可以交流。";
	// Use this for initialization
	void Start () {
    
    
		text0 = transform.GetChild(0).GetChild(0).GetComponent<Text>();
	}
	public void PrintText()
    {
    
    
		transform.GetChild(0).GetChild(1).GetComponent<AudioSource>().Play();
		Tweener tweener= text0.DOText(s, 18);
		tweener.SetAutoKill(false);
		tweener.SetEase(Ease.Linear);
    }

  • 实现的效果都一样

猜你喜欢

转载自blog.csdn.net/weixin_44954896/article/details/125420114
今日推荐