Search Unity

How to instantiate a text box correctly onto a panel that could either be landscape or portrait?

Discussion in 'UGUI & TextMesh Pro' started by bullettea, Jun 24, 2022.

  1. bullettea

    bullettea

    Joined:
    Oct 24, 2019
    Posts:
    29
    So, learning Unity and I'm making a small game that mostly uses text boxes.

    The game could be played on desktop or on mobile, and the resolution could either be landscape or portrait.

    I have a panel that will be wide if it's in landscape mode, and tall if it's in portrait mode.

    I want to instantiate a text prefab onto the panel, and want to have the text align itself accordingly (e.g., one long line if it's in landscape, several short lines if it's in portrait).

    What settings does this text prefab need to be able to resize itself correctly on the panel?

    Adding a content size fitter to the prefab and checking "preferred size" only seemed to make it fit in landscape mode, but this wouldn't change for vertical.

    Thanks for any help.
     
  2. bullettea

    bullettea

    Joined:
    Oct 24, 2019
    Posts:
    29
    For any n00bs like me still trying to figure it out, this did the trick:

    Code (CSharp):
    1. Vector2  panelSize= _panel.GetComponent<RectTransform>().sizeDelta;
    2.        
    3. _textExample.GetComponent<RectTransform>().sizeDelta = _panelSize;
    There might be a better way to do this, but I don't really know what that is yet as a beginner.