- Home /
How to set my text to real object
Hello I want to set my text in GUI layer to a position of an object but can't do that work with some thing like this code because my screen size maybe change and this code hang when screen size is change but my X variable is always in true place and my main problem is Y
var TextStyle = new GUIStyle();
TextStyle.font = font;
TextStyle.normal.textColor = Color.white;
var position = Camera.main.WorldToScreenPoint(timer.transform.position);
GUI.Label(new Rect( position.x,Screen.currentResolution.height - position.y - 450, 100, 20), "123" , TextStyle);
I want to put my text in this place :
thanks
Because I don't know how to change 3d text with script and I can't set that 3d Text GUI with best quality.
If you show me the true way I'll use that.
From other side my game is 2d not 3d!!
Answer by Tomer-Barkan · Nov 28, 2013 at 01:46 PM
If you want it in a constant world position and not screen position, why not use 3d text instead of GUI text?
Edit:
Don't worry about your game being 2d. I always use 3d text for 2d games, as long as it's facing the camera it will appear 2d for you, and if your game is 2d then I don't expect the camera to rotate around it...
1 - Create a 3d text object, select the font and color from the inspector (or code if you prefer).
2 - Set the scale of the 3d text object to 0.1,0.1,0.1 so that it appears more crisp.
3 - Change the font size parameter of the object if you want to make the writing bigger. It will remain sharp.
4 - To change the text from code, create a reference to it in your script as TextMesh (set the reference in the inspector), then set the text
property to whatever you want it to display. See example below.
5 - If you don't want to set the TextMesh in the inspector, you can use GameObject.Find() to find the object by name, and then use GetComponent to get the TextMesh component of the object.
var textMesh : TextMesh;
function Start() {
textMesh.text = "123";
}
Answer by karljj1 · Nov 28, 2013 at 01:57 PM
Try this:
http://www.41post.com/3816/programming/unity-scaling-the-gui-based-on-the-screen-resolution
thanks dear, my problem is position not scale and I can't control my Y axis position.
Well your taking 450 regardless of the actual screen height. You want to normalise the value. For example if the value 450 works when the screen height is 1080 then do this
float val = ( 450f / 1080f ) * Screen.height;
GUI.Label(new Rect( position.x,Screen.currentResolution.height - position.y - val , 100, 20), "123" , TextStyle);