unity text 文本符号显示问题与打字机效果的结合

文章介绍了在Unity中使用TextSymbolFit脚本来处理Text组件中符号显示问题的方法,以及如何通过UITextType脚本适配打字机效果,确保文本格式正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题1:unity text显示文本时,符号可能显示在某行的开头的位置
问题2:打字机效果没有适配问题1的脚本

解决方法:
问题1:通过遍历text组件每一行数据(第二行开始),如果是符号,就在它之前的字符前添加换行符
问题2:适配上述脚本

脚本1 解决文本符号显示问题
TextSymbolFit.cs

public class TextSymbolFit : Text
    {
   
        /// <summary>
        /// 用于匹配标点符号
        /// </summary>
        private readonly string strRegex = @"\p{P}";

        /// <summary>
        /// 用于存储text组件中的内容
        /// </summary>
        private System.Text.StringBuilder MExplainText = null;

        /// <summary>
        /// 用于存储text生成器中的内容
        /// </summary>
        private IList<UILineInfo> MExpalinTextLine;

        protected override void OnPopulateMesh(VertexHelper toFill)
        {
   
            base.OnPopulateMesh(toFill);
            StartCoroutine(MClearUpExplainMode(this, text));
        }
        private IEnumerator MClearUpExplainMode(Text _component, string _text)
        {
   
            _component.text = _text;
            yield return new WaitForEndOfFrame();

            MExplainText = new System.Text.StringBuilder(_component.text);
            MExpalinTextLine = _component.cachedTextGenerator.lines;

            int mChangeIndex;

            // 从第二行开始进行检测
            for (int i = 1; i < MExpalinTextLine.Count; i++)
            {
   
                try
                {
   
                    if (MExpalinTextLine[i].startCharIdx >= _component.text.Length) continue;
                    //首位是否有标点
                    bool match = Regex.IsMatch(MExplainText.ToString()[MExpalinTextLine[i].startCharIdx].ToString(), strRegex);

                    if (match)
                    {
   
                        mChangeIndex = MExpalinTextLine[i].startCharIdx - 1;
                        // 解决联系多个都是标点符号的问题
                        for (int j = MExpalinTextLine[i].startCharIdx - 1; j > 0; j--)
                        {
   
                            match = Regex.IsMatch(MExplainText.ToString()[j].ToString(), strRegex);
                            if (match)
                            {
   
                                mChangeIndex--;
                            }
                            else
                            {
   
                                break;
                            }
                        }

                        MExplainText.Insert(mChangeIndex, "\n");
                    }
                }
                catch (Exception e)
                {
   
                    Debug.LogException(e);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值