- Home /
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?
Thanks, Aman Jha
Answer by aman_jha · Aug 02, 2014 at 10:22 PM
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.
Answer by zharik86 · Jul 29, 2014 at 06:24 AM
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.
$$anonymous$$aybe I didn't specify it in my question. It isn't a GUIText that I need fixed. I can use those, I'm confused on using the text fields that unity makes me write in the OnGUI thing. Where the user can type in stuff.
@Yoman For function OnGUI() and text into it use GUI.$$anonymous$$atrix for scaling font size and another elements.
Your answer
Follow this Question
Related Questions
C++ windows DLL and Android? 1 Answer
A node in a childnode? 1 Answer
GUI how to make the scrollbar bigger? 1 Answer
Android Textfield locks out input until esc 2 Answers
Create a Button Scrollview 0 Answers