- Home /
GUI Problem
Hello, I am creating a main menu in Unity. I have one problem. Every time I add a text it moves around when I enlarge the screen. So I need GUI. Other problem, I have no idea how to. So if you could please just direct me to a good place to learn GUI in either JavaScript or C# would be great. All I really need though is to make it so my text don't move around when the screen size changes. Thanks.
Update:
Before full screen:
Full Screen:
I want to make it so that no matter if it's full screen or any other size screen the text looks like it does in the full screen mode. As you can see it doesn't right now.
"text don't move".. relative to WHAT? You want it to always stay in the center?
$$anonymous$$y problem is, when I put a text in say the upright corner in a small frame size... Then I move it to the max size of my computer it's not in the same relative position.
Answer by moghes · Feb 13, 2013 at 09:21 AM
For this type of problem you must use Screen.width and Screen.height First better to set the transform position x,y,z to 0 , and position the guiText with pixelOffset.
create a javascript file (or C#) and call guiController.js
public var myGUIText:GUIText;
private var myStyle:GUIStyle; //
private var sizeOfLabel:Vector2; // since you don't know size of your text
function Start()
{
sizeOfLabel = myStyle.CalcSize(new GUIContent(labelTextHere));
myGUIText.pixelOffset.x = Screen.width/2 - sizeOfLabel.x/2; // Center to Screen
myGUIText.pixelOffset.y = Screen.height/2;
}
to use this script, you can simply create an empty game object, and name it gui_ControllerObject, attach this script to the this object, now drag your GUIText to the public variable myGUIText.
Try this and leave comments for questions since I wrote it directly here..
Could you please just walk me through the script step by step so I can actually understand it. I'd like to learn a little ;)
create an empty game object, name it guiControllerObject or anything you wish to.. create a script(javascript) and name it guiControllerScript. And attach the script to the object Open the script and paste this code.
If you don't know the difference between public and private variables, see this
I have an another variable "myStyle" of type GUIStyle for calculating the size of the text.. click here to read more about GUIStyle
and the third variable is to store the size, which will be needed in the calculation of the position of the guiText.
The implementation of the function seems clear, ask if you didn't get anything, just ask again. And if the answer was helpful and correct , just tick as true.
Answer by Benproductions1 · Feb 13, 2013 at 08:58 AM
Reading the Unity Docs really helps. Most of the questions here can get answered by a simple link to them: Just like this one
Your answer
Follow this Question
Related Questions
GUI text blinking opacity 1 Answer
MonoDevelop 4.0.1 code completion doesn't work 2 Answers
What Am I Doing Wrong? Variable Names 3 Answers
Create GUIText from Javascript 3 Answers