问题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);