Search Unity

Panel & Text: Text not staying inside panel

Discussion in 'Scripting' started by mholmes, Oct 23, 2018.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Not sure what I'm doing wrong but I'm building a panel and I'm adding text but when I run it in game mode, the text is not aligned the way it looked in the editor view. Its a child of the Panel. Shouldn't stay with in the bounds of the parent automatically?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Nothing inherently stops a child from extending outside the bounds of its parent. If you want to stop drawing the portion of a child that extends outside the bounds of the parent, you can add a Mask to the parent. (Or RectMask2D, which is more efficient if you just want a simple rectangular mask.)

    A Text component may or may not constrain the text to fall within the bounds of the component's object, depending on the component's settings. IIRC, classic Unity Text by default will cut off text, but TextMeshPro Text by default will allow the text to overflow. You can configure either of them to treat oversize text in several different ways.

    If you're looking at the Game View and the Scene View side-by-side in the Unity editor and they are showing different things at the same time, that suggests that the viewpoint in the scene view isn't aligned with the camera in your scene being used to render the game view. (Or possibly a bug in Unity? I once had an issue where it seemed like text was positioned incorrectly in the Game View when not in play mode on one specific screen of a game.)

    But if, instead, the text moves when you hit "play", that might indicate that your objects are resizing or repositioning automatically at run time. If the size or aspect ratio of the game window changes (e.g. if you use "maximize on play"), then this could happen as a result of the way your anchors are set up, or as a result of various UI scaling/layout components such as a Canvas Scaler, an Aspect Ratio Fitter, or various kinds of Layout Group. (Or, of course, any custom scripts you've written that alter the size or position of any objects in the relevant hierarchy.)
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    thank you so much for your feedback. I dig some digging and I think I found the issue. I needed to change how my screen scales. In Canvas Scalar -> UI Scale Mode ->Scale with Screen size

    That will scale your screen properly or at least worked for me.

    upload_2018-10-24_9-23-49.png