oddgames / UIToolkit

Single draw call UI solution for Unity with multi resolution support and more.
518 stars 153 forks source link

New line '\n' generates invalid characters, and text is not properly aligned #124

Open criistii opened 12 years ago

criistii commented 12 years ago

When creating a text like "Some\nText", the text is properly split into 2 lines, however, the second line has a strange character before it.

If Aligned to the left, it looks like this: Some þText

criistii commented 12 years ago

I think I fixed the bug, if anyone could update the code here, that would be great:

In the UIText class, in the drawText method (somewhere around line 250), make sure you add a new line validation when adding characters:

        // Extend end of line
        lineEndChar = i;

        if( charId != ASCII_NEWLINE )
        {
            // add quads for each char
            // Use curpos instead of i to compensate for line wrapping hyphenation
            // reuse a UISprite if we have one. if we don't, we need to set it's parent and add it to the textInstance's list
            var currentTextSprite = textInstance.textSpriteAtIndex( i );
            var addingNewTextSprite = currentTextSprite == null;

            currentTextSprite = configureSpriteForCharId( currentTextSprite, charId, dx, dy, scale, 0 );

            if( addingNewTextSprite )
            {
                currentTextSprite.color = color.Length == 1 ? color[0] : color[i];
                currentTextSprite.parentUIObject = textInstance;
                textInstance.textSprites.Add( currentTextSprite );
            }

            // Ensure the sprite is hidden if the textInstance is
            currentTextSprite.hidden = hidden;

            // See below @NOTE re: offsetx vs. xadvance bugfix.
            // advance the position to draw the next letter
            dx += _fontDetails[charId].xadvance * scale;
        }