- Home /
Align TextMesh on Top Right
Hi,
I want know (i search a lot and tried a lot of codes) how i can align a TextMesh (or my extension that im using for it EasyFontTextMesh) on Top Right position of screen.
Like this: https://dl.dropboxusercontent.com/u/7386482/top-right.png
Im using ortographic camera for a 2D game. Im porting my game with cocos2d-x (https://itunes.apple.com/br/app/flyforcoins/id870686218?mt=8) to use Unity3D. Im near to finish, this is a problem that i cant solve on this week :(
Thanks for any help :)
Answer by robertbu · Aug 27, 2014 at 05:53 PM
Set the anchor for the text to upper right. Then execute this code:
#pragma strict
function Start() {
transform.position = Camera.main.ViewportToWorldPoint(Vector3(1,1,10));
}
Consider using GUIText. You can set the anchor to upper right and the position to 1,1, and no code is involved unless you need to change font size.
To reiterate: the ANCHOR option.
As far as I can tell, the Alignment dropdown does nothing.
Also, just for fun, exact placement with Anchor depends on the font. I have one commercial font where middle acts like upper and lower acts like middle (and upper is "add an entire blank line on top."
@OwenReynolds - would he be better off using Renderer.bounds to position the text rather than setting the anchor and position? Renderer.bounds for Text$$anonymous$$esh is not precise, but it would suffer from the problems you outline.
Hi Robertbu,
Your answer is near to be correct, the problem is when i use your code, the score text is out of screen:
SS: https://www.dropbox.com/s/tnq8w9qp1mosmqg/Screenshot%202014-08-27%2015.46.23.png?dl=0
It can be 20px distant from border top and right too?
private void UpdateScoreText()
{
scoreText.Text = GlobalObjects.score.ToString();
scoreText.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(1,1,10));
}
Hi, i solve the problem with:
scoreText.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0.99f,0.94f,10));
Thanks robertbu.