TextPic.cs was not working for href.
Unity-UI-Extensions helped me a lot thanks for creating it. Today i was trying href tag provided with TextPic.cs, but unfortunately it was not working. I figured out that, OnPopulateMesh() was creating hrefInfo.boxes but at the end when OnPopulateMesh() calls UpdateQuadImage(), hrefInfo.boxes.count was coming 0. This was little strange but i saw that GetOutputText() was re-creating m_HrefInfos array from scratch but it didn't include the code to add boxes, so this was the issue. so what i did is, i commented this line m_HrefInfos.Clear(); in GetOutputText() and added few lines of code to check if m_HrefInfos elements exists than don't destroy it, just update it with new values. This helped in resolving the issue. Now i was getting onClick event on href tag. Thanks --HG-- branch : sushanta1991/textpiccs-was-not-working-for-href-unit-1474979920532pull/413/head
parent
154c9c8d31
commit
292fb39c93
|
@ -10,421 +10,456 @@ using UnityEngine.EventSystems;
|
||||||
|
|
||||||
namespace UnityEngine.UI.Extensions
|
namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
// Image according to the label inside the name attribute to load, read from the Resources directory. The size of the image is controlled by the size property.
|
// Image according to the label inside the name attribute to load, read from the Resources directory. The size of the image is controlled by the size property.
|
||||||
// Use: <quad name=NAME size=25 width=1 />
|
// Use: <quad name=NAME size=25 width=1 />
|
||||||
[AddComponentMenu("UI/Extensions/TextPic")]
|
[AddComponentMenu("UI/Extensions/TextPic")]
|
||||||
|
|
||||||
[ExecuteInEditMode] // Needed for culling images that are not used //
|
|
||||||
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Image Pool
|
|
||||||
/// </summary>
|
|
||||||
private readonly List<Image> m_ImagesPool = new List<Image>();
|
|
||||||
private readonly List<GameObject> culled_ImagesPool = new List<GameObject>();
|
|
||||||
private bool clearImages = false;
|
|
||||||
|
|
||||||
/// <summary>
|
[ExecuteInEditMode] // Needed for culling images that are not used //
|
||||||
/// Vertex Index
|
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
|
||||||
/// </summary>
|
{
|
||||||
private readonly List<int> m_ImagesVertexIndex = new List<int>();
|
/// <summary>
|
||||||
|
/// Image Pool
|
||||||
|
/// </summary>
|
||||||
|
private readonly List<Image> m_ImagesPool = new List<Image>();
|
||||||
|
private readonly List<GameObject> culled_ImagesPool = new List<GameObject>();
|
||||||
|
private bool clearImages = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Regular expression to replace
|
/// Vertex Index
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Regex s_Regex =
|
private readonly List<int> m_ImagesVertexIndex = new List<int>();
|
||||||
new Regex(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
|
|
||||||
|
|
||||||
private string fixedString;
|
/// <summary>
|
||||||
|
/// Regular expression to replace
|
||||||
|
/// </summary>
|
||||||
|
private static readonly Regex s_Regex =
|
||||||
|
new Regex(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
|
||||||
|
|
||||||
public override void SetVerticesDirty()
|
private string fixedString;
|
||||||
{
|
|
||||||
base.SetVerticesDirty();
|
|
||||||
UpdateQuadImage();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
public override void SetVerticesDirty()
|
||||||
protected override void OnValidate()
|
{
|
||||||
{
|
base.SetVerticesDirty();
|
||||||
base.OnValidate();
|
UpdateQuadImage();
|
||||||
UpdateQuadImage();
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// <summary>
|
#if UNITY_EDITOR
|
||||||
/// After parsing the final text
|
protected override void OnValidate()
|
||||||
/// </summary>
|
{
|
||||||
private string m_OutputText;
|
base.OnValidate();
|
||||||
|
UpdateQuadImage();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
[System.Serializable]
|
/// <summary>
|
||||||
public struct IconName
|
/// After parsing the final text
|
||||||
{
|
/// </summary>
|
||||||
public string name;
|
private string m_OutputText;
|
||||||
public Sprite sprite;
|
|
||||||
}
|
|
||||||
public IconName[] inspectorIconList;
|
|
||||||
|
|
||||||
private Dictionary<string, Sprite> iconList = new Dictionary<string, Sprite>();
|
[System.Serializable]
|
||||||
|
public struct IconName
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public Sprite sprite;
|
||||||
|
}
|
||||||
|
public IconName[] inspectorIconList;
|
||||||
|
|
||||||
public float ImageScalingFactor = 1;
|
private Dictionary<string, Sprite> iconList = new Dictionary<string, Sprite>();
|
||||||
|
|
||||||
// Write the name or hex value of the hyperlink color
|
public float ImageScalingFactor = 1;
|
||||||
public string hyperlinkColor = "blue";
|
|
||||||
|
|
||||||
// Offset image by x, y
|
// Write the name or hex value of the hyperlink color
|
||||||
[SerializeField]
|
public string hyperlinkColor = "blue";
|
||||||
public Vector2 imageOffset = Vector2.zero;
|
|
||||||
|
|
||||||
private Button button;
|
// Offset image by x, y
|
||||||
|
[SerializeField]
|
||||||
|
public Vector2 imageOffset = Vector2.zero;
|
||||||
|
|
||||||
//Commented out as private and not used.. Yet?
|
private Button button;
|
||||||
//private bool selected = false;
|
|
||||||
|
|
||||||
private List<Vector2> positions = new List<Vector2>();
|
//Commented out as private and not used.. Yet?
|
||||||
|
//private bool selected = false;
|
||||||
|
|
||||||
/**
|
private List<Vector2> positions = new List<Vector2>();
|
||||||
|
|
||||||
|
/**
|
||||||
* Unity Inspector cant display Dictionary vars,
|
* Unity Inspector cant display Dictionary vars,
|
||||||
* so we use this little hack to setup the iconList
|
* so we use this little hack to setup the iconList
|
||||||
*/
|
*/
|
||||||
new void Start()
|
new void Start()
|
||||||
{
|
{
|
||||||
button = GetComponent<Button>();
|
button = GetComponent<Button>();
|
||||||
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
||||||
{
|
{
|
||||||
foreach (IconName icon in inspectorIconList)
|
foreach (IconName icon in inspectorIconList)
|
||||||
{
|
{
|
||||||
// Debug.Log(icon.sprite.name);
|
// Debug.Log(icon.sprite.name);
|
||||||
iconList.Add(icon.name, icon.sprite);
|
iconList.Add(icon.name, icon.sprite);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateQuadImage()
|
protected void UpdateQuadImage()
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab)
|
if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
m_OutputText = GetOutputText();
|
m_OutputText = GetOutputText();
|
||||||
m_ImagesVertexIndex.Clear();
|
m_ImagesVertexIndex.Clear();
|
||||||
foreach (Match match in s_Regex.Matches(m_OutputText))
|
foreach (Match match in s_Regex.Matches(m_OutputText))
|
||||||
{
|
{
|
||||||
var picIndex = match.Index;
|
var picIndex = match.Index;
|
||||||
var endIndex = picIndex * 4 + 3;
|
var endIndex = picIndex * 4 + 3;
|
||||||
m_ImagesVertexIndex.Add(endIndex);
|
m_ImagesVertexIndex.Add(endIndex);
|
||||||
|
|
||||||
m_ImagesPool.RemoveAll(image => image == null);
|
m_ImagesPool.RemoveAll(image => image == null);
|
||||||
if (m_ImagesPool.Count == 0)
|
if (m_ImagesPool.Count == 0)
|
||||||
{
|
{
|
||||||
GetComponentsInChildren<Image>(m_ImagesPool);
|
GetComponentsInChildren<Image>(m_ImagesPool);
|
||||||
}
|
}
|
||||||
if (m_ImagesVertexIndex.Count > m_ImagesPool.Count)
|
if (m_ImagesVertexIndex.Count > m_ImagesPool.Count)
|
||||||
{
|
{
|
||||||
var resources = new DefaultControls.Resources();
|
var resources = new DefaultControls.Resources();
|
||||||
var go = DefaultControls.CreateImage(resources);
|
var go = DefaultControls.CreateImage(resources);
|
||||||
go.layer = gameObject.layer;
|
go.layer = gameObject.layer;
|
||||||
var rt = go.transform as RectTransform;
|
var rt = go.transform as RectTransform;
|
||||||
if (rt)
|
if (rt)
|
||||||
{
|
{
|
||||||
rt.SetParent(rectTransform);
|
rt.SetParent(rectTransform);
|
||||||
rt.localPosition = Vector3.zero;
|
rt.localPosition = Vector3.zero;
|
||||||
rt.localRotation = Quaternion.identity;
|
rt.localRotation = Quaternion.identity;
|
||||||
rt.localScale = Vector3.one;
|
rt.localScale = Vector3.one;
|
||||||
}
|
}
|
||||||
m_ImagesPool.Add(go.GetComponent<Image>());
|
m_ImagesPool.Add(go.GetComponent<Image>());
|
||||||
}
|
}
|
||||||
|
|
||||||
var spriteName = match.Groups[1].Value;
|
var spriteName = match.Groups[1].Value;
|
||||||
//var size = float.Parse(match.Groups[2].Value);
|
//var size = float.Parse(match.Groups[2].Value);
|
||||||
var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
|
var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
|
||||||
if (img.sprite == null || img.sprite.name != spriteName)
|
if (img.sprite == null || img.sprite.name != spriteName)
|
||||||
{
|
{
|
||||||
// img.sprite = Resources.Load<Sprite>(spriteName);
|
// img.sprite = Resources.Load<Sprite>(spriteName);
|
||||||
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
||||||
{
|
{
|
||||||
foreach (IconName icon in inspectorIconList)
|
foreach (IconName icon in inspectorIconList)
|
||||||
{
|
{
|
||||||
if (icon.name == spriteName)
|
if (icon.name == spriteName)
|
||||||
{
|
{
|
||||||
img.sprite = icon.sprite;
|
img.sprite = icon.sprite;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor, fontSize * ImageScalingFactor);
|
img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor, fontSize * ImageScalingFactor);
|
||||||
img.enabled = true;
|
img.enabled = true;
|
||||||
if (positions.Count == m_ImagesPool.Count)
|
if (positions.Count == m_ImagesPool.Count)
|
||||||
{
|
{
|
||||||
img.rectTransform.anchoredPosition = positions[m_ImagesVertexIndex.Count - 1];
|
img.rectTransform.anchoredPosition = positions[m_ImagesVertexIndex.Count - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = m_ImagesVertexIndex.Count; i < m_ImagesPool.Count; i++)
|
for (var i = m_ImagesVertexIndex.Count; i < m_ImagesPool.Count; i++)
|
||||||
{
|
{
|
||||||
if (m_ImagesPool[i])
|
if (m_ImagesPool[i])
|
||||||
{
|
{
|
||||||
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
||||||
// m_ImagesPool[i].enabled = false;
|
// m_ImagesPool[i].enabled = false;
|
||||||
m_ImagesPool[i].gameObject.SetActive(false);
|
m_ImagesPool[i].gameObject.SetActive(false);
|
||||||
m_ImagesPool[i].gameObject.hideFlags = HideFlags.HideAndDontSave;
|
m_ImagesPool[i].gameObject.hideFlags = HideFlags.HideAndDontSave;
|
||||||
culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
|
culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
|
||||||
m_ImagesPool.Remove(m_ImagesPool[i]);
|
m_ImagesPool.Remove(m_ImagesPool[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (culled_ImagesPool.Count > 1) {
|
if (culled_ImagesPool.Count > 1) {
|
||||||
clearImages = true;
|
clearImages = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnPopulateMesh(VertexHelper toFill)
|
protected override void OnPopulateMesh(VertexHelper toFill)
|
||||||
{
|
{
|
||||||
var orignText = m_Text;
|
var orignText = m_Text;
|
||||||
m_Text = m_OutputText;
|
m_Text = m_OutputText;
|
||||||
base.OnPopulateMesh(toFill);
|
base.OnPopulateMesh(toFill);
|
||||||
m_Text = orignText;
|
m_Text = orignText;
|
||||||
positions.Clear();
|
positions.Clear();
|
||||||
|
|
||||||
UIVertex vert = new UIVertex();
|
UIVertex vert = new UIVertex();
|
||||||
for (var i = 0; i < m_ImagesVertexIndex.Count; i++)
|
for (var i = 0; i < m_ImagesVertexIndex.Count; i++)
|
||||||
{
|
{
|
||||||
var endIndex = m_ImagesVertexIndex[i];
|
var endIndex = m_ImagesVertexIndex[i];
|
||||||
var rt = m_ImagesPool[i].rectTransform;
|
var rt = m_ImagesPool[i].rectTransform;
|
||||||
var size = rt.sizeDelta;
|
var size = rt.sizeDelta;
|
||||||
if (endIndex < toFill.currentVertCount)
|
if (endIndex < toFill.currentVertCount)
|
||||||
{
|
{
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex);
|
toFill.PopulateUIVertex(ref vert, endIndex);
|
||||||
positions.Add(new Vector2((vert.position.x + size.x / 2), (vert.position.y + size.y / 2)) + imageOffset);
|
positions.Add(new Vector2((vert.position.x + size.x / 2), (vert.position.y + size.y / 2)) + imageOffset);
|
||||||
|
|
||||||
// Erase the lower left corner of the black specks
|
// Erase the lower left corner of the black specks
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex - 3);
|
toFill.PopulateUIVertex(ref vert, endIndex - 3);
|
||||||
var pos = vert.position;
|
var pos = vert.position;
|
||||||
for (int j = endIndex, m = endIndex - 3; j > m; j--)
|
for (int j = endIndex, m = endIndex - 3; j > m; j--)
|
||||||
{
|
{
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex);
|
toFill.PopulateUIVertex(ref vert, endIndex);
|
||||||
vert.position = pos;
|
vert.position = pos;
|
||||||
toFill.SetUIVertex(vert, j);
|
toFill.SetUIVertex(vert, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_ImagesVertexIndex.Count != 0)
|
if (m_ImagesVertexIndex.Count != 0)
|
||||||
{
|
{
|
||||||
m_ImagesVertexIndex.Clear();
|
m_ImagesVertexIndex.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hyperlinks surround processing box
|
// Hyperlinks surround processing box
|
||||||
foreach (var hrefInfo in m_HrefInfos)
|
foreach (var hrefInfo in m_HrefInfos)
|
||||||
{
|
{
|
||||||
hrefInfo.boxes.Clear();
|
hrefInfo.boxes.Clear();
|
||||||
if (hrefInfo.startIndex >= toFill.currentVertCount)
|
if (hrefInfo.startIndex >= toFill.currentVertCount)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hyperlink inside the text is added to surround the vertex index coordinate frame
|
// Hyperlink inside the text is added to surround the vertex index coordinate frame
|
||||||
toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);
|
toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);
|
||||||
var pos = vert.position;
|
var pos = vert.position;
|
||||||
var bounds = new Bounds(pos, Vector3.zero);
|
var bounds = new Bounds(pos, Vector3.zero);
|
||||||
for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
|
for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
|
||||||
{
|
{
|
||||||
if (i >= toFill.currentVertCount)
|
if (i >= toFill.currentVertCount)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
toFill.PopulateUIVertex(ref vert, i);
|
toFill.PopulateUIVertex(ref vert, i);
|
||||||
pos = vert.position;
|
pos = vert.position;
|
||||||
if (pos.x < bounds.min.x) // Wrap re-add surround frame
|
if (pos.x < bounds.min.x) // Wrap re-add surround frame
|
||||||
{
|
{
|
||||||
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
|
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
|
||||||
bounds = new Bounds(pos, Vector3.zero);
|
bounds = new Bounds(pos, Vector3.zero);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bounds.Encapsulate(pos); // Extended enclosed box
|
bounds.Encapsulate(pos); // Extended enclosed box
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
|
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
|
||||||
}
|
|
||||||
UpdateQuadImage();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
}
|
||||||
/// Hyperlink List
|
|
||||||
/// </summary>
|
|
||||||
private readonly List<HrefInfo> m_HrefInfos = new List<HrefInfo>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Text Builder
|
|
||||||
/// </summary>
|
|
||||||
private static readonly StringBuilder s_TextBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
/// <summary>
|
UpdateQuadImage();
|
||||||
/// Hyperlink Regular Expression
|
|
||||||
/// </summary>
|
|
||||||
private static readonly Regex s_HrefRegex =
|
|
||||||
new Regex(@"<a href=([^>\n\s]+)>(.*?)(</a>)", RegexOptions.Singleline);
|
|
||||||
|
|
||||||
[Serializable]
|
}
|
||||||
public class HrefClickEvent : UnityEvent<string> { }
|
|
||||||
|
|
||||||
[SerializeField]
|
/// <summary>
|
||||||
private HrefClickEvent m_OnHrefClick = new HrefClickEvent();
|
/// Hyperlink List
|
||||||
|
/// </summary>
|
||||||
|
private readonly List<HrefInfo> m_HrefInfos = new List<HrefInfo>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Hyperlink Click Event
|
/// Text Builder
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HrefClickEvent onHrefClick
|
private static readonly StringBuilder s_TextBuilder = new StringBuilder();
|
||||||
{
|
|
||||||
get { return m_OnHrefClick; }
|
|
||||||
set { m_OnHrefClick = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finally, the output text hyperlinks get parsed
|
/// Hyperlink Regular Expression
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
private static readonly Regex s_HrefRegex =
|
||||||
protected string GetOutputText()
|
new Regex(@"<a href=([^>\n\s]+)>(.*?)(</a>)", RegexOptions.Singleline);
|
||||||
{
|
|
||||||
s_TextBuilder.Length = 0;
|
|
||||||
m_HrefInfos.Clear();
|
|
||||||
var indexText = 0;
|
|
||||||
fixedString = this.text;
|
|
||||||
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
|
||||||
{
|
|
||||||
foreach (IconName icon in inspectorIconList)
|
|
||||||
{
|
|
||||||
if (icon.name != null && icon.name != "")
|
|
||||||
{
|
|
||||||
fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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];
|
[Serializable]
|
||||||
var hrefInfo = new HrefInfo
|
public class HrefClickEvent : UnityEvent<string> { }
|
||||||
{
|
|
||||||
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);
|
|
||||||
|
|
||||||
s_TextBuilder.Append(match.Groups[2].Value);
|
[SerializeField]
|
||||||
s_TextBuilder.Append("</color>");
|
private HrefClickEvent m_OnHrefClick = new HrefClickEvent();
|
||||||
indexText = match.Index + match.Length;
|
|
||||||
}
|
|
||||||
s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText));
|
|
||||||
|
|
||||||
return s_TextBuilder.ToString();
|
/// <summary>
|
||||||
}
|
/// Hyperlink Click Event
|
||||||
|
/// </summary>
|
||||||
|
public HrefClickEvent onHrefClick
|
||||||
|
{
|
||||||
|
get { return m_OnHrefClick; }
|
||||||
|
set { m_OnHrefClick = value; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Click event is detected whether to click a hyperlink text
|
/// Finally, the output text hyperlinks get parsed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventData"></param>
|
/// <returns></returns>
|
||||||
public void OnPointerClick(PointerEventData eventData)
|
protected string GetOutputText()
|
||||||
{
|
{
|
||||||
Vector2 lp;
|
s_TextBuilder.Length = 0;
|
||||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
// This also clears the list of boxes in m_HrefInfos created by OnPopulateMesh function, without boxes href wont work, so i commented this
|
||||||
rectTransform, eventData.position, eventData.pressEventCamera, out lp);
|
//m_HrefInfos.Clear();
|
||||||
|
var indexText = 0;
|
||||||
|
fixedString = this.text;
|
||||||
|
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
||||||
|
{
|
||||||
|
foreach (IconName icon in inspectorIconList)
|
||||||
|
{
|
||||||
|
if (icon.name != null && icon.name != "")
|
||||||
|
{
|
||||||
|
fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (Match match in s_HrefRegex.Matches(fixedString))
|
||||||
|
{
|
||||||
|
s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText));
|
||||||
|
s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color
|
||||||
|
|
||||||
foreach (var hrefInfo in m_HrefInfos)
|
var group = match.Groups[1];
|
||||||
{
|
int foundAtIndex = -1;
|
||||||
var boxes = hrefInfo.boxes;
|
|
||||||
for (var i = 0; i < boxes.Count; ++i)
|
|
||||||
{
|
|
||||||
if (boxes[i].Contains(lp))
|
|
||||||
{
|
|
||||||
m_OnHrefClick.Invoke(hrefInfo.name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPointerEnter(PointerEventData eventData)
|
if(HrefInfosDoesExists(group.Value,out foundAtIndex)) {
|
||||||
{
|
m_HrefInfos[foundAtIndex].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices;
|
||||||
//do your stuff when highlighted
|
m_HrefInfos[foundAtIndex].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3;
|
||||||
//selected = true;
|
|
||||||
if (m_ImagesPool.Count >= 1)
|
|
||||||
{
|
|
||||||
foreach (Image img in m_ImagesPool)
|
|
||||||
{
|
|
||||||
if (button != null && button.isActiveAndEnabled)
|
|
||||||
{
|
|
||||||
img.color = button.colors.highlightedColor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPointerExit(PointerEventData eventData)
|
} 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);
|
||||||
|
}
|
||||||
|
|
||||||
//do your stuff when highlighted
|
s_TextBuilder.Append(match.Groups[2].Value);
|
||||||
//selected = false;
|
s_TextBuilder.Append("</color>");
|
||||||
if (m_ImagesPool.Count >= 1)
|
indexText = match.Index + match.Length;
|
||||||
{
|
}
|
||||||
foreach (Image img in m_ImagesPool)
|
s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText));
|
||||||
{
|
|
||||||
if (button != null && button.isActiveAndEnabled)
|
|
||||||
{
|
|
||||||
img.color = button.colors.normalColor;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
img.color = color;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void OnSelect(BaseEventData eventData)
|
|
||||||
{
|
|
||||||
//do your stuff when selected
|
|
||||||
//selected = true;
|
|
||||||
if (m_ImagesPool.Count >= 1)
|
|
||||||
{
|
|
||||||
foreach (Image img in m_ImagesPool)
|
|
||||||
{
|
|
||||||
if (button != null && button.isActiveAndEnabled)
|
|
||||||
{
|
|
||||||
img.color = button.colors.highlightedColor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
return s_TextBuilder.ToString();
|
||||||
/// Hyperlinks Info
|
}
|
||||||
/// </summary>
|
|
||||||
private class HrefInfo
|
|
||||||
{
|
|
||||||
public int startIndex;
|
|
||||||
|
|
||||||
public int endIndex;
|
/// <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;
|
||||||
|
}
|
||||||
|
|
||||||
public string name;
|
/// <summary>
|
||||||
|
/// Click event is detected whether to click a hyperlink text
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="eventData"></param>
|
||||||
|
public void OnPointerClick(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
Vector2 lp;
|
||||||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||||||
|
rectTransform, eventData.position, eventData.pressEventCamera, out lp);
|
||||||
|
|
||||||
public readonly List<Rect> boxes = new List<Rect>();
|
foreach (var hrefInfo in m_HrefInfos)
|
||||||
}
|
{
|
||||||
|
var boxes = hrefInfo.boxes;
|
||||||
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
for (var i = 0; i < boxes.Count; ++i)
|
||||||
void Update() {
|
{
|
||||||
if (clearImages) {
|
if (boxes[i].Contains(lp))
|
||||||
for (int i = 0; i < culled_ImagesPool.Count; i++){
|
{
|
||||||
DestroyImmediate(culled_ImagesPool[i]);
|
m_OnHrefClick.Invoke(hrefInfo.name);
|
||||||
}
|
return;
|
||||||
culled_ImagesPool.Clear();
|
}
|
||||||
clearImages = false;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void OnPointerEnter(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
//do your stuff when highlighted
|
||||||
|
//selected = true;
|
||||||
|
if (m_ImagesPool.Count >= 1)
|
||||||
|
{
|
||||||
|
foreach (Image img in m_ImagesPool)
|
||||||
|
{
|
||||||
|
if (button != null && button.isActiveAndEnabled)
|
||||||
|
{
|
||||||
|
img.color = button.colors.highlightedColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnPointerExit(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
|
||||||
|
//do your stuff when highlighted
|
||||||
|
//selected = false;
|
||||||
|
if (m_ImagesPool.Count >= 1)
|
||||||
|
{
|
||||||
|
foreach (Image img in m_ImagesPool)
|
||||||
|
{
|
||||||
|
if (button != null && button.isActiveAndEnabled)
|
||||||
|
{
|
||||||
|
img.color = button.colors.normalColor;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
img.color = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void OnSelect(BaseEventData eventData)
|
||||||
|
{
|
||||||
|
//do your stuff when selected
|
||||||
|
//selected = true;
|
||||||
|
if (m_ImagesPool.Count >= 1)
|
||||||
|
{
|
||||||
|
foreach (Image img in m_ImagesPool)
|
||||||
|
{
|
||||||
|
if (button != null && button.isActiveAndEnabled)
|
||||||
|
{
|
||||||
|
img.color = button.colors.highlightedColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Hyperlinks Info
|
||||||
|
/// </summary>
|
||||||
|
private class HrefInfo
|
||||||
|
{
|
||||||
|
public int startIndex;
|
||||||
|
|
||||||
|
public int endIndex;
|
||||||
|
|
||||||
|
public string name;
|
||||||
|
|
||||||
|
public readonly List<Rect> boxes = new List<Rect>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
||||||
|
void Update() {
|
||||||
|
if (clearImages) {
|
||||||
|
for (int i = 0; i < culled_ImagesPool.Count; i++){
|
||||||
|
DestroyImmediate(culled_ImagesPool[i]);
|
||||||
|
}
|
||||||
|
culled_ImagesPool.Clear();
|
||||||
|
clearImages = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue