コード例 #1
0
        void GenerateText()
        {
            bool regenText = isTextDirty || lastExtents != rectTransform.rect.size;

            if (isTextDirty)
            {
                isTextDirty = false;
                if (m_hrefText == null)
                {
                    m_hrefText = new HrefText();
                }
                if (info.OriginalText != originalText)
                {
                    m_hrefText.getHrefList.Clear();
                    string m_outText = m_hrefText.getResolveText(originalText);

                    if (m_ImageFont)
                    {
                        info = m_ImageFont.ReplaceText(m_outText, imageSize);
                    }
                    else
                    {
                        return;
                    }
                }
                m_Text = info.ReplacedText;
            }
            if (info.Symbols == null)
            {
                return;
            }
            if (regenText)
            {
                lastExtents = rectTransform.rect.size;
                List <UIVertex> textVertList = null;
                if (!isPureEmoji || IgnoreVertexCache || vertCache == null || !vertCache.TryGetValue(info.Symbols.Length, out textVertList))
                {
                    textVertList = new List <UIVertex>();
                    OnTextFillVBO(textVertList);

                    // 处理超链接包含的点击区域
                    foreach (var hrefInfo in m_hrefText.getHrefList)
                    {
                        hrefInfo.boxes.Clear();
                        if (hrefInfo.startIndex >= textVertList.Count)
                        {
                            continue;
                        }

                        // 将超链接里面的文本顶点索引坐标加入到点击区域内
                        var pos    = textVertList[hrefInfo.startIndex];
                        var bounds = new Bounds(pos.position, Vector3.zero);
                        for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
                        {
                            if (i >= textVertList.Count)
                            {
                                break;
                            }

                            pos = textVertList[i];
                            if (pos.position.x < bounds.min.x) // 换行重新添加点击区域
                            {
                                hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
                                bounds = new Bounds(pos.position, Vector3.zero);
                            }
                            else
                            {
                                bounds.Encapsulate(pos.position); //再次扩展范围
                            }
                        }
                        hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
                    }

                    if (isPureEmoji && !IgnoreVertexCache)
                    {
                        if (vertCache == null)
                        {
                            vertCache = new Dictionary <int, List <UIVertex> >();
                        }
                        vertCache[info.Symbols.Length] = textVertList;
                    }
                }
                Vector3 offset = new Vector3(0, -fontSize / 4f, 0);
                int     newIdx = 0;
                int     skip   = 0;
#if !UNITY_5_2
                int totalSkip = 0;
#endif
                SymbolSpriteInfo lastSprite = default(SymbolSpriteInfo);
                int  counter    = 0;
                bool isLastSkip = false;
                vertImg.Clear();
                vertTxt.Clear();

                for (int i = 0; i < textVertList.Count; i++)
                {
                    if (skip > 0)
                    {
#if UNITY_5_2
                        if (skip <= 4 && skip > 0)
#else
                        int processed = totalSkip - skip;
                        if (processed < 4)
#endif
                        {
                            UIVertex vert = new UIVertex();
                            UIVertex old  = textVertList[i];
#if UNITY_5_2
                            int idx = 4 - skip;
#else
                            int idx = processed;
#endif
                            vert.position = old.position + offset;
                            vert.uv0      = lastSprite.UV[idx];
                            vert.normal   = old.normal;
                            vert.tangent  = old.tangent;
                            Color32 col = new Color32(255, 255, 255, 255);
                            col.a      = old.color.a;
                            vert.color = col;
                            vertImg.Add(vert);

                            /*m_TempVerts[idx] = vert;
                             * if (idx == 3)
                             * {
                             *  vhImg.AddUIVertexQuad(m_TempVerts);
                             * }*/
                            counter++;
                        }
                        skip--;
                        continue;
                    }
                    if (newIdx < info.Symbols.Length)
                    {
                        int idx = counter / 4;
                        lastSprite = info.Symbols[newIdx];
                        if (idx == lastSprite.Index)
                        {
#if UNITY_5_2
                            skip = info.ReplacedSpriteLength * 4 - 1;
#else
                            totalSkip = info.ReplacedSpriteLength * 4;
                            i--;
                            skip = totalSkip;
#endif
                            newIdx++;
                            continue;
                        }
                    }
                    int tmpIdx = counter % 4;
                    if (tmpIdx == 0)
                    {
                        //filter junk vertices
                        if (textVertList[i].position == textVertList[i + 1].position)
                        {
                            isLastSkip = true;
                        }
                        else
                        {
                            isLastSkip = false;
                        }
                    }
                    if (!isLastSkip)
                    {
                        vertTxt.Add(textVertList[i]);
                    }

                    /*m_TempVerts[tmpIdx] = textVertList[i];
                     * if (tmpIdx == 3)
                     * {
                     *  if (_toFill != null)
                     *  {
                     *      _toFill.AddUIVertexQuad(m_TempVerts);
                     *  }
                     * }*/
                    counter++;
                }
            }
            else
            {
                Color32 col = color;
                for (int i = 0; i < vertImg.size; i++)
                {
                    var vert = vertImg[i];
                    vert.color = new Color32(255, 255, 255, col.a);
                    vertImg[i] = vert;
                }
                for (int i = 0; i < vertTxt.size; i++)
                {
                    var vert = vertTxt[i];
                    vert.color = col;
                    vertTxt[i] = vert;
                }
            }
            if (_toFill != null)
            {
                FillUIQuad(_toFill, vertTxt);
            }
            if (m_CachedInputRenderer)
            {
                m_CachedInputRenderer.ImageVertices = vertImg;
            }
        }
コード例 #2
0
ファイル: ImageFont.cs プロジェクト: kingpowervrg/my_seeker
        public ReplacementInfo ReplaceText(string text, float imgSize = 0f)
        {
            if (needRebuild || rootNode == null)
            {
                RebuildMapping();
            }

            ReplacementInfo res = new ReplacementInfo();

            res.OriginalText = text;
            List <DirtyWordNode> replacement;

            text.ReplaceDirtWord(rootNode, out replacement);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            res.Symbols = new SymbolSpriteInfo[replacement.Count];

            int curIdx       = 0;
            int newIdx       = 0;
            int appendLength = 0;
            int idx          = 0;

            foreach (var i in replacement)
            {
                SymbolSpriteInfo info = new SymbolSpriteInfo();

                sb.Append(text.Substring(curIdx, i.Index - curIdx));
                int prefixLength = 0;
                if (imgSize != 0)
                {
                    int b = sb.Length;
                    sb.Append("<size=");
                    sb.Append(imgSize);
                    sb.Append('>');
                    prefixLength = sb.Length - b;
                }
                appendLength += prefixLength;
                newIdx       += i.Index - curIdx + prefixLength;
                info.Index    = newIdx;
                newIdx++;

                Sprite sp = mapping[i.Word];
                info.UV = CalcUV(sp);

                Rect   r     = sp.rect;
                float  ratio = r.width / r.height;
                string rep   = string.Format("<quad width={0:0.00}>", ratio);
                res.ReplacedSpriteLength = rep.Length;
                sb.Append(rep);
                curIdx             = i.Index + i.Word.Length;
                res.Symbols[idx++] = info;
                if (imgSize != 0)
                {
                    sb.Append("</size>");
                    appendLength += 7;
                    newIdx       += 7;
                }
            }
            sb.Append(text.Substring(curIdx));
            res.ReplacedText = sb.ToString();

            return(res);
        }