97 lines
2.8 KiB
C#
97 lines
2.8 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.UI;
|
|
using UnityEngine;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
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;
|
|
|
|
if (!string.IsNullOrEmpty(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();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
[MenuItem("GameObject/UIEx/TextEx", priority = -999)]
|
|
private static void CreateToggleEx(MenuCommand menuCmd)
|
|
{
|
|
GameObject selection = Selection.activeGameObject;
|
|
|
|
CommonExtension.CreateComponent<TextEx>(selection, (obj) =>
|
|
{
|
|
obj.GetComponent<TextEx>().raycastTarget = false;
|
|
});
|
|
}
|
|
|
|
#endif
|
|
}
|
|
}
|