- Home /
GUIText position changes with changing resolution
I have a GUIText element that I'm positioning on top of a graphic. The position looks fine at first, but I soon realize it's only staying in the correct place for a single resolution. It's not necessarily a shock that elements shift against the background when the resolution changes, I'm just not sure what the fix for this is. Searches for the problem find a variety of issues that are basically unrelated to the problem I'm having.
I set it up by putting a GUIText element on top of a graphic in the scene and made sure it lines up visually. The coordinates for the GUIText are X: .165 Y: -1.11344 Z: 0. The GUIText is a child of the graphic to which I am aligning it.
Is there an easy way to do like an offset for this GUIText element off of the parent element? The strange thing to me is that I have another image behind the GUIText's parent image and the parent and this other, further back image, stay perfectly aligned with eachother at any resolution.
Appreciate the help.
Answer by robertbu · Aug 06, 2014 at 05:01 AM
GUIText lives in Viewport space while the parent object (apparently) lives in world space. It might be possible to make a parent/child relationship work, but I'd approach the problem differently:
Break the child relationship so you have two, top-level game objects.
Place an empty game object at the position you want to anchor the text relative to the object that was the parent.
Make the empty game object a child of the previous parent object.
Make sure the anchor and alignment is set correctly so that when the anchor is at the empty child's position, you get the layout you want.
Make sure the pixel offset is set to (0,0).
Put the following script on the GUIText
Select the GUITExt in the Hierarchy view, and drag and drop the empty game you place for position on the 'toFollow' variable.
pragma strict
public var toFollow : Transform;
function Update() { transform.position = Camera.main.WorldToViewportPoint(toFollow.position); }
If your objects move around, you may have to change Update() to LateUpdate() to get smooth movement.
@robertbu I see, so essentially get an element that will stay aligned with my world-space object and keep the position of my GUIText aligned to that object.
You seem pretty comfortable with the limitations of text in Unity so let me just also quickly inquire. Regarding GUIText vs creating user interface elements with UnityGUI for displaying text, do you have a preference?
Interesting question, but I cannot give you an in-depth answer. I use EZGUI for my UI work, so all elements live in World space. A couple of things come to $$anonymous$$d. It is a bit harder to get GUI elements to follow world positions and to handle changes in resolution. You can rotate GUI elements, but you cannot rotate GUIText. Post it as a new question and someone with more experience in the two can probably give you more points of comparison.
Answer by UprightToast · Dec 28, 2015 at 02:13 AM
Honestly you guys get so technical??
Any new comers?
Try this video it helped me and it will help you and it is way more easy: https://www.youtube.com/watch?v=_9mOh7_xX2o&feature=iv&src_vid=hzvQnYkS9O4&annotation_id=annotation_3467516253
:D CHEERS
Your answer
Follow this Question
Related Questions
Changing the font size of GUIText (on Unity iPhone)? 1 Answer
GUI Text line break Alt+Enter doesn't work 1 Answer
How do i make a text box appear on screen after a chest has been opened? 3 Answers
How to parent a GUI text to current game object 3 Answers
In game message displayed on GUI - text and texture 1 Answer