Search Unity

TextMesh Pro Position of text is wrong

Discussion in 'UGUI & TextMesh Pro' started by SimRuJ, Feb 10, 2021.

  1. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Hi everyone,

    I've never used TextMesh Pro before and just created my first object. In the beginning it was positioned at (0,0,0) but the text was huge, so I changed the font size to 8, the width to 10 and the height to 5.

    The position is still set to (0,0,0) but the text isn't actually in the middle of the scene but more like at (-2,1,0). Rotating the text makes it even worse.
    The anchors are still all "0.5" but the margins (right and bottom) are both set to "3.xxx" and if I set them back to 0, the yellow binding box turns really big.

    How do I position the object at (0,0,0) and give it only a small bounding box (basically just surrounding the text, without additional spacing)?
     
    Last edited: Feb 10, 2021
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    When using the <TextMeshPro> component that works with the Mesh Renderer, it is important to know how Camera settings can affect the text object. For instance, an Orthographic camera has a size property which affects the rendering size. It is important to set this size appropriately.

    For instance, if the Game View's vertical resolution is 720 then the camera size should be set to 36 which will result in a 1:1 scale. You should be able to find several posts about proper Camera scaling / sizing. For example, if you create a Cube of scale (1, 1, 1) and set the camera size properly the cube will be exactly 10 pixels where 1 unit = 10 pixels.

    When using the Canvas system in Screenspace overlay, you do not need to worry about the camera scale / size as that is handled by the Canvas where the <TextMeshProUGUI> objects will be scaled in according to the Canvas.

    <TextMeshPro> objects will be the same exact size / scale compared to <TextMeshProUGUI> when the Orthographic camera size is set properly where the rendering will be considered 1:1.

    When using the Rect Tool in the scene view as seen below

    upload_2021-2-10_13-25-41.png

    You will be able to change the size of the RectTransform by placing the cursor on the yellow lines which will display the double arrow resize widget as seen below.

    upload_2021-2-10_13-27-39.png

    When placing the cursor on the white dots, you are controlling the size of the Margins relative to the RectTransform. Think of these margins as those on a piece of paper, they allow you to control the positioning of the text within the RectTransform.

    In your case, you most likely meant to resizing the RectTransform but ended up changing the margins which will affect the relative position of the text thus resulting in the appearance of the text being incorrectly positioned.

    To reset the margins back to (0, 0, 0, 0), you can do so in the Scene View via the widget but more easily via the Margins property in the Extra Settings panel of the text component inspector as seen below.

    upload_2021-2-10_13-32-24.png

    In terms of the margin widgets behaving strangely in perspective view, that is something that will be addressed in the next release. If you need to set the margins, just do it in 2D view or via the Margins property.
     
    sisF and SimRuJ like this.
  3. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Thanks for the long explanation!
    I set the margins to 0 and then used the rect transform tool and now everything's were it should be. :)
    I'm using a regular "perspective" camera that's a child of my player object. The text object is place inside a room and acts as a sort of loading screen (no canvas). Do I still have to set the resolution in that case? I looked at the camera settings but couldn't find anything about "resolution".
     
  4. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    Is this a bug?

    Im using the canvas rendered version of textmesh and what i set in the scene view is different from the game view!
     

    Attached Files:

  5. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    As you can see the text is shifted downward in the game view
     
  6. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    So i deleted the gameobject and recreated it and that fixed it. I should mention that it was part of a layout group and i removed the layout group component from the parent before this started. i wonder if that triggered this bug.
     
  7. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    OK, so now my whole UI is broken. I'll just put this gun to my head now.
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I suspect the behavior is the result of those layout components reacting to the screen size / resolution / aspect ratio changes between GameView and SceneView. This would especially be true when using RectTransform with various anchors / stretch anchor positions.

    I suggest carefully looking at how these layout components and anchors affect the positioning of those objects relative to each other and their size as you switch between those views.

    Look more specifically at how the RectTransform of the text is affected between the Scene view and Game view.
     
  9. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    So what is the aspect of the scene view then? If what you're saying is the case, then the scene would have to be different from the game view. In my case, the Canvas' top level rect is set to 1920 x 1080. the game view is set to 1920 x 1080.
     

    Attached Files:

  10. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    I'll take your silence as: Yes, this is a bug.
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I think the issue is the result of layout components combined with the Canvas Scaler where depending on how you have the anchors of the individual objects setup are affected when the screen size or aspect ratio changes.

    Check what happens to the individual objects (more specifically) their position relative to their parent when you go from scene view to game view. Check how the anchors and pivots of these objects affect all of this.
     
    Last edited: Jul 15, 2021
  12. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    i see what's happening, the vertical layout component permanently screws the child objects if you remove it. the game view shows everything shifted left. Only way i found around this is to delete and recreate all my child objects, now they are showing in the same position on both views.
     
  13. blackjok28

    blackjok28

    Joined:
    Aug 3, 2021
    Posts:
    1
    you should try canvas rendermode = world space
     
  14. transporter_gate_studios

    transporter_gate_studios

    Joined:
    Oct 17, 2016
    Posts:
    219
    no, its a problem with layouts which I fixed by no longer using them.
     
  15. blackfritz

    blackfritz

    Joined:
    Sep 13, 2018
    Posts:
    9
    sorry for necro, this is a bug, i can confirm it:

    Steps to reproduce:
    1. make a canvas
    2. make textmeshpro inside it
    3. change TMP transform using alt shift and select the lower right
    4. position the TMP as you like
    5. now change TMP transform to original anchor and pivot (reset to default)
    6. Voila, the text stays on the same place, nothing will be able to change it until it deleted
     
    thathurtabit likes this.
  16. thathurtabit

    thathurtabit

    Joined:
    Jul 29, 2018
    Posts:
    6
  17. thathurtabit

    thathurtabit

    Joined:
    Jul 29, 2018
    Posts:
    6
    To be more specific to my TextMeshPro issue with incorrect text position; I have a Canvas set to Screen Space - Camera, and a TMP UI Panel (in light blue in the screenshot) and TMP Text inside that.

    I'm Instantiating the surrounding game object, and Instantiating multiple TMP Text panels inside the UI Panel, but although I'm setting the TMP Text panels to stretch to their parent width/height - when it is instantiated, it adds a random offset (Top: 114 - Bottom -114). I've no idea where this value comes from - it should be all 0;

    If I click on the RectTransform settings and reapply the Stretch/Stretch settings, everything returns to 0 and is visually fine.

    I've no idea how/why it is adding a 114 offset when the TMPText is instantiated.
     

    Attached Files: