UnityText组件排版-正则排除标点为行首解决方

文本web排版工具

1.Unicode的三种空格编码有三种空格格式
1.不间断空格 \u00A0 不换行空格(显示效果和\u0020一致,主要用途用于禁止自动换行)
2.半角空格 \u0020 换行空格(平时按空格键敲出来的空格)
3.全角空格 \u3000 不换行空格
2.一个“全角空格”与“两个半角空格”是不相同的,一个"全角空格"是占用两个字符,两个"半角空格"是分别占用两个字符,而在排版的时候,字符于字符之间是有字符间距的,所以"一个全角字符"与"两个半角空格"是不同的。
3.空格与普通字符显示宽度不一致的问题。字体问题,字体中的文字(也就是放有字的那个图片所切割的单个文字)一般不是等宽的(也就是没有等宽切割)。你可以使用等宽字体,但是不能保证用户电脑装有那种字体。
4.字体序号对齐,如果字体为等宽字体,如word的默认字体一般是等宽字体,如果字体不等宽,无法自动对齐。解决方法一:可以将带序号部分的行文本单独拆出来进行对齐进行排版。(这种方法不适用于会存在文本扩容的需求,因为Vertical/Horizontal LayOut Group会自动将某种方法的统一对齐,所以慎用);解决方法二:获取序号部分无法对齐的字的顶点(一个文字其实就是一个图片,两个三角面,六个顶点坐标,将这六个顶点的位置进行偏移可以实现改变文字间隔的效果,但是有个很致命的缺点,无法识别空格已经换行符号等,在读取文本中某个文字时读取不到想要的这个文字。

代码实现UnityText文本排版去掉行首标点符号

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class TextPro : Text
{
    /// <summary>
    /// 用于匹配标点符号(正则表达式)
    /// </summary>
    private readonly string strRegex = @"(\!|\?|\,|\。|\《|\》|\(|\)|\(|\)|\:|\“|\‘|\、|\;|\+|\-|\·|\#|\¥|\;|\”|\【|\】|\——|\/)"; 
    /// <summary>
    /// 用于存储text组件中的内容
    /// </summary>
    private System.Text.StringBuilder MExplainText = null;
    /// <summary>
    /// 用于存储text生成器中的内容
    /// </summary>
    private IList<UILineInfo> MExpalinTextLine; 
    protected override void UpdateGeometry()
    {
        //执行N多次
        base.UpdateGeometry();
    }
    public void SetStringFormat(string content)
    {

    }
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        //执行2次
        base.OnPopulateMesh(toFill);
        //StartCoroutine(IClearUpExplainMode(this, text));
    }
    /// <summary>
    /// 此方法可行。暂未发现断行乱象
    /// </summary>
    public override void SetVerticesDirty()
    { 
        var settings = GetGenerationSettings(rectTransform.rect.size);
        cachedTextGenerator.Populate(text, settings);

        StringBuilder stringBuilder = new StringBuilder(text);
        IList<UILineInfo> lineList = cachedTextGenerator.lines;
        int changeIndex = -1;
        for (int i = 1; i < lineList.Count; i++)
        {
            bool isMark = Regex.IsMatch(text[lineList[i].startCharIdx].ToString(), strRegex);

            if (isMark)
            {
                bool b = Regex.IsMatch(text[lineList[i].startCharIdx].ToString(), strRegex);
                if (b)
                {
                    changeIndex = lineList[i].startCharIdx - 1;
                    break;
                }
            }
        }

        if (changeIndex >= 0)
        {
            stringBuilder.Insert(changeIndex, '\n');
            text = stringBuilder.ToString();
        }

        base.SetVerticesDirty();
    } 
    /// <summary>
    /// 会出现超出数组大小的问题导致断行乱象
    /// </summary>
    /// <param name="_component"></param>
    /// <param name="_text"></param>
    /// <returns></returns>
    IEnumerator IClearUpExplainMode(Text _component, string _text)
    {
        _component.text = _text;
        //如果直接执行下边方法的话,那么_component.cachedTextGenerator.lines将会获取的是之前text中的内容,而不是_text的内容,所以需要等待一下
        yield return new WaitForSeconds(0.005f);
        MExpalinTextLine = _component.cachedTextGenerator.lines;
        MExplainText = new System.Text.StringBuilder(_component.text);  
        try
        {
            for (int i = 1; i < MExpalinTextLine.Count; i++)
            {
                int CheckId = MExpalinTextLine[i].startCharIdx;
                //首位是否有标点
                while (Regex.IsMatch(_component.text[CheckId].ToString(), strRegex))
                {
                    CheckId--;
                    if (CheckId == 0 || CheckId - 1 == 0)
                    {
                        break;
                    }
                    //不考虑标点符号前有人为换行的情况
                    if (_component.text[CheckId].ToString() == "\n")
                    {
                        MExplainText.Remove(CheckId, 1);
                    }
                    else
                    {
                        MExplainText.Insert(CheckId, '\u00A0');
                    }
                }
            }
        }
        catch (System.Exception e)
        {
            print(e.Message);
        }

        _component.text = MExplainText.ToString();
    }
}

TestMeshProUGUI设置中文显示方法:(有自带行首去标点的功能,但中文动态设置显示需要研究)

1:创建UGUI => Text (TMP)然后会弹出提示导入相关资源包,点击导入。
2:Window => TextMeshPro => FontAssetCreator创建该资源的字体文件;
3:SourceFontFile => 选择目标字体文件。
4:AtlasResoulution => 设置大小
5:CharacterSet => 选择 CharactersFromFile 需要显示的中文文字。可以为中文常用5000字等等。。也可以直接用需要显示的问题作为模板。
6:CharacterFile => 选择文件。然后点击生成并保存,再将字体设置为保存的这个就行了。。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有点朦

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值