c# - Local and World Coordinates of GUI elements on Game Object -


I have a game object that is built on the screen in Unity 2D mode.

is the co-ordination of the game object (3, 3) . In the script related to the game object , I have the following code: OnGUI () Method:

  zero onGUI () {if (GUI button (new vacancy (Renderrkbound.amm.aks, Renderrkbound.am.am.a., Renderrkboundkmaks.aks, Renderrkboundkmaks.a.), "")) {Dibgklog ( "My Status: "+ This.transform.position.x +", "+ this.transform.position.y); }}  

But when I run Unity project, then GUI.Button is created on the top left corner of the screen, absolutely GameObject . What is the reason for local and world co-ordination?

How can I create a GUI.Button () code on the game code?

Update: After reading the 0 @Andrea reply, I updated the code. The problem persists:

  Personal vector 3 worldpost; Zero start () {worldPos = Camera.main.WorldToViewportPoint (gameObject.transform.position); } Zero ongi () (if (GUI button (new ricket (WorldPos. X, WorldPos. 1, 1, 1), "") {debug log ("My position:" + this.transform.position.x + "," + This.transform.position.y);}}  

GUI.Button still in place in the top left corner of the screen, instead of a game object What do I remember?

Unity always work in GUI elements in the screen space. Here's a description of this: Under Status : All Units The controls work in the screen space, which is the resolution of the player published in pixels. Therefore it does not matter where your game object is presented, the view in the GUI presented is independent of that, besides you There is no need to create a game object in "2D mode".

As described by 0 Andrea, you can see the coordinates of the game object during the screen Industry by

  Vector 3 Skrinpoj = camera. Word Tos Skrinpoint (target.position);  

(instead of WorldToViewportPoint )

Unity 4.6 will be released with an additional new GUI system called UUII. With this system you will be able to create GUI elements in world space. . If you are in urgent need of this type of system then there are NONESIUI or NGOs that you can buy in the property shop.


Comments