From 16dba0ff4d2eed7f5a4a7c37640f158ee9f73d8c Mon Sep 17 00:00:00 2001
From: lcy <1459594991@qq.com>
Date: 星期五, 05 六月 2026 19:22:43 +0800
Subject: [PATCH] 669 子 【武将】武将系统 / 时装新增开服天数显示入口字段
---
Main/System/Message/RichText.cs | 143 +++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 130 insertions(+), 13 deletions(-)
diff --git a/Main/System/Message/RichText.cs b/Main/System/Message/RichText.cs
index 6eed7b9..044c8bc 100644
--- a/Main/System/Message/RichText.cs
+++ b/Main/System/Message/RichText.cs
@@ -181,7 +181,7 @@
unline = transform.GetComponentInChildren<TextUnline>();
if (unline == null)
{
- GameObject obj = Resources.Load("UIComp/TextUnline") as GameObject;
+ GameObject obj = BuiltInLoader.LoadPrefab("TextUnline");
// GameObject obj = UILoader.LoadPrefab("TextUnline") as GameObject;
obj = Instantiate(obj);
obj.transform.SetParent(transform);
@@ -504,8 +504,26 @@
{
font = FontUtility.preferred;
}
+
+ // 娣诲姞 null 妫�鏌�
+ if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null || string.IsNullOrEmpty(m_OutputText))
+ {
+ return 0f;
+ }
+
var settings = GetGenerationSettings(Vector2.zero);
- return cachedTextGeneratorForLayout.GetPreferredWidth(m_OutputText, settings) / pixelsPerUnit;
+
+ float width = 0f;
+ try
+ {
+ width = cachedTextGeneratorForLayout.GetPreferredWidth(m_OutputText, settings) / pixelsPerUnit;
+ }
+ catch (Exception ex)
+ {
+ Debug.LogError($"GetPreferredWidth failed: {ex.Message}");
+ width = 0f;
+ }
+ return width;
}
}
@@ -517,9 +535,26 @@
{
font = FontUtility.preferred;
}
+
+ // 娣诲姞 null 妫�鏌�
+ if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null || string.IsNullOrEmpty(m_OutputText))
+ {
+ return 0f;
+ }
+
var settings = GetGenerationSettings(new Vector2(rectTransform.rect.size.x, 0.0f));
- float _height = cachedTextGeneratorForLayout.GetPreferredHeight(m_OutputText, settings) / pixelsPerUnit;
- return _height;
+
+ float height = 0f;
+ try
+ {
+ height = cachedTextGeneratorForLayout.GetPreferredHeight(m_OutputText, settings) / pixelsPerUnit;
+ }
+ catch (Exception ex)
+ {
+ Debug.LogError($"GetPreferredHeight failed: {ex.Message}");
+ height = 0f;
+ }
+ return height;
}
}
#endregion
@@ -619,6 +654,13 @@
#region 鎵ц浜嬩欢
public void OnPointerClick(PointerEventData eventData)
{
+ // 妫�鏌ョ粍浠舵槸鍚﹀凡绂佺敤鎴栭攢姣侊紝閬垮厤绌烘寚閽堣В寮曠敤
+ if (!this.isActiveAndEnabled)
+ {
+ return;
+ }
+
+ OnClick?.Invoke();
if (HrefClick)
{
Vector2 lp;
@@ -640,7 +682,6 @@
}
}
}
- if (OnClick != null) OnClick();
}
public void OnImgClick()
@@ -665,6 +706,16 @@
private static StringBuilder textBuilder = new StringBuilder();
private Dictionary<int, Match> matchDics = new Dictionary<int, Match>();
+
+ // 瀛楃瀹藉害缂撳瓨锛岄伩鍏嶉噸澶嶈绠楃浉鍚屽瓧绗�
+ private Dictionary<string, float> charWidthCache = new Dictionary<string, float>();
+
+ protected override void OnDisable()
+ {
+ base.OnDisable();
+ // 缁勪欢绂佺敤鏃舵竻绌虹紦瀛橈紝闃叉鍐呭瓨娉勬紡
+ charWidthCache.Clear();
+ }
private bool IsModifySize(int _index,out int _size)
{
@@ -724,6 +775,14 @@
{
font = FontUtility.preferred;
}
+
+ // 娣诲姞 null 妫�鏌�
+ if (font == null || rectTransform == null || cachedTextGeneratorForLayout == null)
+ {
+ Debug.LogWarning("SetFitterSize: font, rectTransform or cachedTextGeneratorForLayout is null");
+ return;
+ }
+
var settings = GetGenerationSettings(Vector2.zero);
float cache = 0;
@@ -732,6 +791,7 @@
float ratio = GetResolutionRatio();
matchDics.Clear();
+ charWidthCache.Clear();
foreach (Match match in ImgAnalysis.Unity_Img_Regex.Matches(fitterText))
{
@@ -802,7 +862,8 @@
}
else
{
- cache = cachedTextGeneratorForLayout.GetPreferredWidth(match.Value, settings) * ratio;
+ cache = GetCharWidthCached(match.Value, settings, ratio);
+
if (width + cache > (rectTransform.rect.width - 5))
{
CacluHrefAndImgIndex(Mathf.Max(0, i - 1));
@@ -820,19 +881,16 @@
else
{
var _size = 0;
- var _cacheFontSize = fontSize;
+ // 涓嶅啀淇敼 fontSize锛屼娇鐢ㄤ复鏃� TextGenerator 璁$畻涓嶅悓瀛楀彿鐨勫搴�
if (_modifySize && IsModifySize(i, out _size))
{
- fontSize = _size;
- settings = GetGenerationSettings(Vector2.zero);
- cache = cachedTextGeneratorForLayout.GetPreferredWidth(fitterText[i].ToString(), settings) * ratio;
- fontSize = _cacheFontSize;
- settings = GetGenerationSettings(Vector2.zero);
+ cache = GetCharWidthWithCustomSize(fitterText[i], _size, ratio);
}
else
{
- cache = cachedTextGeneratorForLayout.GetPreferredWidth(fitterText[i].ToString(), settings) * ratio;
+ cache = GetCharWidthCached(fitterText[i].ToString(), settings, ratio);
}
+
if (width + cache > (rectTransform.rect.width - 5))
{
CacluHrefAndImgIndex(Mathf.Max(0, i - 1));
@@ -850,6 +908,65 @@
m_OutputText = textBuilder.ToString();
}
+ /// <summary>
+ /// 鑾峰彇瀛楃瀹藉害锛堝甫缂撳瓨锛�
+ /// </summary>
+ private float GetCharWidthCached(string charStr, TextGenerationSettings settings, float ratio)
+ {
+ string cacheKey = $"{fontSize}_{charStr}";
+
+ if (charWidthCache.ContainsKey(cacheKey))
+ {
+ return charWidthCache[cacheKey];
+ }
+
+ float width = 0;
+ try
+ {
+ width = cachedTextGeneratorForLayout.GetPreferredWidth(charStr, settings) * ratio;
+ }
+ catch (Exception ex)
+ {
+ Debug.LogError($"GetPreferredWidth failed for '{charStr}': {ex.Message}");
+ width = 0;
+ }
+
+ charWidthCache[cacheKey] = width;
+ return width;
+ }
+
+ /// <summary>
+ /// 浣跨敤鑷畾涔夊瓧鍙疯绠楀瓧绗﹀搴︼紙涓嶄慨鏀瑰師濮� Text 缁勪欢鐨� fontSize锛�
+ /// </summary>
+ private float GetCharWidthWithCustomSize(char c, int customFontSize, float ratio)
+ {
+ string charStr = c.ToString();
+ string cacheKey = $"{customFontSize}_{charStr}";
+
+ if (charWidthCache.ContainsKey(cacheKey))
+ {
+ return charWidthCache[cacheKey];
+ }
+
+ float width = 0;
+ try
+ {
+ // 鍒涘缓涓存椂鐨� TextGenerationSettings锛屼娇鐢ㄨ嚜瀹氫箟瀛楀彿
+ TextGenerationSettings tempSettings = GetGenerationSettings(Vector2.zero);
+ tempSettings.fontSize = customFontSize;
+
+ width = cachedTextGeneratorForLayout.GetPreferredWidth(charStr, tempSettings) * ratio;
+ }
+ catch (Exception ex)
+ {
+ Debug.LogError($"GetPreferredWidth failed for '{charStr}' with fontSize {customFontSize}: {ex.Message}");
+ width = 0;
+ }
+
+ charWidthCache[cacheKey] = width;
+ return width;
+ }
+
private void CacluHrefAndImgIndex(int index)
{
for (int i = 0; i < m_ImgList.Count; i++)
--
Gitblit v1.8.0