Example #1
0
        protected override void OnPopulateMesh(VertexHelper toFill)
        {
            if (!m_SupportEmoji)
            {
                base.OnPopulateMesh(toFill);
                return;
            }
            if (font == null)
            {
                return;
            }

            m_DisableFontTextureRebuiltCallback = true;

            Material mat = materialForRendering;

            int   emojIndex = 0;
            Emoji emoj      = mEmojies.Count > 0 ? mEmojies[emojIndex] : default(Emoji);

            Vector2 extents = rectTransform.rect.size;

            var settings = GetGenerationSettings(extents);

            cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
            // Apply the offset to the vertices
            IList <UIVertex> verts         = cachedTextGenerator.verts;
            float            unitsPerPixel = 1 / pixelsPerUnit;
            //Last 4 verts are always a new line... (\n)
            int vertCount = verts.Count - 4;

            Vector2 roundingOffset = verts.Count == 0 ? Vector2.zero : new Vector2(verts[0].position.x, verts[0].position.y) * unitsPerPixel;

            roundingOffset = PixelAdjustPoint(roundingOffset) - roundingOffset;
            toFill.Clear();
            if (roundingOffset != Vector2.zero)
            {
                for (int i = 0; i < vertCount; ++i)
                {
                    int tempVertsIndex = i & 3;
                    m_TempVerts[tempVertsIndex]             = verts[i];
                    m_TempVerts[tempVertsIndex].position   *= unitsPerPixel;
                    m_TempVerts[tempVertsIndex].position.x += roundingOffset.x;
                    m_TempVerts[tempVertsIndex].position.y += roundingOffset.y;
                    if (tempVertsIndex == 3)
                    {
                        toFill.AddUIVertexQuad(m_TempVerts);
                    }
                }
            }
            else
            {
                Vector2 duv = Vector2.one * 2;
                for (int i = 0; i < vertCount; ++i)
                {
                    int tempVertsIndex = i & 3;
                    m_TempVerts[tempVertsIndex]           = verts[i];
                    m_TempVerts[tempVertsIndex].position *= unitsPerPixel;
                    if (tempVertsIndex == 3)
                    {
                        int es = 0;
                        while (emojIndex < mEmojies.Count && emoj.charIndex == (i >> 2) && mat != null)
                        {
                            es++;
                            string sprName;
                            float  rot;
                            emoj.GetFrame(mTime, out sprName, out rot);
                            Sprite spr = GetSprite(sprName);
                            if (spr != null)
                            {
                                ModifyTempVertsUV(spr, duv);
                                if (Mathf.Abs(rot) > 0.1)
                                {
                                    RotateTempVerts(rot);
                                }
                                if (emoj.offset.sqrMagnitude > 0.001)
                                {
                                    var offset = (Vector3)emoj.offset;
                                    if (Mathf.Abs(offset.x) < 1)
                                    {
                                        offset.x = Mathf.Abs(m_TempVerts[3].position.x - m_TempVerts[0].position.x) * offset.x;
                                    }
                                    if (Mathf.Abs(offset.y) < 1)
                                    {
                                        offset.y = Mathf.Abs(m_TempVerts[3].position.y - m_TempVerts[0].position.y) * offset.y;
                                    }
                                    MoveTempVerts(offset);
                                }
                                if (emoj.raw)
                                {
                                    var col = Color.white;
                                    col.a = color.a;
                                    ModifyTempVertsColor(col);
                                }
                                var btn = GetEmojiBtn(emoj, emojIndex);
                                if (btn != null)
                                {
                                    btn.rect = CalculateTempVertsBounds();
                                }
                                mat.SetTexture(mTexId, spr.texture);
                                toFill.AddUIVertexQuad(m_TempVerts);
                            }
                            if (++emojIndex < mEmojies.Count)
                            {
                                emoj = mEmojies[emojIndex];
                            }
                        }
                        if (es == 0)
                        {
                            toFill.AddUIVertexQuad(m_TempVerts);
                        }
                    }
                }
            }

            m_DisableFontTextureRebuiltCallback = false;
        }