Merged in playemgames/unity-ui-extensions-2/playemgames/fixed-rebuild-loop-error-in-textpic-and--1510001052642 (pull request #21)
Fixed rebuild loop error in TextPic and added disabling/enabling images when the TextPic component is disabled/enabled.pull/413/head
commit
ba555c63bc
|
@ -8,15 +8,15 @@ using System.Text.RegularExpressions;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.EventSystems;
|
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: Add Icon name and sprite to the icons list
|
||||||
|
|
||||||
[AddComponentMenu("UI/Extensions/TextPic")]
|
[AddComponentMenu("UI/Extensions/TextPic")]
|
||||||
|
|
||||||
[ExecuteInEditMode] // Needed for culling images that are not used //
|
[ExecuteInEditMode] // Needed for culling images that are not used //
|
||||||
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
|
public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler {
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Image Pool
|
/// Image Pool
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -25,9 +25,6 @@ namespace UnityEngine.UI.Extensions
|
||||||
private bool clearImages = false;
|
private bool clearImages = false;
|
||||||
private Object thisLock = new Object();
|
private Object thisLock = new Object();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vertex Index
|
/// Vertex Index
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -39,33 +36,36 @@ namespace UnityEngine.UI.Extensions
|
||||||
private static readonly Regex s_Regex =
|
private static readonly Regex s_Regex =
|
||||||
new Regex(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
|
new Regex(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
|
||||||
|
|
||||||
private string fixedString;
|
private string fixedString; [SerializeField]
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
[Tooltip("Allow click events to be received by parents, (default) blocks")]
|
[Tooltip("Allow click events to be received by parents, (default) blocks")]
|
||||||
|
|
||||||
private bool m_ClickParents;
|
private bool m_ClickParents;
|
||||||
|
|
||||||
public bool AllowClickParents
|
// Update the quad images when true
|
||||||
{
|
private bool updateQuad = false;
|
||||||
|
|
||||||
|
public bool AllowClickParents {
|
||||||
get { return m_ClickParents; }
|
get { return m_ClickParents; }
|
||||||
set { m_ClickParents = value; }
|
set { m_ClickParents = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void SetVerticesDirty()
|
public override void SetVerticesDirty() {
|
||||||
{
|
|
||||||
base.SetVerticesDirty();
|
base.SetVerticesDirty();
|
||||||
UpdateQuadImage();
|
|
||||||
|
// Update the quad images
|
||||||
|
updateQuad = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
protected override void OnValidate()
|
protected override void OnValidate() {
|
||||||
{
|
|
||||||
base.OnValidate();
|
base.OnValidate();
|
||||||
UpdateQuadImage();
|
|
||||||
for (int i = 0; i < inspectorIconList.Length; i++)
|
// Update the quad images
|
||||||
{
|
updateQuad = true;
|
||||||
if (inspectorIconList[i].scale == Vector2.zero)
|
|
||||||
{
|
for (int i = 0; i < inspectorIconList.Length; i++) {
|
||||||
|
if (inspectorIconList[i].scale == Vector2.zero) {
|
||||||
inspectorIconList[i].scale = Vector2.one;
|
inspectorIconList[i].scale = Vector2.one;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,16 +78,16 @@ namespace UnityEngine.UI.Extensions
|
||||||
private string m_OutputText;
|
private string m_OutputText;
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public struct IconName
|
public struct IconName {
|
||||||
{
|
|
||||||
public string name;
|
public string name;
|
||||||
public Sprite sprite;
|
public Sprite sprite;
|
||||||
public Vector2 offset;
|
public Vector2 offset;
|
||||||
public Vector2 scale;
|
public Vector2 scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IconName[] inspectorIconList;
|
public IconName[] inspectorIconList;
|
||||||
|
|
||||||
[Tooltip("Global scaling factor for all images")]
|
[Tooltip("Global scaling factor for all images")]
|
||||||
public float ImageScalingFactor = 1;
|
public float ImageScalingFactor = 1;
|
||||||
|
|
||||||
// Write the name or hex value of the hyperlink color
|
// Write the name or hex value of the hyperlink color
|
||||||
|
@ -98,125 +98,124 @@ namespace UnityEngine.UI.Extensions
|
||||||
public Vector2 imageOffset = Vector2.zero;
|
public Vector2 imageOffset = Vector2.zero;
|
||||||
|
|
||||||
private Button button;
|
private Button button;
|
||||||
private Selectable highlightselectable;
|
|
||||||
|
|
||||||
//Commented out as private and not used.. Yet?
|
//Commented out as private and not used.. Yet?
|
||||||
//private bool selected = false;
|
private bool selected = false;
|
||||||
|
|
||||||
private List<Vector2> positions = new List<Vector2>();
|
private List<Vector2> positions = new List<Vector2>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Little heck to support multiple hrefs with same name
|
* Little hack to support multiple hrefs with same name
|
||||||
*/
|
*/
|
||||||
private string previousText = "";
|
private string previousText = "";
|
||||||
public bool isCreating_m_HrefInfos = true;
|
public bool isCreating_m_HrefInfos = true;
|
||||||
|
|
||||||
|
new void Start() {
|
||||||
new void Start()
|
button = GetComponent<Button>();
|
||||||
{
|
ResetIconList();
|
||||||
button = GetComponentInParent<Button>();
|
|
||||||
if (button != null)
|
|
||||||
{
|
|
||||||
CanvasGroup cg;
|
|
||||||
cg = GetComponent<CanvasGroup>();
|
|
||||||
if (cg == null)
|
|
||||||
{
|
|
||||||
cg = gameObject.AddComponent<CanvasGroup>();
|
|
||||||
}
|
|
||||||
cg.blocksRaycasts = false;
|
|
||||||
highlightselectable = cg.GetComponent<Selectable>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
highlightselectable = GetComponent<Selectable>();
|
|
||||||
}
|
|
||||||
|
|
||||||
Reset_m_HrefInfos ();
|
|
||||||
base.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UpdateQuadImage()
|
public void ResetIconList() {
|
||||||
{
|
Reset_m_HrefInfos ();
|
||||||
|
base.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
|
||||||
{
|
|
||||||
var picIndex = match.Index;
|
|
||||||
var endIndex = picIndex * 4 + 3;
|
|
||||||
m_ImagesVertexIndex.Add(endIndex);
|
|
||||||
|
|
||||||
m_ImagesPool.RemoveAll(image => image == null);
|
MatchCollection matches = s_Regex.Matches(m_OutputText);
|
||||||
if (m_ImagesPool.Count == 0)
|
|
||||||
{
|
|
||||||
GetComponentsInChildren<Image>(m_ImagesPool);
|
|
||||||
}
|
|
||||||
if (m_ImagesVertexIndex.Count > m_ImagesPool.Count)
|
|
||||||
{
|
|
||||||
var resources = new DefaultControls.Resources();
|
|
||||||
var go = DefaultControls.CreateImage(resources);
|
|
||||||
go.layer = gameObject.layer;
|
|
||||||
var rt = go.transform as RectTransform;
|
|
||||||
if (rt)
|
|
||||||
{
|
|
||||||
rt.SetParent(rectTransform);
|
|
||||||
rt.localPosition = Vector3.zero;
|
|
||||||
rt.localRotation = Quaternion.identity;
|
|
||||||
rt.localScale = Vector3.one;
|
|
||||||
}
|
|
||||||
m_ImagesPool.Add(go.GetComponent<Image>());
|
|
||||||
}
|
|
||||||
|
|
||||||
var spriteName = match.Groups[1].Value;
|
if (matches != null && matches.Count > 0) {
|
||||||
var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
|
foreach (Match match in matches) {
|
||||||
Vector2 imgoffset = Vector2.zero;
|
var picIndex = match.Index;
|
||||||
if (img.sprite == null || img.sprite.name != spriteName)
|
var endIndex = picIndex * 4 + 3;
|
||||||
{
|
m_ImagesVertexIndex.Add(endIndex);
|
||||||
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
|
||||||
{
|
|
||||||
foreach (IconName icon in inspectorIconList)
|
|
||||||
{
|
|
||||||
if (icon.name == spriteName)
|
|
||||||
{
|
|
||||||
img.sprite = icon.sprite;
|
|
||||||
img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor * icon.scale.x, fontSize * ImageScalingFactor * icon.scale.y);
|
|
||||||
imgoffset = icon.offset;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
img.enabled = true;
|
|
||||||
if (positions.Count == m_ImagesPool.Count)
|
|
||||||
{
|
|
||||||
img.rectTransform.anchoredPosition = positions[m_ImagesVertexIndex.Count - 1] += imgoffset;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = m_ImagesVertexIndex.Count; i < m_ImagesPool.Count; i++)
|
m_ImagesPool.RemoveAll(image => image == null);
|
||||||
{
|
|
||||||
if (m_ImagesPool[i])
|
if (m_ImagesPool.Count == 0) {
|
||||||
{
|
GetComponentsInChildren<Image>(true, m_ImagesPool);
|
||||||
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
}
|
||||||
// m_ImagesPool[i].enabled = false;
|
|
||||||
m_ImagesPool[i].gameObject.SetActive(false);
|
if (m_ImagesVertexIndex.Count > m_ImagesPool.Count) {
|
||||||
m_ImagesPool[i].gameObject.hideFlags = HideFlags.HideAndDontSave;
|
var resources = new DefaultControls.Resources();
|
||||||
culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
|
var go = DefaultControls.CreateImage(resources);
|
||||||
m_ImagesPool.Remove(m_ImagesPool[i]);
|
go.layer = gameObject.layer;
|
||||||
}
|
var rt = go.transform as RectTransform;
|
||||||
}
|
|
||||||
if (culled_ImagesPool.Count > 1) {
|
if (rt) {
|
||||||
|
rt.SetParent(rectTransform);
|
||||||
|
rt.anchoredPosition3D = Vector3.zero;
|
||||||
|
rt.localRotation = Quaternion.identity;
|
||||||
|
rt.localScale = Vector3.one;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_ImagesPool.Add(go.GetComponent<Image>());
|
||||||
|
}
|
||||||
|
|
||||||
|
var spriteName = match.Groups[1].Value;
|
||||||
|
|
||||||
|
var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
|
||||||
|
|
||||||
|
Vector2 imgoffset = Vector2.zero;
|
||||||
|
|
||||||
|
if (img.sprite == null || img.sprite.name != spriteName) {
|
||||||
|
if (inspectorIconList != null && inspectorIconList.Length > 0) {
|
||||||
|
foreach (IconName icon in inspectorIconList) {
|
||||||
|
if (icon.name == spriteName) {
|
||||||
|
img.sprite = icon.sprite;
|
||||||
|
img.preserveAspect = true;
|
||||||
|
img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor * icon.scale.x, fontSize * ImageScalingFactor * icon.scale.y);
|
||||||
|
imgoffset = icon.offset;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
img.enabled = true;
|
||||||
|
|
||||||
|
if (positions.Count == m_ImagesPool.Count) {
|
||||||
|
img.rectTransform.anchoredPosition = positions[m_ImagesVertexIndex.Count - 1] += imgoffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If there are no matches, remove the images from the pool
|
||||||
|
for (int i = 0; i < m_ImagesPool.Count; i++) {
|
||||||
|
if (m_ImagesPool[i]) {
|
||||||
|
if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject)) {
|
||||||
|
culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
|
||||||
|
m_ImagesPool.Remove(m_ImagesPool[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any images that are not being used
|
||||||
|
for (var i = m_ImagesVertexIndex.Count; i < m_ImagesPool.Count; i++) {
|
||||||
|
if (m_ImagesPool[i]) {
|
||||||
|
if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject)) {
|
||||||
|
culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
|
||||||
|
m_ImagesPool.Remove(m_ImagesPool[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the images when it is safe to do so
|
||||||
|
if (culled_ImagesPool.Count > 0) {
|
||||||
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 = GetOutputText();
|
m_Text = GetOutputText();
|
||||||
base.OnPopulateMesh(toFill);
|
base.OnPopulateMesh(toFill);
|
||||||
|
@ -224,39 +223,41 @@ namespace UnityEngine.UI.Extensions
|
||||||
positions.Clear();
|
positions.Clear();
|
||||||
|
|
||||||
UIVertex vert = new UIVertex();
|
UIVertex vert = new UIVertex();
|
||||||
for (var i = 0; i < m_ImagesVertexIndex.Count; i++)
|
|
||||||
{
|
|
||||||
var endIndex = m_ImagesVertexIndex[i];
|
|
||||||
var rt = m_ImagesPool[i].rectTransform;
|
|
||||||
var size = rt.sizeDelta;
|
|
||||||
if (endIndex < toFill.currentVertCount)
|
|
||||||
{
|
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex);
|
|
||||||
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
|
for (var i = 0; i < m_ImagesVertexIndex.Count; i++) {
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex - 3);
|
var endIndex = m_ImagesVertexIndex[i];
|
||||||
var pos = vert.position;
|
|
||||||
for (int j = endIndex, m = endIndex - 3; j > m; j--)
|
if (m_ImagesPool[i] != null) {
|
||||||
{
|
var rt = m_ImagesPool[i].rectTransform;
|
||||||
toFill.PopulateUIVertex(ref vert, endIndex);
|
|
||||||
vert.position = pos;
|
var size = rt.sizeDelta;
|
||||||
toFill.SetUIVertex(vert, j);
|
|
||||||
}
|
if (endIndex < toFill.currentVertCount) {
|
||||||
}
|
toFill.PopulateUIVertex(ref vert, endIndex);
|
||||||
|
positions.Add(new Vector2((vert.position.x + fontSize / 2), (vert.position.y + fontSize / 2)) + imageOffset);
|
||||||
|
|
||||||
|
// Erase the lower left corner of the black specks
|
||||||
|
toFill.PopulateUIVertex(ref vert, endIndex - 3);
|
||||||
|
var pos = vert.position;
|
||||||
|
|
||||||
|
for (int j = endIndex, m = endIndex - 3; j > m; j--) {
|
||||||
|
toFill.PopulateUIVertex(ref vert, endIndex);
|
||||||
|
vert.position = pos;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,28 +265,30 @@ namespace UnityEngine.UI.Extensions
|
||||||
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
|
|
||||||
{
|
// Wrap re-add surround frame
|
||||||
|
if (pos.x < bounds.min.x) {
|
||||||
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();
|
|
||||||
|
// Update the quad images
|
||||||
|
updateQuad = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -313,8 +316,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Hyperlink Click Event
|
/// Hyperlink Click Event
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HrefClickEvent onHrefClick
|
public HrefClickEvent onHrefClick {
|
||||||
{
|
|
||||||
get { return m_OnHrefClick; }
|
get { return m_OnHrefClick; }
|
||||||
set { m_OnHrefClick = value; }
|
set { m_OnHrefClick = value; }
|
||||||
}
|
}
|
||||||
|
@ -323,38 +325,40 @@ namespace UnityEngine.UI.Extensions
|
||||||
/// Finally, the output text hyperlinks get parsed
|
/// Finally, the output text hyperlinks get parsed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected string GetOutputText()
|
protected string GetOutputText() {
|
||||||
{
|
|
||||||
s_TextBuilder.Length = 0;
|
s_TextBuilder.Length = 0;
|
||||||
|
|
||||||
var indexText = 0;
|
var indexText = 0;
|
||||||
fixedString = this.text;
|
fixedString = this.text;
|
||||||
if (inspectorIconList != null && inspectorIconList.Length > 0)
|
|
||||||
{
|
if (inspectorIconList != null && inspectorIconList.Length > 0) {
|
||||||
foreach (IconName icon in inspectorIconList)
|
foreach (IconName icon in inspectorIconList) {
|
||||||
{
|
if (!string.IsNullOrEmpty(icon.name)) {
|
||||||
if (icon.name != null && icon.name != "")
|
|
||||||
{
|
|
||||||
fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />");
|
fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
foreach (Match match in s_HrefRegex.Matches(fixedString))
|
|
||||||
{
|
foreach (Match match in s_HrefRegex.Matches(fixedString)) {
|
||||||
s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText));
|
s_TextBuilder.Append(fixedString.Substring(indexText, match.Index - indexText));
|
||||||
s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color
|
s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color
|
||||||
|
|
||||||
var group = match.Groups[1];
|
var group = match.Groups[1];
|
||||||
if(isCreating_m_HrefInfos) {
|
|
||||||
|
if (isCreating_m_HrefInfos) {
|
||||||
var hrefInfo = new HrefInfo
|
var hrefInfo = new HrefInfo
|
||||||
|
|
||||||
{
|
{
|
||||||
startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices
|
startIndex = s_TextBuilder.Length * 4, // Hyperlinks in text starting vertex indices
|
||||||
endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3,
|
endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3,
|
||||||
name = group.Value
|
name = group.Value
|
||||||
};
|
};
|
||||||
|
|
||||||
m_HrefInfos.Add(hrefInfo);
|
m_HrefInfos.Add(hrefInfo);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if(m_HrefInfos.Count > 0) {
|
if(m_HrefInfos.Count > 0) {
|
||||||
m_HrefInfos[count].startIndex = s_TextBuilder.Length * 4; // Hyperlinks in text starting vertex indices;
|
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;
|
m_HrefInfos[count].endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3;
|
||||||
|
@ -366,8 +370,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
s_TextBuilder.Append("</color>");
|
s_TextBuilder.Append("</color>");
|
||||||
indexText = match.Index + match.Length;
|
indexText = match.Index + match.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we should create array only once or if there is any change in the text
|
// we should create array only once or if there is any change in the text
|
||||||
if(isCreating_m_HrefInfos)
|
if (isCreating_m_HrefInfos)
|
||||||
isCreating_m_HrefInfos = false;
|
isCreating_m_HrefInfos = false;
|
||||||
|
|
||||||
s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText));
|
s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText));
|
||||||
|
@ -379,19 +384,15 @@ namespace UnityEngine.UI.Extensions
|
||||||
/// Click event is detected whether to click a hyperlink text
|
/// Click event is detected whether to click a hyperlink text
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventData"></param>
|
/// <param name="eventData"></param>
|
||||||
public void OnPointerClick(PointerEventData eventData)
|
public void OnPointerClick(PointerEventData eventData) {
|
||||||
{
|
|
||||||
Vector2 lp;
|
Vector2 lp;
|
||||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||||||
rectTransform, eventData.position, eventData.pressEventCamera, out lp);
|
rectTransform, eventData.position, eventData.pressEventCamera, out lp);
|
||||||
|
|
||||||
foreach (var hrefInfo in m_HrefInfos)
|
foreach (var hrefInfo in m_HrefInfos) {
|
||||||
{
|
|
||||||
var boxes = hrefInfo.boxes;
|
var boxes = hrefInfo.boxes;
|
||||||
for (var i = 0; i < boxes.Count; ++i)
|
for (var i = 0; i < boxes.Count; ++i) {
|
||||||
{
|
if (boxes[i].Contains(lp)) {
|
||||||
if (boxes[i].Contains(lp))
|
|
||||||
{
|
|
||||||
m_OnHrefClick.Invoke(hrefInfo.name);
|
m_OnHrefClick.Invoke(hrefInfo.name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -399,65 +400,65 @@ namespace UnityEngine.UI.Extensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPointerEnter(PointerEventData eventData)
|
public void OnPointerEnter(PointerEventData eventData) {
|
||||||
{
|
|
||||||
//do your stuff when highlighted
|
//do your stuff when highlighted
|
||||||
//selected = true;
|
selected = true;
|
||||||
if (m_ImagesPool.Count >= 1)
|
|
||||||
{
|
if (m_ImagesPool.Count >= 1) {
|
||||||
foreach (Image img in m_ImagesPool)
|
foreach (Image img in m_ImagesPool) {
|
||||||
{
|
if (button != null && button.isActiveAndEnabled) {
|
||||||
if (highlightselectable != null && highlightselectable.isActiveAndEnabled)
|
img.color = button.colors.highlightedColor;
|
||||||
{
|
|
||||||
img.color = highlightselectable.colors.highlightedColor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPointerExit(PointerEventData eventData)
|
public void OnPointerExit(PointerEventData eventData) {
|
||||||
{
|
|
||||||
|
|
||||||
//do your stuff when highlighted
|
//do your stuff when highlighted
|
||||||
//selected = false;
|
selected = false;
|
||||||
if (m_ImagesPool.Count >= 1)
|
|
||||||
{
|
if (m_ImagesPool.Count >= 1) {
|
||||||
foreach (Image img in m_ImagesPool)
|
foreach (Image img in m_ImagesPool) {
|
||||||
{
|
if (button != null && button.isActiveAndEnabled) {
|
||||||
if (highlightselectable != null && highlightselectable.isActiveAndEnabled)
|
img.color = button.colors.normalColor;
|
||||||
{
|
|
||||||
img.color = highlightselectable.colors.normalColor;
|
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
img.color = color;
|
img.color = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void OnSelect(BaseEventData eventData)
|
|
||||||
{
|
public void OnSelect(BaseEventData eventData) {
|
||||||
//do your stuff when selected
|
//do your stuff when selected
|
||||||
//selected = true;
|
selected = true;
|
||||||
if (m_ImagesPool.Count >= 1)
|
|
||||||
{
|
if (m_ImagesPool.Count >= 1) {
|
||||||
foreach (Image img in m_ImagesPool)
|
foreach (Image img in m_ImagesPool) {
|
||||||
{
|
if (button != null && button.isActiveAndEnabled) {
|
||||||
if (highlightselectable != null && highlightselectable.isActiveAndEnabled)
|
img.color = button.colors.highlightedColor;
|
||||||
{
|
|
||||||
img.color = highlightselectable.colors.highlightedColor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnDeselect(BaseEventData eventData) {
|
||||||
|
//do your stuff when selected
|
||||||
|
selected = false;
|
||||||
|
|
||||||
|
if (m_ImagesPool.Count >= 1) {
|
||||||
|
foreach (Image img in m_ImagesPool) {
|
||||||
|
if (button != null && button.isActiveAndEnabled) {
|
||||||
|
img.color = button.colors.normalColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Hyperlinks Info
|
/// Hyperlinks Info
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private class HrefInfo
|
private class HrefInfo {
|
||||||
{
|
|
||||||
public int startIndex;
|
public int startIndex;
|
||||||
|
|
||||||
public int endIndex;
|
public int endIndex;
|
||||||
|
@ -467,19 +468,34 @@ namespace UnityEngine.UI.Extensions
|
||||||
public readonly List<Rect> boxes = new List<Rect>();
|
public readonly List<Rect> boxes = new List<Rect>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TEMPORARY FIX REMOVE IMAGES FROM POOL DELETE LATER SINCE CANNOT DESTROY */
|
void LateUpdate() {
|
||||||
void Update() {
|
// Reset the hrefs if text is changed
|
||||||
|
if( previousText != text) {
|
||||||
|
Reset_m_HrefInfos ();
|
||||||
|
|
||||||
|
// Update the quad on text change
|
||||||
|
updateQuad = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Need to lock to remove images properly
|
||||||
lock (thisLock) {
|
lock (thisLock) {
|
||||||
|
// Can only update the images when it is not in a rebuild, this prevents the error
|
||||||
|
if (updateQuad) {
|
||||||
|
UpdateQuadImage();
|
||||||
|
updateQuad = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy any images that are not in use
|
||||||
if (clearImages) {
|
if (clearImages) {
|
||||||
for (int i = 0; i < culled_ImagesPool.Count; i++){
|
for (int i = 0; i < culled_ImagesPool.Count; i++) {
|
||||||
DestroyImmediate(culled_ImagesPool[i]);
|
DestroyImmediate(culled_ImagesPool[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
culled_ImagesPool.Clear();
|
culled_ImagesPool.Clear();
|
||||||
|
|
||||||
clearImages = false;
|
clearImages = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( previousText != text)
|
|
||||||
Reset_m_HrefInfos ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reseting m_HrefInfos array if there is any change in text
|
// Reseting m_HrefInfos array if there is any change in text
|
||||||
|
@ -488,5 +504,34 @@ namespace UnityEngine.UI.Extensions
|
||||||
m_HrefInfos.Clear();
|
m_HrefInfos.Clear();
|
||||||
isCreating_m_HrefInfos = true;
|
isCreating_m_HrefInfos = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnEnable() {
|
||||||
|
base.OnEnable();
|
||||||
|
|
||||||
|
// Enable images on TextPic disable
|
||||||
|
if (m_ImagesPool.Count >= 1) {
|
||||||
|
for (int i = 0; i < m_ImagesPool.Count; i++) {
|
||||||
|
if(m_ImagesPool[i] != null) {
|
||||||
|
m_ImagesPool[i].enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the quads on re-enable
|
||||||
|
updateQuad = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnDisable() {
|
||||||
|
base.OnDisable();
|
||||||
|
|
||||||
|
// Disable images on TextPic disable
|
||||||
|
if (m_ImagesPool.Count >= 1) {
|
||||||
|
for (int i = 0; i < m_ImagesPool.Count; i++) {
|
||||||
|
if(m_ImagesPool[i] != null) {
|
||||||
|
m_ImagesPool[i].enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue