日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity超链接:支持点击事件,下划线以及自定义颜色

發布時間:2024/1/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity超链接:支持点击事件,下划线以及自定义颜色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基于這篇:
zyf2533 - Unity 超鏈接 Text
修正了一些bug,額外支持了下劃線以及自定義顏色。

/*https://blog.csdn.net/zyf2533/article/details/122703640*/using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.EventSystems; //using UnityEngine.UI;namespace UnityEngine.UI {[AddComponentMenu("UI/UIHyperlinkText")]/// <summary>/// eg:/// 獲得了:<a href="Tip#1102201">先鋒盾合成卷軸</a> /// </summary>public class UIHyperlinkText : Text, IPointerClickHandler{/// <summary>/// 超鏈接信息類/// </summary>private class HyperlinkInfo{//起始Indexpublic int StartIndex;//結束Indexpublic int EndIndex;//內容public string RefValue;public string InnerValue;//包圍框public List<Rect> BoxList = new List<Rect>();}#region 私有變量//超鏈接正則private static Regex hrefRegex = new Regex(@"<a href=([^>\n\s]+)>(.*?)(</a>)", RegexOptions.Singleline);//顏色正則private static Regex colorRegex = new Regex(@"<color=([^>\n\s]+)>(.*?)(</color>)", RegexOptions.Singleline);private static Regex colorPreRegex = new Regex(@"<color=([^>\n\s]+)>", RegexOptions.Singleline);private static Regex colorEndRegex = new Regex(@"</color>", RegexOptions.Singleline);//超鏈接信息列表private List<HyperlinkInfo> hyperlinkInfoList = new List<HyperlinkInfo>();private static Action<string, string> clickCallback = null;private static Color innerTextColor = Color.blue;#endregion#region 公有變量#endregion#region 生命周期protected override void OnPopulateMesh(VertexHelper toFill){base.OnPopulateMesh(toFill);InitHyperlinkInfo();InitHyperlinkBox(toFill);DrawUnderLine(toFill);}#endregion#region 公有方法#endregion#region 動作public void OnPointerClick(PointerEventData eventData){Vector2 localPoint;RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out localPoint);foreach (HyperlinkInfo hyperlinkInfo in hyperlinkInfoList){var boxeList = hyperlinkInfo.BoxList;for (var i = 0; i < boxeList.Count; ++i){if (boxeList[i].Contains(localPoint)){if (clickCallback != null){clickCallback(hyperlinkInfo.RefValue, hyperlinkInfo.InnerValue);}return;}}}}public static void RegisterClickCallback(Action<string, string> callback, string innerColor){ clickCallback = callback;if (!innerColor.StartsWith("#")){innerColor = "#" + innerColor;}Color nowColor;ColorUtility.TryParseHtmlString(innerColor, out nowColor);innerTextColor = nowColor;}#endregion#region 私有方法/// <summary>/// 初始化連接信息/// </summary>private void InitHyperlinkInfo(){GetOutputText(text);}/// <summary>/// 初始化連接包圍框/// </summary>/// <param name="toFill"></param>private void InitHyperlinkBox(VertexHelper toFill){UIVertex vert = new UIVertex();// 處理超鏈接包圍框foreach (var hrefInfo in hyperlinkInfoList){hrefInfo.BoxList.Clear();//一個字符是四個頂點,所以Index要乘以4int startVertex = hrefInfo.StartIndex * 4;int endVertex = hrefInfo.EndIndex * 4;if (startVertex >= toFill.currentVertCount){continue;}// 將超鏈接里面的文本頂點索引坐標加入到包圍框toFill.PopulateUIVertex(ref vert, startVertex);var pos = vert.position;var bounds = new Bounds(pos, Vector3.zero);for (int i = startVertex; i < endVertex; i++){if (i >= toFill.currentVertCount){break;}toFill.PopulateUIVertex(ref vert, i);//toFill.SetUIVertex(vert, i);pos = vert.position;bool needEncapsulate = true;if ((i - startVertex) % 4 == 0){if (i < 4) continue;UIVertex lastV = new UIVertex();toFill.PopulateUIVertex(ref lastV, i - 4);var lastPos = lastV.position;if (pos.x < lastPos.x && pos.y < lastPos.y) // 換行重新添加包圍框{hrefInfo.BoxList.Add(new Rect(bounds.min, bounds.size));bounds = new Bounds(pos, Vector3.zero);needEncapsulate = false;}}if (needEncapsulate){bounds.Encapsulate(pos); // 擴展包圍框}}hrefInfo.BoxList.Add(new Rect(bounds.min, bounds.size));}}private void DrawUnderLine(VertexHelper vh){foreach (var link in hyperlinkInfoList){foreach (var rect in link.BoxList){float height = rect.height;// 左下var pos1 = new Vector3(rect.min.x, rect.min.y, 0);// 右下var pos2 = new Vector3(rect.max.x, rect.max.y, 0) - new Vector3(0, height, 0);MeshUnderLine(vh, pos1, pos2);}}}private void MeshUnderLine(VertexHelper vh, Vector2 startPos, Vector2 endPos){ Vector2 extents = rectTransform.rect.size;var setting = GetGenerationSettings(extents);TextGenerator underlineText = new TextGenerator();underlineText.Populate(" ̄", setting);IList<UIVertex> lineVer = underlineText.verts; //" ̄"的的頂點數組Vector3[] pos = new Vector3[4];pos[0] = startPos + new Vector2(-8, 0);pos[3] = startPos + new Vector2(-8, -4f);pos[2] = endPos + new Vector2(8, -4f);pos[1] = endPos + new Vector2(8, 0);if (lineVer.Count == 4){UIVertex[] tempVerts = new UIVertex[4];for (int i = 0; i < 4; i++){tempVerts[i] = lineVer[i];tempVerts[i].color = Color.white;tempVerts[i].position = pos[i];tempVerts[i].uv0 = lineVer[i].uv0;tempVerts[i].uv1 = lineVer[i].uv1;tempVerts[i].uv2 = lineVer[i].uv2;tempVerts[i].uv3 = lineVer[i].uv3;}vh.AddUIVertexQuad(tempVerts);}}/// <summary>/// 獲取超鏈接解析后的最后輸出文本/// </summary>/// <returns></returns>private string GetOutputText(string outputText){StringBuilder stringBuilder = new StringBuilder();hyperlinkInfoList.Clear();int strIndex = 0;foreach (Match match in hrefRegex.Matches(outputText)){//Debug.Log(match.Value);string appendStr = outputText.Substring(strIndex, match.Index - strIndex);//空格和回車沒有頂點渲染,所以要去掉appendStr = appendStr.Replace(" ", "");appendStr = appendStr.Replace("\n", "");appendStr = RichStringFilter(appendStr);stringBuilder.Append(appendStr);int startIndex = stringBuilder.Length;//第一個是連接url,第二個是連接文本,跳轉用url,計算index用文本Group urlGroup = match.Groups[1];Group titleGroup = match.Groups[2];//如果有Color語法嵌套,則還要繼續扒,直到把最終文本扒出來Match colorMatch = colorRegex.Match(titleGroup.Value);if (colorMatch.Groups.Count > 3){titleGroup = colorMatch.Groups[2];}var inner = titleGroup.Value;inner = inner.Replace(" ", "");inner = inner.Replace("\n", "");stringBuilder.Append(inner);HyperlinkInfo hyperlinkInfo = new HyperlinkInfo{StartIndex = startIndex,EndIndex = (startIndex + inner.Length),RefValue = urlGroup.Value,InnerValue = titleGroup.Value,};strIndex = match.Index + match.Length;hyperlinkInfoList.Add(hyperlinkInfo);}stringBuilder.Append(outputText.Substring(strIndex, outputText.Length - strIndex));return stringBuilder.ToString();}private string RichStringFilter(string outputText){outputText = colorPreRegex.Replace(outputText, "");outputText = colorEndRegex.Replace(outputText, "");return outputText;}#endregion/// <summary>/// 添加可視包圍框(測試用方法)/// </summary>private void AddVisibleBound(){int index = 0;foreach (Transform item in this.gameObject.transform.transform){Destroy(item.gameObject);}foreach (var hyperLinkInfo in hyperlinkInfoList){Color color = new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), 0.2f);index++;foreach (Rect rect in hyperLinkInfo.BoxList){GameObject gameObject = new GameObject();gameObject.name = string.Format("GOBoundBox[{0}]", hyperLinkInfo.InnerValue);gameObject.transform.SetParent(this.gameObject.transform);RectTransform rectTransform = gameObject.AddComponent<RectTransform>();rectTransform.sizeDelta = rect.size;rectTransform.localPosition = new Vector3(rect.position.x + rect.size.x / 2, rect.position.y + rect.size.y / 2, 0);Image image = gameObject.AddComponent<Image>();image.color = color;image.raycastTarget = false;}}}} }

總結

以上是生活随笔為你收集整理的Unity超链接:支持点击事件,下划线以及自定义颜色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。