Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

TextMesh Pro Scaling inline sprites using Rich Text tags causes weird alignment

Discussion in 'UGUI & TextMesh Pro' started by CDGKen, Jul 26, 2019.

  1. CDGKen

    CDGKen

    Joined:
    Jun 28, 2019
    Posts:
    30
    Hello,

    I'm experiencing an issue when using rich text tags to resize in-line sprites in TextMeshPro. The image I'm using right now is too small when placed next to the text, and I was hoping I could increase its size using the <size> rich text tag. However, doing that results in some unexpected behaviour. Here is an image of what I'm experiencing:

    tmp_sizes.jpg

    In the original the image is too small, so I use the following code:

    <size=200%><sprite="x1_glyphs_texture" name="X1_ButtonA_Glyph"></size> CONTINUE

    The text is vertically centre-aligned. The alignment seems to work for the sprite, but anything proceeding it seems to lose its alignment .

    I understand I can modify Sprite Glyph Table and change the Scale Factor so that the sprite doesn't appear as small next to the text, but doing that changes the sprite's size for all text. If I only wanted to change the sprite size in this particular instance, I am unable to.

    Is there perhaps, a third way to do this, or am I using the rich text tags incorrectly?

    I'm on Unity 2019.1.9 and using TextMeshPro 2.0.1.

    Many thanks for any help on this,

    Kenny
     
    naviln likes this.
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Just trying to make sure I understand the issue correctly.

    Is the issue the vertical alignment of the sprite as it gets larger?
     
  3. CDGKen

    CDGKen

    Joined:
    Jun 28, 2019
    Posts:
    30
    I believe the issue is the vertical alignment of the text as the sprite gets larger, but I can't be sure because I'm not certain what's going on.
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The behavior you have observed is the result of how characters (more specifically glyphs) are positioned relative to the baseline. This is how text layout works.

    For example: In the image below, notice how the "C" at size=200% is still correctly resting on the baseline (the blue line indicated by the red arrow).

    upload_2019-7-27_13-55-30.png

    Now replacing this C by a shape (Circle \uF111 from FontAwesome), notice how the circle appears correctly aligned relative to the same size letter "O".

    upload_2019-7-27_13-57-36.png

    By contrast, when changing the size of this circle to 200%, it remains at the same position relative to the baseline of the text but no longer appears vertically aligned with the letter "O".

    upload_2019-7-27_13-59-46.png

    Since Characters and Sprites are Text Elements, they follow the same text layout rules and are positioned relative to the baseline.

    Hopefully, the above helps you understand the result you got in your example.

    Having said that, I can certainly see a use case for providing control over the vertical alignment of sprites where by default they would continue to be positioned relative to the baseline but could also be positioned relative to some other text metric like the "Mean Line", "Cap Line", "Mid Line" or "Cap Mid Line".

    P.S. Mid Line would be the center of the lower case "o" or "x". Cap Mid Line would be the center of the upper case "O" or "X". These two metrics do not currently exist in text layout but could easily be added and adjusted in the Face Info of Font Assets.

    The idea would be to add an enum field to Character Sprites where the vertical alignment could be set to those alternative text metrics.
     
    Last edited: Jul 27, 2019
    Whatever560 likes this.
  5. CDGKen

    CDGKen

    Joined:
    Jun 28, 2019
    Posts:
    30
    Thanks for the response and the clarification. I think deep down I knew what was happening, but was holding out hope I was doing something wrong.

    I guess I'll have to find some other way, or settle with tweaking the glyph table. I'm not complaining, I know first-hand how complex text rendering can be, and in many ways TextMeshPro is a godsend.

    Thanks again.
     
  6. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    361
    Did you get any of this ideas implemented ? I'm sure sprite/text is a common usecase for all as it is for us ? For now we're using <valign> tags but it gets ugly ! So indeed what you suggest is interesting
     
  7. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    361
  8. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    361