Unity 避免Text组件每行开头不是字符和空格,适配不同分辨率

using System.Collections;

using System.Collections.Generic;

using System.Text.RegularExpressions;

using UnityEngine;

using UnityEngine.UI;

/// <summary>

/// Text组件首行标点符号优化排版工具

/// 自动检测并处理行首标点符号,避免出现句首标点的尴尬排版

/// </summary>

public class TextSymbolWrap : MonoBehaviour

{

public Text textCom;

private string origStr;

private string replaceStr;

private string finalReplaceStr;

/// 标记不换行的空格(换行空格Unicode编码为/u0020,不换行的/u00A0)

public static readonly string Non_breaking_space = "\u00A0";

/// 用于匹配标点符号,为了不破坏富文本标签,所以只匹配指定的符号

private readonly string strPunctuation = @",。??!!...";

/// 用于存储text组件中的内容

private System.Text.StringBuilder TempText = null;

/// 用于存储text生成器中的内容

private IList<UILineInfo> TextLine;

private int screenWidth = 0;

private int screenHeight = 0;

//在替换后的文本最后面添加一个看不到的字符,以用于辨别当前输入的文本是不是原始文本

private string endString = " ";

private bool isReplacing = false;

private void OnEnable()

{

isReplacing = false;

CheckTextComponent();

CheckScreenSizeChange();

ReplaceTextFun();

}

void Update()

{

// 当屏幕分辨率发生变化时,恢复原文本并重新计算,防止排版错乱

if (CheckScreenSizeChange())

{

if (textCom != null && !string.IsNullOrEmpty(origStr))

{

textCom.text = origStr;

replaceStr = "";

finalReplaceStr = "";

}

}

CheckReplaceText();

}

private bool CheckScreenSizeChange()

{

if (Screen.width != screenWidth || Screen.height != screenHeight)

{

screenWidth = Screen.width;

screenHeight = Screen.height;

return true;

}

return false;

}

private void CheckTextComponent()

{

if (textCom == null)

{

textCom = this.gameObject.GetComponent<Text>();

}

}

private void CheckReplaceText()

{

if (textCom == null || !CheckTextIsChange()) return;

ReplaceTextFun();

}

private void ReplaceTextFun()

{

if (isReplacing) return;

replaceStr = "";

finalReplaceStr = "";

StartCoroutine("ClearUpPunctuationMode", textCom);

}

private bool CheckTextIsChange()

{

if (textCom == null) return false;

return !string.Equals(textCom.text, finalReplaceStr);

}

IEnumerator ClearUpPunctuationMode(Text _component)

{

isReplacing = true;

// 不能立刻进行计算,要等渲染完上一帧才计算,故延迟60毫秒(约两帧多)

yield return new WaitForSeconds(0.06f);

if (string.IsNullOrEmpty(_component.text))

{

isReplacing = false;

}

else

{

string tempTxt = _component.text;

bool isOrigStr = false;

// 如果结尾没有特指标记字符,就认为是业务刚赋的值(即原始字符串)

if (tempTxttempTxt.Length - 1.ToString() != endString)

{

origStr = tempTxt;

isOrigStr = true;

}

TextLine = _component.cachedTextGenerator.lines;

int ChangeIndex = -1;

TempText = new System.Text.StringBuilder(_component.text);

// 从1开始遍历,只看第二行及以后的首字符

for (int i = 1; i < TextLine.Count; i++)

{

UILineInfo lineInfo = TextLinei;

int startCharIdx = lineInfo.startCharIdx;

if (TempText.Length <= startCharIdx) continue;

bool IsPunctuation = Regex.IsMatch(TempTextstartCharIdx.ToString(), strPunctuation);

// 将换行空格改成不换行空格后,如果首字符是不换行空格同样需要调整

if (TempTextstartCharIdx.ToString() == Non_breaking_space)

{

IsPunctuation = true;

}

if (IsPunctuation)

{

ChangeIndex = startCharIdx;

// 回退操作:判断提前一个字符后当前首字符是否仍是标点

while (IsPunctuation)

{

ChangeIndex -= 1;

if (ChangeIndex < 0) break;

IsPunctuation = Regex.IsMatch(TempTextChangeIndex.ToString(), strPunctuation);

if (TempTextChangeIndex.ToString() == Non_breaking_space)

{

IsPunctuation = true;

}

}

if (ChangeIndex < 0) continue;

// 在合适的位置手动插入换行符

if (TempTextChangeIndex - 1 != '\n')

TempText.Insert(ChangeIndex, "\n");

}

}

replaceStr = TempText.ToString();

// 如果最终排版有改动

if (!string.Equals(tempTxt, replaceStr))

{

if (isOrigStr)

{

replaceStr += endString;

}

_component.text = replaceStr;

}

else

{

// 计算后结果一致,证明当前文本排版已合法,记录状态防止死循环重复验证

finalReplaceStr = replaceStr;

}

isReplacing = false;

}

}

}

相关推荐
辻弋2013 天前
五年前的旅行视频糊成马赛克?Video2X用Real-ESRGAN逐帧重建细节,但只支持Windows、集显用户建议直接放弃
服务器·数据库·windows·游戏引擎·电脑
Martina_03214 天前
AI生成3D场景导入 Unity/Unreal 后,NPC 总串层?用6步检查导航高度与跨层连接
人工智能·游戏·数学建模·3d·unity·自然语言处理·aigc
小贺儿开发4 天前
Unity 局域网遥控幻灯片展示工具 3.0
科技·http·unity·工具·ppt·通信·网页
WarPigs6 天前
Unity对象不应该使用null传播的问题
unity
每日出拳老爷子7 天前
【VR】【Unity】抓走转放系列21|一天搭完门和抽屉工具槽:小场景按这个次序拼装
unity·vr·驱动·排错·snap zone
淡海水7 天前
02-02-原理篇-分代GC
算法·unity·c#·游戏引擎·.net·gc
小贺儿开发8 天前
Unity 文物新生 会讲故事的文物展墙
人工智能·科技·unity·ai·视频·互动·演示
淡海水8 天前
02-03-原理篇-大对象堆
unity·c#·游戏引擎·.net·gc
WarPigs8 天前
Unity在Create菜单添加创建自定义脚本模板选项
unity
郝学胜-神的一滴9 天前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建