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

功能:

1.文字逐个出现

2.支持富文本

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

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

准备:

 

 

代码:

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

/// <summary>
/// 文本预处理
/// </summary>
public class AdvanceTextPreprocessor : ITextPreprocessor
{
    public Dictionary<int, float> IntervalDic;

    public AdvanceTextPreprocessor()
    {
        IntervalDic = new Dictionary<int, float>();
    }
    
    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;
            }
        }
    }
}

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值