UIFont.Print C# (CSharp) Méthode

Print() public méthode

Print the specified text into the buffers. Note: 'lineWidth' parameter should be in pixels.
public Print ( string text, Color32 color, BetterList verts, BetterList uvs, BetterList cols, bool encoding, SymbolStyle symbolStyle, Alignment, alignment, int lineWidth, bool premultiply ) : void
text string
color UnityEngine.Color32
verts BetterList
uvs BetterList
cols BetterList
encoding bool
symbolStyle SymbolStyle
alignment Alignment,
lineWidth int
premultiply bool
Résultat void
    public void Print(string text, Color32 color, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols,
        bool encoding, SymbolStyle symbolStyle, Alignment alignment, int lineWidth, bool premultiply)
    {
        if (mReplacement != null)
        {
            mReplacement.Print(text, color, verts, uvs, cols, encoding, symbolStyle, alignment, lineWidth, premultiply);
        }
        else if (mFont != null && text != null)
        {
            if (!mFont.isValid)
            {
                Debug.LogError("Attempting to print using an invalid font!");
                return;
            }

            mColors.Clear();
            mColors.Add(color);

            Vector2 scale = mFont.charSize > 0 ? new Vector2(1f / mFont.charSize, 1f / mFont.charSize) : Vector2.one;

            int indexOffset = verts.size;
            int maxX = 0;
            int x = 0;
            int y = 0;
            int prev = 0;
            int lineHeight = (mFont.charSize + mSpacingY);
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            float invX = uvRect.width / mFont.texWidth;
            float invY = mUVRect.height / mFont.texHeight;
            int textLength = text.Length;
            bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols && sprite != null;

            for (int i = 0; i < textLength; ++i)
            {
                char c = text[i];

                if (c == '\n')
                {
                    if (x > maxX) maxX = x;

                    if (alignment != Alignment.Left)
                    {
                        Align(verts, indexOffset, alignment, x, lineWidth);
                        indexOffset = verts.size;
                    }

                    x = 0;
                    y += lineHeight;
                    prev = 0;
                    continue;
                }

                if (c < ' ')
                {
                    prev = 0;
                    continue;
                }

                if (encoding && c == '[')
                {
                    int retVal = NGUITools.ParseSymbol(text, i, mColors, premultiply);

                    if (retVal > 0)
                    {
                        color = mColors[mColors.Count - 1];
                        i += retVal - 1;
                        continue;
                    }
                }

                // See if there is a symbol matching this text
                BMSymbol symbol = useSymbols ? MatchSymbol(text, i, textLength) : null;

                if (symbol == null)
                {
                    BMGlyph glyph = mFont.GetGlyph(c);
                    if (glyph == null) continue;

                    if (prev != 0) x += glyph.GetKerning(prev);

                    if (c == ' ')
                    {
                        x += mSpacingX + glyph.advance;
                        prev = c;
                        continue;
                    }

                    v0.x =  scale.x * (x + glyph.offsetX);
                    v0.y = -scale.y * (y + glyph.offsetY);

                    v1.x = v0.x + scale.x * glyph.width;
                    v1.y = v0.y - scale.y * glyph.height;

                    u0.x = mUVRect.xMin + invX * glyph.x;
                    u0.y = mUVRect.yMax - invY * glyph.y;

                    u1.x = u0.x + invX * glyph.width;
                    u1.y = u0.y - invY * glyph.height;

                    x += mSpacingX + glyph.advance;
                    prev = c;

                    if (glyph.channel == 0 || glyph.channel == 15)
                    {
                        for (int b = 0; b < 4; ++b) cols.Add(color);
                    }
                    else
                    {
                        // Packed fonts come as alpha masks in each of the RGBA channels.
                        // In order to use it we need to use a special shader.
                        //
                        // Limitations:
                        // - Effects (drop shadow, outline) will not work.
                        // - Should not be a part of the atlas (eastern fonts rarely are anyway).
                        // - Lower color precision

                        Color col = color;

                        col *= 0.49f;

                        switch (glyph.channel)
                        {
                            case 1: col.b += 0.51f; break;
                            case 2: col.g += 0.51f; break;
                            case 4: col.r += 0.51f; break;
                            case 8: col.a += 0.51f; break;
                        }

                        for (int b = 0; b < 4; ++b) cols.Add(col);
                    }
                }
                else
                {
                    v0.x =  scale.x * (x + symbol.offsetX);
                    v0.y = -scale.y * (y + symbol.offsetY);

                    v1.x = v0.x + scale.x * symbol.width;
                    v1.y = v0.y - scale.y * symbol.height;

                    Rect uv = symbol.uvRect;

                    u0.x = uv.xMin;
                    u0.y = uv.yMax;
                    u1.x = uv.xMax;
                    u1.y = uv.yMin;

                    x += mSpacingX + symbol.advance;
                    i += symbol.length - 1;
                    prev = 0;

                    if (symbolStyle == SymbolStyle.Colored)
                    {
                        for (int b = 0; b < 4; ++b) cols.Add(color);
                    }
                    else
                    {
                        Color32 col = Color.white;
                        col.a = color.a;
                        for (int b = 0; b < 4; ++b) cols.Add(col);
                    }
                }

                verts.Add(new Vector3(v1.x, v0.y));
                verts.Add(new Vector3(v1.x, v1.y));
                verts.Add(new Vector3(v0.x, v1.y));
                verts.Add(new Vector3(v0.x, v0.y));

                uvs.Add(new Vector2(u1.x, u0.y));
                uvs.Add(new Vector2(u1.x, u1.y));
                uvs.Add(new Vector2(u0.x, u1.y));
                uvs.Add(new Vector2(u0.x, u0.y));
            }

            if (alignment != Alignment.Left && indexOffset < verts.size)
            {
                Align(verts, indexOffset, alignment, x, lineWidth);
                indexOffset = verts.size;
            }
        }
    }

