- Home /
Why is setting the parent of a text transform alters its world coordinates?
Hi eveyone,
I seem to have found something strange with the transform.parent function... it is seeming to contradict what it says in the docs "modify the parent-relative position, scale and rotation but keep the world space position, rotation and scale the same.". Usually this is the case... but in this particular case this does not occur
http://docs.unity3d.com/ScriptReference/Transform-parent.html
My Goal
I'm currently making a function to generate a text for GUI purposes the function is called DrawText and returns a TextMesh object. the aim of this is so whenever i need to add text in game on a GUIBox or something i can call this function and place it where i need to in ScreenSpace... becuase for the most part this will be done within a Custom GUI Object thus the text should be parented to the GUI Object. the Draw Text function is as below
public TextMesh DrawText(Rect rect, string text)
{
GameObject textGo = new GameObject();
textGo.name = "text";
textGo.layer = LayerMask.NameToLayer("UI");
Debug.Log(viewport);
float x = rect.x + this.rect.x;
float y = Screen.height - (rect.y + this.rect.y);
textGo.transform.position = (viewport.camera.ScreenToWorldPoint(new Vector3(x, y, 0.99f)));
textGo.transform.parent = gameObject.transform;
this.text = textGo.AddComponent("TextMesh") as TextMesh;
this.text.font = Resources.Load("GUI/Font/MainFont") as Font;
this.text.richText = true;
this.text.renderer.material = this.text.font.material;
this.text.characterSize = 0.1f;
this.text.fontSize = 24;
this.text.text = text;
return this.text;
}
view port is the camera object I use to render GUI on a separate layer.. an inbuilt layer called UI. it is orthographic with a size of 5 usually at the worlds origin (0,0,0).
rect refers to where the text will be placed on the GUI object this.rect is the pixel coordinates of the GUIObject not the text itself
The Problem
now the problem with this code is in this where i set the parent of the text object to the GUIobject transform... when that line is called the to object is shifted to a very strange position... it seems to start ignoring the fact that i moved the object with the gameObject resulting it moving it out of the screen ... its local coordinates seem equal to the values of the parent + itself.... e.g(Box would read -8.8x 5y in world space , text would read -8.68x 4.88y in local space)
My attempt on fixing it
so incidentally i tried removing this.rect
from the equation. this did not solve thew issue ... it didn't change a thing.... I have also tried adding the transform to the text since with thistextGo.transform.position = (viewport.camera.ScreenToWorldPoint(new Vector3(x, y, 0.99f))) + transform.position;
that just caused it to completely shift out of line. with or without considering this.rect
.
now this is where it just gets weird.... by removing the transform.parent code... everything will line up perfectly... also this seems to only ever ever happen when the transform.parent code is called from within or anything that originates from Start()... infact if a i call the transform.parent code in the very next frame... it will work...perfectly it only seems to affect it within start(). in the screen shots provided you noticed HAHAHA is only affected... as it is called within start()
here is the code where DrawText is called
public class GUITitleInterface : GUIInterface {
GUI3DWindow win;
WindowTitleCommand select;
TextMesh text;
// Use this for initialization
public override void Start()
{
win = GUI3DWindow.New(new Rect(0, 0, Screen.width - 20, 100));
select = WindowTitleCommand.New(new Rect(400, 300, 200, 200));
ResourceManager.PlayLoopingClipAtPoint(Resources.Load("Audio/BGM/Prelude") as AudioClip, Vector3.zero);
win.DrawText(new Rect(10, 10, 0, 0), "HAHAHAHA!");
}
now i know i could just call the code elsewhere without any major performance issues but what what i want to know is why?
Your answer
Follow this Question
Related Questions
Making text look clear 2 Answers
Changing an objects transform position 1 Answer
Display Name Above Object 2 Answers
I want pixelated unicode text. Need suggestions... 2 Answers
Detect Text in GUI; Print 1 Answer