IndieGame/client/Assets/Ether/Scripts/Module/Extension/UGUI/Text/TextEx.cs

97 lines
2.8 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: 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;
2024-10-31 10:58:14 +08:00
using UnityEngine.UI;
2024-10-11 10:12:15 +08:00
using UnityEngine;
2024-10-31 10:58:14 +08:00
#if UNITY_EDITOR
using UnityEditor;
#endif
2024-10-11 10:12:15 +08:00
namespace Ether
{
public class TextExPreprocesser : ITextPreprocessor
{
public Dictionary<int, float> IntervalDic;
2024-10-18 18:30:22 +08:00
2024-10-11 10:12:15 +08:00
public TextExPreprocesser()
{
IntervalDic = new Dictionary<int, float>();
}
2024-10-18 18:30:22 +08:00
2024-10-11 10:12:15 +08:00
public string PreprocessText(string text)
{
IntervalDic.Clear();
2024-10-18 18:30:22 +08:00
string processingText = text;
2024-10-11 10:12:15 +08:00
2024-10-18 18:30:22 +08:00
if (!string.IsNullOrEmpty(text))
2024-10-11 10:12:15 +08:00
{
2024-10-18 18:30:22 +08:00
string pattern = "<.*?>";
Match match = Regex.Match(processingText, pattern);
while (match.Success)
{
string label = match.Value.Substring(1, match.Length - 2);
2024-10-11 10:12:15 +08:00
2024-10-18 18:30:22 +08:00
if (float.TryParse(label, out float result))
{
IntervalDic[match.Index - 1] = result;
}
2024-10-11 10:12:15 +08:00
2024-10-18 18:30:22 +08:00
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;
}
2024-10-11 10:12:15 +08:00
return processingText;
}
}
public class TextEx : TextMeshProUGUI
{
public TextEx()
{
textPreprocessor = new TextExPreprocesser();
}
2024-10-31 10:58:14 +08:00
#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
2024-10-11 10:12:15 +08:00
}
}