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
release
sushanta chakraborty 2016-09-28 06:56:06 +00:00
parent b75a03963e
commit 18cf84b4d0
1 changed files with 37 additions and 37 deletions

View File

@ -82,6 +82,12 @@ namespace UnityEngine.UI.Extensions
private List<Vector2> positions = new List<Vector2>();
/**
* Little heck to support multiple hrefs with same name
*/
private string previousText = "";
public bool isCreating_m_HrefInfos = true;
/**
* Unity Inspector cant display Dictionary vars,
* so we use this little hack to setup the iconList
@ -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,18 +310,14 @@ 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("<color=" + hyperlinkColor + ">"); // 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 {
if(isCreating_m_HrefInfos) {
var hrefInfo = new HrefInfo
{
startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices
@ -322,39 +325,27 @@ namespace UnityEngine.UI.Extensions
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("</color>");
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();
}
/// <summary>
/// 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.
/// </summary>
/// <returns><c>true</c>, if hrefName already exists in the list of m_HrefInfos, <c>false</c> otherwise.</returns>
/// <param name="hrefName">Href name.</param>
/// <param name="indexAt">Index at.</param>
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;
}
/// <summary>
/// Click event is detected whether to click a hyperlink text
/// </summary>
@ -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;
}
}
}