//处理超链接的信息 private void DealHrefInfo(VertexHelper toFill) { if (_listHrefInfos.Count > 0) { #if UNITY_2019_1_OR_NEWER bool autoLF = AutoLF(); #endif // 处理超链接包围框 for (int i = 0; i < _listHrefInfos.Count; i++) { _listHrefInfos[i].Boxes.Clear(); #if UNITY_2019_1_OR_NEWER int startIndex = autoLF ? _listHrefInfos[i].StartIndex : _listHrefInfos[i].NewStartIndex; int endIndex = autoLF ? _listHrefInfos[i].EndIndex : _listHrefInfos[i].NewEndIndex; #else int startIndex = _listHrefInfos[i].StartIndex + 1; int endIndex = _listHrefInfos[i].EndIndex + 1; #endif if (startIndex >= toFill.currentVertCount) { continue; } toFill.PopulateUIVertex(ref _tempVertex, startIndex); // 将超链接里面的文本顶点索引坐标加入到包围框 var pos = _tempVertex.position; var bounds = new Bounds(pos, Vector3.zero); for (int j = startIndex; j <= endIndex; j++) { if (j >= toFill.currentVertCount) { break; } toFill.PopulateUIVertex(ref _tempVertex, j); pos = _tempVertex.position; if (pos.x < bounds.min.x) { // 换行重新添加包围框 _listHrefInfos[i].Boxes.Add(new Rect(bounds.min, bounds.size)); bounds = new Bounds(pos, Vector3.zero); } else { bounds.Encapsulate(pos); // 扩展包围框 } } //添加包围盒 _listHrefInfos[i].Boxes.Add(new Rect(bounds.min, bounds.size)); } //添加下划线 Vector2 extents = rectTransform.rect.size; var settings = GetGenerationSettings(extents); TextGenerator underlineText = Pool <TextGenerator> .Get(); underlineText.Populate("_", settings); IList <UIVertex> tut = underlineText.verts; for (int m = 0; m < _listHrefInfos.Count; m++) { for (int i = 0; i < _listHrefInfos[m].Boxes.Count; i++) { //计算下划线的位置 Vector3[] ulPos = new Vector3[4]; if (isVerticle) { ulPos[0] = _listHrefInfos[m].Boxes[i].position + new Vector2(-fontSize * 0.2f, 0); ulPos[1] = ulPos[0] + new Vector3(0, _listHrefInfos[m].Boxes[i].height); ulPos[2] = _listHrefInfos[m].Boxes[i].position + new Vector2(0, _listHrefInfos[m].Boxes[i].height); ulPos[3] = _listHrefInfos[m].Boxes[i].position; } else { ulPos[0] = _listHrefInfos[m].Boxes[i].position + new Vector2(0.0f, -fontSize * 0.2f); ulPos[1] = ulPos[0] + new Vector3(_listHrefInfos[m].Boxes[i].width, 0.0f); ulPos[2] = _listHrefInfos[m].Boxes[i].position + new Vector2(_listHrefInfos[m].Boxes[i].width, 0.0f); ulPos[3] = _listHrefInfos[m].Boxes[i].position; } //绘制下划线 for (int j = 0; j < 4; j++) { m_TempVerts[j] = tut[j]; m_TempVerts[j].color = Color.blue; m_TempVerts[j].position = ulPos[j]; m_TempVerts[j].uv1 = -Vector2.one; m_TempVerts[j].uv2 = Vector2.zero; if (j == 3) { toFill.AddUIVertexQuad(m_TempVerts); } } } } //回收下划线的对象 Pool <TextGenerator> .Release(underlineText); } }
private string GetOutputText(string inputText) { //回收各种对象 ReleaseSpriteTageInfo(); ReleaseHrefInfos(); if (string.IsNullOrEmpty(inputText)) { return(""); } _textBuilder.Clear(); int textIndex = 0; int newIndex = 0; string part = ""; int tempIndex = 0; int vertSum = 0; if (tagParser == null) { tagParser = new TagParser.TagParser(); } tagParser.Reset(); tagParser.ParseText(inputText); tagBegin = 0; _textBuilder.Append(inputText); for (int i = 0; i < tagParser.Tags.Count; i++) { var tag = tagParser.Tags[i]; // _textBuilder.AppendFormat(g.TextWithNoTags.Substring(tagBegin, tag.startIndex - tagBegin)); // tagBegin += tag.endIndex - tag.startIndex; switch (tag.Name) { case "sprite": { int spriteSub = 0; string spriteTag = ""; for (int j = 0; j < tag.Properties.Count; j++) { var tempId = tag.Properties[j].key; var tempTag = tag.Properties[j].value; if (tempId == "id") { spriteSub = int.Parse(tempTag); } if (tempId == "tag") { spriteTag = tempTag; } } SpriteInfoGroup tempGroup = SpriteAssetManager.Instance.GetSpriteGroup(spriteSub, spriteTag); if (tempGroup == null) { continue; } //清理标签 SpriteTagInfo tempSpriteTag = Pool <SpriteTagInfo> .Get(); tempSpriteTag.Index = (tag.endIndex - tag.lineIndex) * 4; var s = fontSize / tempGroup.Size; // _textBuilder.AppendFormat("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width); _textBuilder.Replace(string.Format("<{0}></sprite>", tag.content), string.Format("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width)); tempSpriteTag.Id = spriteSub; tempSpriteTag.Tag = spriteTag; tempSpriteTag.Size = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size); tempSpriteTag.UVs = tempGroup.ListSpriteInfor[0].Uv; //添加正则表达式的信息 SpriteTagInfoDic.TryGetValue(tempSpriteTag.Id, out var list); if (list == null) { if (SpriteTagInfoDic.ContainsKey(tempSpriteTag.Id)) { SpriteTagInfoDic.Remove(tempSpriteTag.Id); } list = new List <SpriteTagInfo>(); SpriteTagInfoDic.Add(tempSpriteTag.Id, list); } list.Add(tempSpriteTag); SpriteEmoji spriteEmoji = GetSpriteEmoji(spriteSub); spriteEmoji.m_spriteAsset = SpriteAssetManager.Instance.GetSpriteAsset(spriteSub); spriteEmoji.material.mainTexture = SpriteAssetManager.Instance.GetSpriteTexture(spriteSub); spriteEmoji.ClearEmojiVert(); spriteEmoji.SetAllDirty(); break; } case "herf": { int herfid = 0; for (int j = 0; j < tag.Properties.Count; j++) { var tempId = tag.Properties[j].key; var tempTag = tag.Properties[j].value; if (tempId == "id") { herfid = int.Parse(tempTag); } } _textBuilder.Replace(string.Format("<{0}>{1}</herf>", tag.content, tag.Value), string.Format("<color=blue>{0}</color>", tag.Value)); var hrefInfo = Pool <HrefInfo> .Get(); hrefInfo.Id = Mathf.Abs(herfid); hrefInfo.StartIndex = (tag.startIndex - tag.lineIndex) * 4;; // 超链接里的文本起始顶点索引 hrefInfo.EndIndex = (tag.endIndex - tag.lineIndex) * 4;; #if UNITY_2019_1_OR_NEWER hrefInfo.NewStartIndex = newStartIndex; hrefInfo.NewEndIndex = newIndex - 1; #endif hrefInfo.Name = tag.Name; hrefInfo.HrefValue = tag.Value; _listHrefInfos.Add(hrefInfo); break; } } } /* * _textBuilder.Clear(); * * foreach (Match match in _inputTagRegex.Matches(inputText)) * { * int tempId = 0; * if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-")) * tempId = int.Parse(match.Groups[1].Value); * string tempTag = match.Groups[2].Value; * var tagLength = tempTag.Length; * //更新超链接 * if (tempId < 0) * { * part = inputText.Substring(textIndex, match.Index - textIndex); * _textBuilder.Append(part); * * var lastLength = ReplaceRichText(_textBuilder.ToString()).Length; * int startIndex = (lastLength- vertSum)*4;; * _textBuilder.AppendFormat("<color=blue>{0}</color>", match.Groups[2].Value); * * vertSum += ReplaceRichText(_textBuilder.ToString()).Length - lastLength-tagLength; * int endIndex = startIndex + tagLength*4 ; * #if UNITY_2019_1_OR_NEWER * newIndex += ReplaceRichText(part).Length * 4; * int newStartIndex = newIndex; * newIndex += match.Groups[2].Value.Length * 4 + 8; #endif * * var hrefInfo = Pool<HrefInfo>.Get(); * hrefInfo.Id = Mathf.Abs(tempId); * hrefInfo.StartIndex = startIndex;// 超链接里的文本起始顶点索引 * hrefInfo.EndIndex = endIndex; #if UNITY_2019_1_OR_NEWER * hrefInfo.NewStartIndex = newStartIndex; * hrefInfo.NewEndIndex = newIndex - 1; #endif * hrefInfo.Name = match.Groups[2].Value; * hrefInfo.HrefValue = match.Groups[3].Value; * _listHrefInfos.Add(hrefInfo); * } * //更新表情 * else * { * SpriteInfoGroup tempGroup = SpriteAssetManager.Instance.GetSpriteGroup(tempId, tempTag); * if (tempGroup == null) * continue; * part = inputText.Substring(textIndex, match.Index - textIndex); * _textBuilder.Append(part); * * * var lastLength = ReplaceRichText(_textBuilder.ToString()).Length; * #if UNITY_2019_1_OR_NEWER * newIndex += ReplaceRichText(part).Length * 4; #endif * tempIndex = (lastLength- vertSum)*4; * var s= fontSize/tempGroup.Size; * _textBuilder.AppendFormat("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width); * vertSum += ReplaceRichText(_textBuilder.ToString()).Length - lastLength-1; * * //清理标签 * SpriteTagInfo tempSpriteTag = Pool<SpriteTagInfo>.Get(); * tempSpriteTag.Index = tempIndex; * #if UNITY_2019_1_OR_NEWER * tempSpriteTag.NewIndex = newIndex; #endif * * tempSpriteTag.Id = tempId; * tempSpriteTag.Tag = tempTag; * tempSpriteTag.Size = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size); * tempSpriteTag.UVs = tempGroup.ListSpriteInfor[0].Uv; * * //添加正则表达式的信息 * SpriteTagInfoDic.TryGetValue(tempSpriteTag.Id, out var list); * if (list == null) * { * if (SpriteTagInfoDic.ContainsKey(tempSpriteTag.Id)) * { * SpriteTagInfoDic.Remove(tempSpriteTag.Id); * } * * list = new List<SpriteTagInfo>(); * SpriteTagInfoDic.Add(tempSpriteTag.Id,list); * } * list.Add(tempSpriteTag); * SpriteEmoji spriteEmoji = GetSpriteEmoji(tempId); * spriteEmoji.m_spriteAsset =SpriteAssetManager.Instance.GetSpriteAsset(tempId); * spriteEmoji.material.mainTexture = SpriteAssetManager.Instance.GetSpriteTexture(tempId); * spriteEmoji.ClearEmojiVert(); * spriteEmoji.SetAllDirty(); #if UNITY_2019_1_OR_NEWER * newIndex += 4; #endif * } * * * textIndex = match.Index + match.Length; * } * * _textBuilder.Append(inputText.Substring(textIndex, inputText.Length - textIndex)); */ return(_textBuilder.ToString()); }