//字符串网格控制 public override bool HandleTextMesh(URichTextParseInfo info) { if (info.vertStartIndex < 0) { return(false); } int vertEndIndex = GetVertEndIndex(info.vertStartIndex, endMsg); int vertStartIndex = info.vertStartIndex + preMsg.Length; //可以考虑用内存池,减少内存分配 var output = new List <UIVertex>(); vHelper.GetUIVertexStream(output); //绘制下划线 vHelper.Clear(); List <Rect> lineRects = richText.GetTextTiles(vertStartIndex, vertEndIndex, output); for (int i = 0; i < lineRects.Count; i++) { output.AddRange(URichTextUtil.GetUnderLineVerts(lineRects[i], vertEndIndex, richText.GetUnderLineInfo())); } //添加的顶点数组 vHelper.AddUIVertexTriangleStream(output); info.urlHrefInfo.m_tagTextRects.AddRange(lineRects); info.urlHrefInfo.strHrefEvent = info.values[1].ToString(); return(true); }
public static bool HandleTextMesh(URichTextParseInfo info, VertexHelper toFill, URichText text) { //Debug.Log("HandleMesh [" + info.type + "] :" + info.vertStartIndex + " " + (info.startIndex + info.realLength-1) + " " + info.realLength); handlers[(int)info.type].richText = text; handlers[(int)info.type].vHelper = toFill; bool success = handlers[(int)info.type].HandleTextMesh(info); handlers[(int)info.type].curIndex++; return(success); }
public static int CompareByIndex(URichTextParseInfo x, URichTextParseInfo y)//从大到小排序器 { if (x == null) { Debug.LogError("the x URichTextParseInfo isNUll"); return(0); } if (y == null) { Debug.LogError("the y URichTextParseInfo isNUll"); return(0); } int retval = x.sortIndex.CompareTo(y.sortIndex); return(retval); }
public virtual bool HandleTextMesh(URichTextParseInfo info) { return(true); }
//字符串网格控制 public override bool HandleTextMesh(URichTextParseInfo info) { return(true); }
public static void AnalyzeText(ref string text) { hrefInfos.Clear(); if (text == "" || text.Length <= 3) { return; } if (!isInit) { Init(); } //***********第一步:对整个字符串进行正则表达式替换********// //三种正则: //自定义的([x],[\x]) 替换成## //系统A类型的(<x>,<\x>) 替换成## //系统B类型(<x/>) 替换成$$ string beginFlagText = text; //记录富文本的实际开始作用index string originalText = text; for (int n = 0; n < handlers.Count; n++) { URichTextHandler_Base handler = handlers[n]; int regexCount = 2; if (handler.IsSingleRegex()) { regexCount = 1; } for (int i = 0; i < regexCount; i++) { s_TextBuilder.Length = 0; Regex s_Regex = new Regex(handler.GetRegexInfo()[i], RegexOptions.Singleline); int isHead = regexCount == 1 ? -1 : i; foreach (Match match in s_Regex.Matches(originalText)) { var group = match.Groups[1]; string[] richValues = new string[match.Groups.Count]; var hrefInfo = new URichTextParseInfo { sortIndex = match.Index, type = (RichType)(n), values = match.Groups, isHead = isHead }; ////替换字符串,进替换匹配到的第一个 int length = match.Value.Length; int index = text.IndexOf(match.Value); text = ReplaceTheFirst(text, match.Value, headPlaceholder); beginFlagText = ReplaceTheFirst(beginFlagText, match.Value, isHead == 1 ? "" : headPlaceholder); //text = text.Replace(match.Value, hrefInfo.isHead==1 ? tailPlaceholder:headPlaceholder); //重复replace,消耗性能 hrefInfos.Add(hrefInfo); } //Debug.Log(beginFlagText); //Debug.Log(text); } } //***********第二步:对href进行排序********// hrefInfos.Sort(CompareByIndex); //***********第三步:对替换后的字符串进行分析********// int curTextIndex = 0; int curHrefIndex = 0; //遍历字符串 while (true) { int addTextCount = 1; //处理完后,会对字符串进行添加的字符数 if (curTextIndex >= 0 && curTextIndex < text.Length && text[curTextIndex].ToString() == headPlaceholder && curHrefIndex < hrefInfos.Count) { URichTextParseInfo hrefInfo = hrefInfos[curHrefIndex]; //Debug.Log("currrrr: " + hrefInfo.type + " " + hrefInfo.sortIndex +" "+ hrefInfo.isHead); int hrefStartIndex = curTextIndex; //富文本需要处理字符串的首字母index int hrefEndIndex = curTextIndex + 1; //富文本需要处理字符串的尾字母index int hrefLength = 0; //富文本需要处理字符串的长度 int hrefInsideCout = 0; //富文本需要处理字符串的中内嵌的占位符数量 //遍历起始占位符到结尾占位符之间的字符串 bool isFindEnd = false; if (hrefInfo.isHead != -1) { while (true) { if (hrefEndIndex >= 0 && hrefEndIndex < text.Length && text[hrefEndIndex].ToString() == headPlaceholder) { hrefInsideCout++; URichTextParseInfo nextHrefInfo = hrefInfos[curHrefIndex + hrefInsideCout]; //Debug.Log("nenenenenL: " + nextHrefInfo.type + " " + nextHrefInfo.sortIndex + " " + nextHrefInfo.isHead); //找到的末尾 if (nextHrefInfo.isHead == 1 && nextHrefInfo.type == hrefInfo.type) { hrefInfos.Remove(nextHrefInfo); isFindEnd = true; } } if (!isFindEnd) { hrefEndIndex++; } else { hrefInsideCout--; //减去末尾的 //检查是否合法: //... break; } if (hrefEndIndex >= text.Length) { break; } } if (isFindEnd) { //计算总长度 hrefLength = hrefEndIndex - hrefStartIndex - hrefInsideCout + 1 - 2; hrefInfo.vertStartIndex = curTextIndex; hrefInfo.realLength = hrefLength; //保存作用长度 text = handlers[(int)hrefInfo.type].HandleText(ref addTextCount, text, hrefStartIndex, hrefEndIndex, hrefLength, hrefInfo.values); curHrefIndex++; } else { curTextIndex = text.Length + 1; //如果没找到那就是该字符串有问题,直接返回 Debug.LogWarning("regex Type[" + hrefInfo.type + "] has no end!!!"); } } else { hrefInfo.vertStartIndex = curTextIndex; hrefInfo.realLength = 0; //保存作用长度 text = handlers[(int)hrefInfo.type].HandleText(ref addTextCount, text, hrefStartIndex, hrefStartIndex, hrefLength, hrefInfo.values); curHrefIndex++; } //Debug.Log("regex Type[" + hrefInfo.type + "]: " + text); } curTextIndex += addTextCount; if (curTextIndex >= text.Length) { break; } } //***********第四步:得到HrefInfo的开始作用Index********// curTextIndex = 0; curHrefIndex = 0; //遍历beginFlagText,找到开始的index while (true) { if (beginFlagText[curTextIndex].ToString() == headPlaceholder) { //Debug.Log(beginFlagText); if (curHrefIndex >= hrefInfos.Count) { break; } URichTextParseInfo hrefInfo = hrefInfos[curHrefIndex]; hrefInfo.startIndex = curTextIndex; beginFlagText = beginFlagText.Remove(curTextIndex, PLACE_HOLDER_LENGTH); curHrefIndex++; } else { curTextIndex++; } if (curTextIndex >= beginFlagText.Length) { break; } } //Debug.Log(beginFlagText); }
//字符串网格控制 public override bool HandleTextMesh(URichTextParseInfo info) { if (!Application.isPlaying) { Debug.LogWarning("请在运行模式下观察富文本转图片结果"); return(false); } if (info.vertStartIndex < 0) { return(false); } List <UIVertex> uivertList = new List <UIVertex>(); vHelper.GetUIVertexStream(uivertList); // 移除角落产生的字符纹理 if (info.vertStartIndex * 6 + 6 > uivertList.Count) { return(false); } int spriteIndex = 0; Data.AnimSpriteConfig.SSchemeAnimFrameData frameData = Data.AnimSpriteConfig.AnimSpriteConfig.Instance.GetData(info.values[1].Value); if (frameData == null) { return(false); } Rect newSpriteRect = frameData.GetFrameRect(spriteIndex);//sInfo.rect; URichTextSpriteMeshInfo meshInfo = new URichTextSpriteMeshInfo(); if (frameData.nAnimFrame > 0) { meshInfo.animKey = frameData.strAnimName; } else { meshInfo.animKey = ""; } meshInfo.animLen = frameData.nAnimFrame == 0?1: frameData.nAnimFrame; meshInfo.spriteIndex = spriteIndex; meshInfo.place = info.startIndex; //顶点: //textpos:图片的左上角 //之前的富文本被隐藏部分也会占用顶点!! //每个索引一个顶点...所以要乘以6 Vector3 textpos = uivertList[info.vertStartIndex * 6].position; int size = int.Parse(info.values[2].Value); int width = int.Parse(info.values[3].Value); //设置图片的位置 meshInfo.vertices[0] = new Vector3(0, 0, 0) + textpos; meshInfo.vertices[1] = new Vector3(size * width, 0, 0) + textpos; meshInfo.vertices[2] = new Vector3(size * width, -size, 0) + textpos; meshInfo.vertices[3] = new Vector3(0, -size, 0) + textpos; //uv Sprite frameSprite = USpriteManager.Instance.GetAnimSprite(frameData.strAnimName); if (frameSprite == null) { return(false); } Vector2 newTexSize = new Vector2(frameSprite.texture.width, frameSprite.texture.height); meshInfo.uvs[3] = new Vector2(newSpriteRect.x / newTexSize.x, 1 - (newSpriteRect.y + newSpriteRect.height) / newTexSize.y); //new Vector2(newSpriteRect.x / newTexSize.x, newSpriteRect.y / newTexSize.y); meshInfo.uvs[1] = new Vector2((newSpriteRect.x + newSpriteRect.width) / newTexSize.x, 1 - newSpriteRect.y / newTexSize.y); //new Vector2((newSpriteRect.x + newSpriteRect.width) / newTexSize.x, (newSpriteRect.y + newSpriteRect.height) / newTexSize.y); meshInfo.uvs[2] = new Vector2((newSpriteRect.x + newSpriteRect.width) / newTexSize.x, 1 - (newSpriteRect.y + newSpriteRect.height) / newTexSize.y); //new Vector2((newSpriteRect.x + newSpriteRect.width) / newTexSize.x, newSpriteRect.y / newTexSize.y); meshInfo.uvs[0] = new Vector2(newSpriteRect.x / newTexSize.x, 1 - newSpriteRect.y / newTexSize.y); //new Vector2(newSpriteRect.x / newTexSize.x, (newSpriteRect.y + newSpriteRect.height) / newTexSize.y); meshInfo.colors[0] = Color.white; meshInfo.colors[1] = Color.white; meshInfo.colors[2] = Color.white; meshInfo.colors[3] = Color.white; //索引先不处理 TextSprite.UpdateSpriteMeshInfo(meshInfo); return(true); }