当前位置: 首页 > news >正文

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

using System.Collections;using System.Collections.Generic;using System.Text.RegularExpressions;using UnityEngine;using UnityEngine.UI;/// summary/// Text组件首行标点符号优化排版工具/// 自动检测并处理行首标点符号避免出现句首标点的尴尬排版/// /summarypublic class TextSymbolWrap : MonoBehaviour{public Text textCom;private string origStr;private string replaceStr;private string finalReplaceStr;/// 标记不换行的空格换行空格Unicode编码为/u0020不换行的/u00A0public static readonly string Non_breaking_space \u00A0;/// 用于匹配标点符号为了不破坏富文本标签所以只匹配指定的符号private readonly string strPunctuation [。?];/// 用于存储text组件中的内容private System.Text.StringBuilder TempText null;/// 用于存储text生成器中的内容private IListUILineInfo 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.GetComponentText();}}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 (tempTxt[tempTxt.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 TextLine[i];int startCharIdx lineInfo.startCharIdx;if (TempText.Length startCharIdx) continue;bool IsPunctuation Regex.IsMatch(TempText[startCharIdx].ToString(), strPunctuation);// 将换行空格改成不换行空格后如果首字符是不换行空格同样需要调整if (TempText[startCharIdx].ToString() Non_breaking_space){IsPunctuation true;}if (IsPunctuation){ChangeIndex startCharIdx;// 回退操作判断提前一个字符后当前首字符是否仍是标点while (IsPunctuation){ChangeIndex - 1;if (ChangeIndex 0) break;IsPunctuation Regex.IsMatch(TempText[ChangeIndex].ToString(), strPunctuation);if (TempText[ChangeIndex].ToString() Non_breaking_space){IsPunctuation true;}}if (ChangeIndex 0) continue;// 在合适的位置手动插入换行符if (TempText[ChangeIndex - 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;}}}
http://www.zskr.cn/news/1374078.html

相关文章:

  • 2026泛塞封密封圈优质品牌推荐:聚四氟乙烯密封圈/铁氟龙密封圈/高分子材料密封圈/O型圈/PEEK密封圈/PU密封圈/选择指南 - 优质品牌商家
  • LLM Weekly(2026.5.11-2026.5.17)
  • CentOS 7 LAMP 服务部署(学习环境)
  • 告别下载量低迷,5套实操方法打通用户增长
  • Win10开机WiFi列表全空?先别慌,按这个‘服务状态排查流程图’走一遍
  • C语言学习:预处理详解
  • 2026电工杯数学建模竞赛A题论文、代码、数据
  • Codex入门19-数据库操作(解放双手:用自然语言写SQL、建表和数据迁移)
  • Codex入门18-批量文件操作(效率神器:一句话批量重命名、格式化、清理几百个文件)
  • 别再手动调阴影了!Godot 4.0 2D光照系统保姆级配置指南(含法线/高光贴图实战)
  • 2026保姆级免费去图片水印教程,这4款微信小程序一键搞定
  • if语句
  • 洛谷官方题单[Java版题解]--【算法1-6】二分查找与二分答案
  • 避坑指南:在Ubuntu 22.04和服务器上成功编译SoftGroup点云分割模型(含gcc降级、sparsehash头文件修复)
  • 【Linux网络编程】进程间关系与守护进程
  • Try和expect的正确使用方式
  • 别再为单细胞数据批次效应发愁了:手把手教你用Harmony算法在R/Seurat中搞定整合
  • 【昇腾CANN】cann-samples示例仓库:快速上手昇腾NPU开发
  • 保姆级教程:用SGImini把物理机Win10的Ghost备份,无损还原到VMware的Win7虚拟机里
  • MacBook新手福音:用Final Cut Pro 10.6.5搞定你的第一门视频课(附保姆级设置与导出指南)
  • 告别数据拼接烦恼!一份教程搞定DMSP与VIIRS夜间灯光数据的融合与校准
  • 别再为立体匹配发愁了!手把手教你用Fusiello法搞定双目相机极线校正(附Python代码)
  • 避坑指南:在openEuler 22.03上配置vsftpd虚拟用户,解决gdbmtool替代db_load的认证问题
  • 2026代运营哪家靠谱:爱采购代运营、爱采购会员、百家号、百度代运营、百度品牌广告、百度官网、矩阵引流、短视频剪辑选择指南 - 优质品牌商家
  • 选型必看!国产RT-Thread才是商用量产最优解
  • 【iOS】底层原理:理解dyld
  • 告别图形界面!5个CUPS命令行技巧,让你在Linux终端高效管理打印机
  • MacBook锁屏别慌!手把手教你用恢复模式+Apple ID重置开机密码(保姆级图文)
  • 昇腾NPU强化学习训练实战——从PPO到GRPO的完整落地
  • 从零开始手把手教你用Python和XFLR5估算小型固定翼无人机的升力系数(附代码)