From ddbfd9b7ef27c3ce5fa0f746f2c9109e588b6450 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Wed, 8 Jan 2025 22:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`TextMeshPro`=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=97=B6=E6=AE=8B=E7=95=99=E7=BB=84=E4=BB=B6=E5=BD=B1?= =?UTF-8?q?=E5=93=8D`Text`=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation~/zh/changelog.md | 2 ++ Runtime/Internal/Utilities/ChartHelper.cs | 27 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index 4f4657a2..d211113e 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -79,6 +79,8 @@ slug: /changelog ## master +* (2025.01.08) 修复`TextMeshPro`关闭时残留组件影响`Text`初始化的问题 + ## v3.13.0 版本要点: diff --git a/Runtime/Internal/Utilities/ChartHelper.cs b/Runtime/Internal/Utilities/ChartHelper.cs index c0bef4fc..6c1b31bf 100644 --- a/Runtime/Internal/Utilities/ChartHelper.cs +++ b/Runtime/Internal/Utilities/ChartHelper.cs @@ -204,6 +204,25 @@ namespace XCharts.Runtime } } + public static void RemoveTMPComponents(GameObject gameObject) + { + var coms = gameObject.GetComponents(); + foreach (var com in coms) + { + if (com.GetType().FullName.Contains("TMPro")) + { +#if UNITY_EDITOR + if (!Application.isPlaying) + GameObject.DestroyImmediate(com as UnityEngine.Object); + else + GameObject.Destroy(com as UnityEngine.Object); +#else + GameObject.Destroy(com as UnityEngine.Object); +#endif + } + } + } + [System.Obsolete("Use EnsureComponent instead")] public static T GetOrAddComponent(Transform transform) where T : Component { @@ -239,7 +258,13 @@ namespace XCharts.Runtime { if (gameObject.GetComponent() == null) { - return gameObject.AddComponent(); + var com = gameObject.AddComponent(); + if (com == null) + { + RemoveTMPComponents(gameObject); + return gameObject.AddComponent(); + } + return com; } else {