From 18cf84b4d04a929223a838ffd89363fd6addbe3a Mon Sep 17 00:00:00 2001 From: sushanta chakraborty Date: Wed, 28 Sep 2016 06:56:06 +0000 Subject: [PATCH] TextPic.cs Added Fix for href tag, which was not working. Added support for multiple href tag with same name. --HG-- branch : sushanta1991/textpiccs-was-not-working-for-href-unit-1474979920532 --- Scripts/Controls/TextPic.cs | 74 ++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/Scripts/Controls/TextPic.cs b/Scripts/Controls/TextPic.cs index 43eacf4..c56e592 100644 --- a/Scripts/Controls/TextPic.cs +++ b/Scripts/Controls/TextPic.cs @@ -81,6 +81,12 @@ namespace UnityEngine.UI.Extensions //private bool selected = false; private List positions = new List(); + + /** + * Little heck to support multiple hrefs with same name + */ + private string previousText = ""; + public bool isCreating_m_HrefInfos = true; /** * Unity Inspector cant display Dictionary vars, @@ -97,6 +103,7 @@ namespace UnityEngine.UI.Extensions iconList.Add(icon.name, icon.sprite); } } + Reset_m_HrefInfos (); } protected void UpdateQuadImage() @@ -290,7 +297,7 @@ namespace UnityEngine.UI.Extensions protected string GetOutputText() { s_TextBuilder.Length = 0; - //m_HrefInfos.Clear(); + var indexText = 0; fixedString = this.text; if (inspectorIconList != null && inspectorIconList.Length > 0) @@ -303,58 +310,42 @@ namespace UnityEngine.UI.Extensions } } } + int count = 0; foreach (Match match in s_HrefRegex.Matches(fixedString)) { s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText)); s_TextBuilder.Append(""); // Hyperlink color var group = match.Groups[1]; - int foundAtIndex = -1; - // checking if m_HrefInfos element with same name already exists than just updating it with new values, instead of recreating it without bound data (which is important for href tag) - if(HrefInfosDoesExists(group.Value,out foundAtIndex)) { - m_HrefInfos[foundAtIndex].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices; - m_HrefInfos[foundAtIndex].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3; - } else { - var hrefInfo = new HrefInfo - { - startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices - endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, - name = group.Value - }; - m_HrefInfos.Add(hrefInfo); - } + if(isCreating_m_HrefInfos) { + var hrefInfo = new HrefInfo + { + startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices + endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3, + name = group.Value + }; + m_HrefInfos.Add(hrefInfo); + } else { + if(m_HrefInfos.Count > 0) { + m_HrefInfos[count].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices; + m_HrefInfos[count].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3; + count++; + } + } s_TextBuilder.Append(match.Groups[2].Value); s_TextBuilder.Append(""); indexText = match.Index + match.Length; } + // we should create array only once or if there is any change in the text + if(isCreating_m_HrefInfos) + isCreating_m_HrefInfos = false; + s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText)); return s_TextBuilder.ToString(); } - /// - /// If Href exists than just modify its startIndex and endIndex, dont clear the whole array of m_HrefInfos which also clears the previously created boxes for href. - /// - /// true, if hrefName already exists in the list of m_HrefInfos, false otherwise. - /// Href name. - /// Index at. - private bool HrefInfosDoesExists(string hrefName, out int indexAt) { - bool flag = false; - int foundAtIndex = -1; - foreach(var hrefInfoTest in m_HrefInfos){ - if(hrefInfoTest.name == hrefName) { - foundAtIndex++; - flag = true; - break; - } else { - foundAtIndex++; - } - } - indexAt = foundAtIndex; - return flag; - } - /// /// Click event is detected whether to click a hyperlink text /// @@ -454,6 +445,15 @@ namespace UnityEngine.UI.Extensions culled_ImagesPool.Clear(); clearImages = false; } + if( previousText != text) + Reset_m_HrefInfos (); + } + + // Reseting m_HrefInfos array if there is any change in text + void Reset_m_HrefInfos () { + previousText = text; + m_HrefInfos.Clear(); + isCreating_m_HrefInfos = true; } } } \ No newline at end of file