75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
/********************************************************************
|
|
文件: TextEx.cs
|
|
作者: 梦语
|
|
邮箱: 1982614048@qq.com
|
|
创建时间: 2024/04/04 01:31:38
|
|
最后修改: 梦语
|
|
最后修改时间: 2024/04/22 13:29:55
|
|
功能: 高级自定义文字组件
|
|
*********************************************************************/
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace Ether
|
|
{
|
|
public class TextExPreprocesser : ITextPreprocessor
|
|
{
|
|
public Dictionary<int, float> IntervalDic;
|
|
|
|
public TextExPreprocesser()
|
|
{
|
|
IntervalDic = new Dictionary<int, float>();
|
|
}
|
|
|
|
public string PreprocessText(string text)
|
|
{
|
|
IntervalDic.Clear();
|
|
|
|
string processingText = text;
|
|
string pattern = "<.*?>";
|
|
Match match = Regex.Match(processingText, pattern);
|
|
while (match.Success)
|
|
{
|
|
string label = match.Value.Substring(1, match.Length - 2);
|
|
|
|
if (float.TryParse(label, out float result))
|
|
{
|
|
IntervalDic[match.Index - 1] = result;
|
|
}
|
|
|
|
processingText = processingText.Remove(match.Index, match.Length);
|
|
|
|
if (Regex.IsMatch(label, "^sprite=.+"))
|
|
{
|
|
processingText = processingText.Insert(match.Index, "*");
|
|
}
|
|
|
|
match = Regex.Match(processingText, pattern);
|
|
}
|
|
|
|
// . 代表任意字符!!!
|
|
// * 代表前一个字符出现零次或多次
|
|
// + 代表前一个字符出现一次或多次
|
|
// ? 代表前一个字符出现零次或一次
|
|
pattern = @"(<(\d+)(\.\d+)?>)|(</r>)|(<r=.*?>)";
|
|
processingText = Regex.Replace(processingText, pattern, "");
|
|
|
|
processingText = text;
|
|
|
|
return processingText;
|
|
}
|
|
}
|
|
|
|
public class TextEx : TextMeshProUGUI
|
|
{
|
|
public TextEx()
|
|
{
|
|
textPreprocessor = new TextExPreprocesser();
|
|
}
|
|
}
|
|
}
|