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

Unity 对话系统 文字打字机效果实现

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

Unity 对话系统 文字打字机效果实现

功能:

1.文字逐个出现

2.支持富文本

3.添加标签可以延迟显示

4.想快速展示完调用函数 ImmediatelyShow

准备:

 

 

代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;

/// 
/// 文本预处理
/// 
public class AdvanceTextPreprocessor : ITextPreprocessor
{
    public Dictionary IntervalDic;

    public AdvanceTextPreprocessor()
    {
        IntervalDic = new Dictionary();
    }
    
    public string PreprocessText(string text)
    {
        IntervalDic.Clear();
        string processingText = text;
        string pattern = @"<(d+)(.d+)?>";
        Match match = Regex.Match(processingText, pattern);
        while (match.Success)
        {
            string lable = match.Value.Substring(1, match.Length - 2);
            
            //Debug.LogError(lable);
            if(float.TryParse(lable, out float result))
            {
                IntervalDic[match.Index - 1] = result;
            }

            processingText = processingText.Remove(match.Index, match.Length);
            
            match = Regex.Match(processingText, pattern);
        }
        return processingText;
    }
}

public class AdvancedText : TextMeshProUGUI
{
    private float m_defaultInterval = 0.06f;
    private int m_typingIndex;

    private Coroutine startCoro;
    //textPreprocessor 接口类型
    private AdvanceTextPreprocessor selfPreprocessor => (AdvanceTextPreprocessor) textPreprocessor;
    
    public AdvancedText()
    {
        textPreprocessor = new AdvanceTextPreprocessor();
    }

    public void ShowTextByTyping(string content)
    {
        SetText(content);
        startCoro = StartCoroutine(Typing());
    }

    public void ImmediatelyShow()
    {
        StopCoroutine(startCoro);
        
        ForceMeshUpdate();
        for (int i = 0; i < m_characterCount; i++)
        {
            SetSingleCharacterAlpha(i, 255);
        }
    }
    
    IEnumerator Typing()
    {
        ForceMeshUpdate();
        for (int i = 0; i < m_characterCount; i++)
        {
            SetSingleCharacterAlpha(i, 0);
        }

        m_typingIndex = 0;
        while (m_typingIndex < m_characterCount)
        {
            if (textInfo.characterInfo[m_typingIndex].isVisible)
            {
                StartCoroutine(FadeInCharacter(m_typingIndex));    
            }
            if (selfPreprocessor.IntervalDic.TryGetValue(m_typingIndex, out float result))
            {
                yield return new WaitForSecondsRealtime(result);    
            }
            else
            {
                yield return new WaitForSecondsRealtime(m_defaultInterval);
            }
            m_typingIndex++;
        }
    }

    private void SetSingleCharacterAlpha(int index, byte newAlpha)
    {
        TMP_CharacterInfo charInfo = textInfo.characterInfo[index];
        //材质实例索引
        int matIndex = charInfo.materialReferenceIndex;
        //顶点索引
        int vertIndex = charInfo.vertexIndex;
        for (int i = 0; i < 4; i++)
        {
            textInfo.meshInfo[matIndex].colors32[vertIndex + i].a = newAlpha;
        }
        UpdateVertexData();
    }

    IEnumerator FadeInCharacter(int index, float duration = 0.5f)
    {
        if (duration <= 0)
        {
            SetSingleCharacterAlpha(index, 255);
        }
        else
        {
            float timer = 0;
            while (timer < duration)
            {
                timer = Math.Min(timer + Time.unscaledDeltaTime, duration);
                SetSingleCharacterAlpha(index, (byte)(timer / duration * 255));
                yield return null;
            }
        }
    }
}

 

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

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

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