Usage Example

Exemple #1
0
    public override void OnFill(BetterList <Vector3> verts, BetterList <Vector2> uvs, BetterList <Color32> cols)
#endif
    {
        if (mFont == null)
        {
            return;
        }
        MakePositionPerfect();
        Pivot p      = pivot;
        int   offset = verts.size;

        // Print the text into the buffers
        if (p == Pivot.Left || p == Pivot.TopLeft || p == Pivot.BottomLeft)
        {
            mFont.Print(processedText, color, verts, uvs, cols, mEncoding, mSymbols, UIFont.Alignment.Left, 0);
        }
        else if (p == Pivot.Right || p == Pivot.TopRight || p == Pivot.BottomRight)
        {
            mFont.Print(processedText, color, verts, uvs, cols, mEncoding, mSymbols, UIFont.Alignment.Right,
                        Mathf.RoundToInt(relativeSize.x * mFont.size));
        }
        else
        {
            mFont.Print(processedText, color, verts, uvs, cols, mEncoding, mSymbols, UIFont.Alignment.Center,
                        Mathf.RoundToInt(relativeSize.x * mFont.size));
        }

        // Apply an effect if one was requested
        if (effectStyle != Effect.None)
        {
            Vector3 scale = cachedTransform.localScale;
            if (scale.x == 0f || scale.y == 0f)
            {
                return;
            }

            int   end   = verts.size;
            float pixel = 1f / mFont.size;

            ApplyShadow(verts, uvs, cols, offset, end, pixel, -pixel);

            if (effectStyle == Effect.Outline)
            {
                offset = end;
                end    = verts.size;

                ApplyShadow(verts, uvs, cols, offset, end, -pixel, pixel);

                offset = end;
                end    = verts.size;

                ApplyShadow(verts, uvs, cols, offset, end, pixel, pixel);

                offset = end;
                end    = verts.size;

                ApplyShadow(verts, uvs, cols, offset, end, -pixel, -pixel);
            }
        }
    }
All Usage Examples Of UIFont::Print