0
\$\begingroup\$

I have the following code that depends on the resolution of the user's display:

void OnGUI()
{
    GUI.Label(new Rect(432, 525, 9000, 9000), ("Hello!"));
}

And of course, the location of the text changes in devices with different resolutions. How can I make the text independent of screen resolution in Unity?

\$\endgroup\$

closed as off-topic by Josh Jan 1 '16 at 19:04

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ I'm voting to close this question as off-topic because it is an intra-network cross post. \$\endgroup\$ – Josh Jan 1 '16 at 19:04
1
\$\begingroup\$

You can use a canvas scaler and put UI text on it.

http://docs.unity3d.com/Manual/script-CanvasScaler.html

http://docs.unity3d.com/ScriptReference/UI.Text.html

GUI.Label on the game screen is only for quick debug hacks as far as I know.

\$\endgroup\$
0
\$\begingroup\$

I have a work around for this problem. Idea is to assign position in percentage instead of hard coded coordinates.

1- First find out your base resolution, on which further work will base on. Print Screen.width and Screen.height. In my case it is 428 x 321. 2- Find out the percentage of current position with resolution. If I consider GUI.Label(new Rect(50, 50, 300, 300), ("Hello!"));, then it'd be 50.0f/428.0f width percentage and 50.0f/321.0f height percentage. 3- Now assign position w.r.t. these percentages. Like, GUI.Label(new Rect((50.0f/428.0f) * Screen.width, (50.0f/321.0f) * Screen.height, 300, 300), ("Hello!"));

\$\endgroup\$
0
\$\begingroup\$

Instead of using a gui label, you could simply add a text object to your project hierarchy (Create->UI->Text). This will automatically add a canvas object as well. Changing the UI scale mode on your canvas scaler to "Scale with Screen Size" will make the UI scale based on the screen resolution. You would then set the anchors/pivot of your text object depending on exactly how you want it to behave.

I recommend watching this series of videos by 3DBuzz on youtube. It goes in depth about how to set up UIs in Unity.

https://www.youtube.com/playlist?list=PLt_Y3Hw1v3QTEbh8fQV1DUOUIh9nF0k6c

\$\endgroup\$