栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity实现单个打字输入效果

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity实现单个打字输入效果

1、新建脚本TypewriterEffect,挂载在要实现效果的Text上,想要实现效果的时候只需要获取到Text上的TypewriterEffect脚本,调用里面的StartEffect()方法即可。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TypewriterEffect : MonoBehaviour
{

    public float charsPerSecond = 0.17f;//打字时间间隔
    public string words="";//需要显示的文字,可外部输入也可调用方法输入,建议调用StartEffect方法输入,因为外部输入后也需要调用StartEffect方法。

    private bool isActive = false;//是否开始打字效果
    private float timer;//计时器
    private Text myText;//显示的文本,start里查找
    private int currentPos = 0;//当前打字位置

   
    void Start()
    {
        timer = 0;
        charsPerSecond = Mathf.Max(0.1f, charsPerSecond); //控制时间间隔最小是0.1
        myText = GetComponent();
        myText.text = "";//初始化文本框
    }
    void Update()
    {
        OnStartWriter();
    }
      /// 
      /// 外部调用此方法,开始实现效果
      /// 
      /// 需要输入的文字,在外部填写后可以不填此参数
    public void StartEffect(string word = "")
    {

        isActive = true;
        if (words == "" && word != null)
        {
            words = word;
        }
        else
        {
            if (word == null)
            {
                Debug.LogError("字符为空");
            }
        }

    }
    /// 
    /// 执行打字任务
    /// 
    void OnStartWriter()
    {

        if (isActive)
        {
            timer += Time.deltaTime;
            if (timer >= charsPerSecond)
            {//判断计时器时间是否到达
                timer = 0;
                currentPos++;
                myText.text = words.Substring(0, currentPos);//刷新文本显示内容

                if (currentPos >= words.Length)
                {
                    OnFinish();
                }
            }

        }
    }
    /// 
    /// 结束打字,初始化数据
    /// 
    void OnFinish()
    {
        isActive = false;
        timer = 0;
        currentPos = 0;
        myText.text = words;
    }




}

2、很多需求是要配合语音效果的,我常用的就是迅捷文字转语音软件了,配合这个脚本可以完美实现语音朗读加文字输入,软件参数如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号