Esempio n. 1
0
        //处理超链接的信息
        private void DealHrefInfo(VertexHelper toFill)
        {
            if (_listHrefInfos.Count > 0)
            {
                // 处理超链接包围框
                for (int i = 0; i < _listHrefInfos.Count; i++)
                {
                    _listHrefInfos[i].Boxes.Clear();
                    if (_listHrefInfos[i].StartIndex >= toFill.currentVertCount)
                    {
                        continue;
                    }

                    toFill.PopulateUIVertex(ref _tempVertex, _listHrefInfos[i].StartIndex);
                    // 将超链接里面的文本顶点索引坐标加入到包围框
                    var pos    = _tempVertex.position;
                    var bounds = new Bounds(pos, Vector3.zero);
                    for (int j = _listHrefInfos[i].StartIndex + 1; j < _listHrefInfos[i].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];
                        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];
                            if (j == 3)
                            {
                                toFill.AddUIVertexQuad(m_TempVerts);
                            }
                        }
                    }
                }
                //回收下划线的对象
                Pool <TextGenerator> .Release(underlineText);
            }
        }
Esempio n. 2
0
        //根据正则规则更新文本
        private string GetOutputText(string inputText)
        {
            //回收各种对象
            ReleaseSpriteTageInfo();
            ReleaseHrefInfos();

            if (string.IsNullOrEmpty(inputText))
            {
                return("");
            }

            _textBuilder.Remove(0, _textBuilder.Length);
            int textIndex = 0;

            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;
                //更新超链接
                if (tempId < 0)
                {
                    _textBuilder.Append(inputText.Substring(textIndex, match.Index - textIndex));
                    _textBuilder.Append("<color=blue>");
                    int startIndex = _textBuilder.Length * 4;
                    _textBuilder.Append("[" + match.Groups[2].Value + "]");
                    int endIndex = _textBuilder.Length * 4 - 1;
                    _textBuilder.Append("</color>");


                    var hrefInfo = Pool <HrefInfo> .Get();

                    hrefInfo.Id         = Mathf.Abs(tempId);
                    hrefInfo.StartIndex = startIndex;                    // 超链接里的文本起始顶点索引
                    hrefInfo.EndIndex   = endIndex;
                    hrefInfo.Name       = match.Groups[2].Value;
                    hrefInfo.HrefValue  = match.Groups[3].Value;
                    _listHrefInfos.Add(hrefInfo);
                }
                //更新表情
                else
                {
                    if (_inlineManager == null || !_inlineManager.IndexSpriteInfo.ContainsKey(tempId) ||
                        !_inlineManager.IndexSpriteInfo[tempId].ContainsKey(tempTag))
                    {
                        continue;
                    }

                    SpriteInforGroup tempGroup = _inlineManager.IndexSpriteInfo[tempId][tempTag];

                    _textBuilder.Append(inputText.Substring(textIndex, match.Index - textIndex));
                    int tempIndex = _textBuilder.Length * 4;
                    _textBuilder.Append(@"<quad size=" + tempGroup.Size + " width=" + tempGroup.Width + " />");

                    //清理标签
                    SpriteTagInfo tempSpriteTag = Pool <SpriteTagInfo> .Get();

                    tempSpriteTag.Index = tempIndex;
                    tempSpriteTag.Id    = tempId;
                    tempSpriteTag.Tag   = tempTag;
                    tempSpriteTag.Size  = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size);
                    tempSpriteTag.UVs   = tempGroup.ListSpriteInfor[0].Uv;

                    //添加正则表达式的信息
                    _spriteInfo.Add(tempSpriteTag);
                }
                textIndex = match.Index + match.Length;
            }

            _textBuilder.Append(inputText.Substring(textIndex, inputText.Length - textIndex));
            return(_textBuilder.ToString());
        }