Exemple #1
0
        private void GenerateSimpleSprite(VertexHelper toFill, RichTextSprite richSprite, Sprite sprite)
        {
            UIVertex v           = UIVertex.simpleVert;
            var      vertexIndex = richSprite.GetVertexIndex() * 4;
            var      fetchIndex  = vertexIndex + 3;

            if (fetchIndex >= toFill.currentVertCount)
            {
                return;
            }

            toFill.PopulateUIVertex(ref v, fetchIndex);
            Vector3 textPos = v.position;

            var tagSize = richSprite.GetSize();

            var texture          = sprite.texture;
            var textureWidthInv  = 1.0f / texture.width;
            var textureHeightInv = 1.0f / texture.height;
            var uvRect           = sprite.textureRect;

            uvRect = new Rect(uvRect.x * textureWidthInv, uvRect.y * textureHeightInv, uvRect.width * textureWidthInv, uvRect.height * textureHeightInv);

            // pos = (0, 0)
            var position = new Vector3(0, 0, 0) + textPos;
            var uv0      = new Vector2(uvRect.x, uvRect.y);

            setSpriteVertex(toFill, vertexIndex, position, uv0);

            var fillAmount = richSprite.GetFillAmount();

            // pos = (1, 0)
            position = new Vector3(tagSize.x * fillAmount, 0, 0) + textPos;
            uv0      = new Vector2(uvRect.x + uvRect.width * fillAmount, uvRect.y);
            setSpriteVertex(toFill, ++vertexIndex, position, uv0);

            // pos = (1, 1)
            position = new Vector3(tagSize.x * fillAmount, tagSize.y, 0) + textPos;
            uv0      = new Vector2(uvRect.x + uvRect.width * fillAmount, uvRect.y + uvRect.height);
            setSpriteVertex(toFill, ++vertexIndex, position, uv0);

            // pos = (0, 1)
            position = new Vector3(0, tagSize.y, 0) + textPos;
            uv0      = new Vector2(uvRect.x, uvRect.y + uvRect.height);
            setSpriteVertex(toFill, ++vertexIndex, position, uv0);
        }
Exemple #2
0
        private void GenerateSlicedSprite(VertexHelper toFill, RichTextSprite richSprite, Sprite sprite)
        {
            UIVertex v           = UIVertex.simpleVert;
            var      vertexIndex = richSprite.GetVertexIndex() * 4;
            var      fetchIndex  = vertexIndex + 3;

            if (fetchIndex >= toFill.currentVertCount)
            {
                return;
            }

            toFill.PopulateUIVertex(ref v, fetchIndex);
            Vector3 textPos = v.position;

            var tagSize = richSprite.GetSize();

            Vector4 border          = sprite.border;
            Vector4 adjustedBorders = border;

            Vector4 outer, inner;

            outer = Sprites.DataUtility.GetOuterUV(sprite);
            inner = Sprites.DataUtility.GetInnerUV(sprite);

            s_VertScratch[0] = new Vector2(0, 0);
            s_VertScratch[3] = new Vector2(tagSize.x, tagSize.y);

            s_VertScratch[1].x = adjustedBorders.x;
            s_VertScratch[1].y = adjustedBorders.y;

            s_VertScratch[2].x = tagSize.x - adjustedBorders.z;
            s_VertScratch[2].y = tagSize.y - adjustedBorders.w;

            for (int i = 0; i < 4; ++i)
            {
                s_VertScratch[i].x += textPos.x;
                s_VertScratch[i].y += textPos.y;
            }

            s_UVScratch[0] = new Vector2(outer.x, outer.y);
            s_UVScratch[1] = new Vector2(inner.x, inner.y);
            s_UVScratch[2] = new Vector2(inner.z, inner.w);
            s_UVScratch[3] = new Vector2(outer.z, outer.w);

            int vertexCount = 0;

            for (int x = 0; x < 3; ++x)
            {
                int x2 = x + 1;

                for (int y = 0; y < 3; ++y)
                {
                    int y2 = y + 1;

                    addSpriteQuad(toFill,
                                  vertexIndex + vertexCount,
                                  new Vector2(s_VertScratch[x].x, s_VertScratch[y].y),
                                  new Vector2(s_VertScratch[x2].x, s_VertScratch[y2].y),
                                  new Vector2(s_UVScratch[x].x, s_UVScratch[y].y),
                                  new Vector2(s_UVScratch[x2].x, s_UVScratch[y2].y));

                    vertexCount += 4;
                }
            }
        }