From 9076ec4ea05ca24634212290a9955359d3207546 Mon Sep 17 00:00:00 2001 From: xucian <5688727+xucian@users.noreply.github.com> Date: Thu, 14 Sep 2023 17:10:09 +0300 Subject: [PATCH] Update TMProUGUIHyperlinks increasing max tag length from 128 to 1024. Whoever uses this script probably uses many links, and 128 chars are just not enough. Performance doesn't seem an issue as operations in TMP_Text class appear to only use defined ranges to iterate over m_htmlTag, not its entire length --- Scripts/Runtime/TMProUGUIHyperlinks.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Scripts/Runtime/TMProUGUIHyperlinks.cs b/Scripts/Runtime/TMProUGUIHyperlinks.cs index 80556fb..5b5bec9 100644 --- a/Scripts/Runtime/TMProUGUIHyperlinks.cs +++ b/Scripts/Runtime/TMProUGUIHyperlinks.cs @@ -30,6 +30,7 @@ public class TMProUGUIHyperlinks : MonoBehaviour, IPointerDownHandler, IPointerU [Serializable] public class LinkEvent : UnityEvent { } + private const int RICH_TEXT_TAG_LENGTH_OVERRIDE = 1024; private List startColors = new List(); private TextMeshProUGUI textMeshPro; private Dictionary usedLinks = new Dictionary(); @@ -37,6 +38,26 @@ public class LinkEvent : UnityEvent { } private int pressedLinkIndex = -1; private Camera mainCamera; +#if UNITY_EDITOR + [UnityEditor.InitializeOnLoadMethod] +#else + [RuntimeInitializeOnLoadMethod] +#endif + static void TryFixTMProTagLength() + { + // Fixing TMPro to have a bigger tag length. Currently it's just 128 which is peanuts - links can be very long. + // More specifically, the link tag breaks if the text inside the tag is longer than 128 chars, eg: + // For + // + var fi = typeof(TMP_Text).GetField("m_htmlTag", BindingFlags.NonPublic | BindingFlags.Static); + var arr = fi.GetValue(null) as char[]; + if (arr.Length < RICH_TEXT_TAG_LENGTH_OVERRIDE) + { + Array.Resize(ref arr, RICH_TEXT_TAG_LENGTH_OVERRIDE); + fi.SetValue(null, arr); + } + } + void Awake() { textMeshPro = GetComponent();