- Home /
The question is answered, right answer was accepted
Is it possible to position a GameObject as relative to a GUI?
Hello again guys ^_^.
I would like to know if its possible someway to position a GameObject in the same position as a GUI (a GUI box for example)? And at the same time if it can scale/move as the GUI does?
To be more specific I'd like the Game Object to appear within the limits of the GUI Box and scale/position itself accordingly.
Keep in mind that the GUI Box that I've defined is already scaling/positioning itself according to the screen resolution/ aspect ratio.
Regards, Eugen
Answer by efge · Dec 24, 2012 at 03:29 PM
First get the rect of the GUI element and its center using GUIElement.GetScreenRect() and Rect.center.
Transform the point from screen space into world space with Camera.ScreenToWorldPoint.
Then you could calculate a scale factor for x and z using Screen.width, Rect.width and Screen.height, Rect.height.
I'm currently attempting to put together the proper code. Once I get it I will let you know if it worked. I know that it's the proper method of making it work but I just have to figure out a couple of more variables. Thanks for the answer!
How can I use the GUIElement.GetScreenRect() function if I am using a Texture2D on my GUI Box? I've made up this code: "var r : Texture2D = AvatarBackground.GetScreenRect(); Debug.Log("AvatarBackground are" + r.width + "si inaltimea de" + r.height);"
Where AvatarBackground is defined at the beginning of the script as Texture2D. I've also tried the same code but with: var r : Rect =... and still to no avail. I keep on receiving the same error: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.Texture2D.GetScreenRect'.
Answer by Bunny83 · Dec 25, 2012 at 12:45 PM
GUIElements like GUITexture and GUIText are the "old" GUI system. If you use OnGUI and the GUI / GUILayout class, you have to use GUIUtility.GUIToScreenPoint and then you could use Camera.ScreenToWorldPoint to bring the coordinates into workspace.
Follow this Question
Related Questions
How to give a relative position to a GUI Texture ? 2 Answers
GUI Button and a gameObject's position 3 Answers
gui position of Screen relative position 2 Answers