热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UnityUGUI图文混排(六)超链接

图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解

图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的。

博主已经将超链接的功能直接整合到了之前的InlineText和InlineSpriteText的两个脚本中


1.定义超链接的正则表达式和事件监听

#region 超链接///

/// 超链接信息列表/// private readonly List m_HrefInfos = new List();/// /// 文本构造器/// private static readonly StringBuilder s_TextBuilder = new StringBuilder();/// /// 超链接正则/// private static readonly Regex s_HrefRegex =new Regex(@"\n\s]+)>(.*?)()", RegexOptions.Singleline);[System.Serializable]public class HrefClickEvent : UnityEvent { }[SerializeField]private HrefClickEvent m_OnHrefClick = new HrefClickEvent();/// /// 超链接点击事件/// public HrefClickEvent onHrefClick{get { return m_OnHrefClick; }set { m_OnHrefClick = value; }}/// /// 获取超链接解析后的最后输出文本/// /// protected string GetOutputText(){s_TextBuilder.Length = 0;m_HrefInfos.Clear();var indexText = 0;foreach (Match match in s_HrefRegex.Matches(text)){s_TextBuilder.Append(text.Substring(indexText, match.Index - indexText));s_TextBuilder.Append(""); // 超链接颜色var group = match.Groups[1];var hrefInfo = new HrefInfo{startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引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);s_TextBuilder.Append("");indexText = match.Index + match.Length;}s_TextBuilder.Append(text.Substring(indexText, text.Length - indexText));return s_TextBuilder.ToString();}/// /// 点击事件检测是否点击到超链接文本/// /// public void OnPointerClick(PointerEventData eventData){Vector2 lp;RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out lp);foreach (var hrefInfo in m_HrefInfos){var boxes = hrefInfo.boxes;for (var i = 0; i /// 超链接信息类/// private class HrefInfo{public int startIndex;public int endIndex;public string name;public readonly List boxes = new List();}#endregion2.在文本绘制完成后处理超链接的包围盒

#region 处理超链接的包围盒// 处理超链接包围框UIVertex vert = new UIVertex();foreach (var hrefInfo in m_HrefInfos){hrefInfo.boxes.Clear();if (hrefInfo.startIndex >= toFill.currentVertCount){continue;}// 将超链接里面的文本顶点索引坐标加入到包围框toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);var pos = vert.position;var bounds = new Bounds(pos, Vector3.zero);for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i = toFill.currentVertCount){break;}toFill.PopulateUIVertex(ref vert, i);pos = vert.position;if (pos.x 3.看一下文中中超链接的输入规则


4.简单写了一个测试脚本,用来监听点击事件

using UnityEngine;
using System.Collections;public class TestClickInlineText : MonoBehaviour {private InlieText _text;void Awake(){_text = GetComponent();}void OnEnable(){_text.onHrefClick.AddListener(OnHrefClick);}void OnDisable(){_text.onHrefClick.RemoveListener(OnHrefClick);}private void OnHrefClick(string hrefName){Debug.Log("点击了 " + hrefName);// Application.OpenURL("www.baidu.com");}
}
5.运行截图:


6.更新速度实在太慢,为了早点完结图文混排,这里的功能是复制的,有什么疑问的话,可以再讨论,这里的功能也就更新得差不多了,最后再给一个最新的源码链接,短时间没有特殊的功能或者bug,就不打算再更新了

工程源码链接:https://github.com/coding2233/TextInlineSprite



推荐阅读
author-avatar
mobiledu2502881211
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有