Search Unity

TextMesh Pro Pixel Perfect Inline Sprite Sizing

Discussion in 'UGUI & TextMesh Pro' started by SweetBro, Mar 15, 2018.

  1. SweetBro

    SweetBro

    Joined:
    Jan 14, 2013
    Posts:
    69
    So here's what's up. I'm using TMPro to render a pixel font at 16, 32, and 42 pt sizes. I have a series of sprites/icons that are 32x42 px. I would like them to use as in line sprites with the 16 or 32 pt sized fonts.

    I'm aware that TMPro has a Scale Factor. However this is useless to me without knowing what scaling formula is being used, since due to this all being pixel art I need perfect precision. So unless there's a way to get the line height to automatically readjust based on the default size of sprite in it, does anyone know what the actual scaling formula is so I can determine what my SF needs to be?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Inline Sprites are normalized to match the size of the font asset assigned to the text object. More specifically, the height of the sprite is adjusted to match the Ascender height.

    Code (csharp):
    1. // The sprite scale calculations are based on the font asset assigned to the text object.
    2. float spriteScale = (m_currentFontSize / m_fontAsset.fontInfo.PointSize * m_fontAsset.fontInfo.Scale);
    3. currentElementScale = m_fontAsset.fontInfo.Ascender / sprite.height * sprite.scale * spriteScale;
    This is for a TMP UGUI text object. The normal TMP object has the following
    float spriteScale = (m_currentFontSize / m_fontAsset.fontInfo.PointSize * m_fontAsset.fontInfo.Scale * (m_isOrthographic ? 1 : 0.1f));

    If you look at any given font asset in the inspector, you will see the value for that Ascender which is based on the sampling point size of the font asset. For testing / understanding this better, set the text object's point size to match the sampling point size. Then the height of that sprite should match exactly the Ascender height.

    Note that the Ascender value can be adjusted so if your font asset is using a sampling point size of 32 but the Ascender is 28, you could actually adjust the Ascender to match the 32 of your sprite's height and this would ensure 1:1.

    Adjusting the Ascender would affect the line spacing but you are unlikely to need to make a big adjust where this might not be much of an issue. You could also adjust the line height to compensate.

    To help visualize this, you can also add the "TMP_TextInfoDebugTool.cs" script included with TMP on a text object. Then toggle showing characters and lines and you will see where those Ascenders are.

    P.S. Sorry about the delayed response.
     
    Last edited: Mar 17, 2018
  3. SweetBro

    SweetBro

    Joined:
    Jan 14, 2013
    Posts:
    69
    Excellent! Just what I was looking for, thank you!
     
    Stephan_B likes this.