Unity Scale One Text Field GUI

Hey Unitarians,

I honestly am disappointed on how hard Unity has made it to do this seemingly simple task.

What I want is a single text field, centered in the exact middle of the screen, with its proportions the same for every android screen resolution, and the text within it centered and the font size changes to accommodate the screen size. This sounds really simple, but 2 hours later I can’t do anything. I really hope Unity fixes their GUI soon.

But before that, does anyone know how to do the above?

Aman Jha

If you need only one text box, use the GUIText component. Set in its parameters alignment on the middle and set transform values (0.5, 0.5, 0). Sets pixelOffset is equal 0 for two value. Now the text will be on the screen middle, but unfortunately Unity isn’t able to scale elements under different screen resolutions. Therefore it is necessary to write a small script and to attach it, for example, to MainCamera (write on CSharp):

 //Base value of resolution screen for which the text is made
 public float origW = 1200.0f;
 public float origH = 720.0f;

 void Start() {
  float scaleX = (float)(Screen.width) / origW; //your scale x
  float scaleY = (float)(Screen.height) / origH; //your scale y
  //Find all GUIText object on your scene
  GUIText[] texts =  FindObjectsOfType(GUIText) as GUIText[]; 
  foreach(GUIText myText in texts) { //find your element of text
   Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
   int origSizeText = myText.fontSize;
   myText.pixelOffset = new Vector2(pixOff.x*scaleX, pixOff.y*scaleY); //new position
   myText.fontSize = origSizeText * scaleX; //new size font

I hope it will help you.

I figured it out by fiddling with NGUI. I found that I had a free pack of the full version in my unity packages so I imported it and worked with it.