|
Post by fuzzy on Jan 17, 2016 21:06:29 GMT
Hi!
How can we attach a GUIText to a GameObject? I am trying to obtain the world coordinates of the cam point (Camera.main.WorldToViewportPoint(gameObject.transform.position)) in World class, but the GUIText instance at that position is not visible. Is there something else we must do?
Thanks.
James
|
|
|
Post by fuzzy on Jan 18, 2016 20:32:27 GMT
This is what I get working with Orthographic view, but I would need it with Perspective one. Any idea?
string sName = oTile.GetPopCenter().GetName(); Vector2 v2 = HexCoordinates.HexToWorld(pair.Key);
Vector2 v2Screen = Camera.main.WorldToScreenPoint(v2); Vector2 v2Viewport = Camera.main.WorldToViewportPoint(v2); float yOffset = 0f;
GUI.Label (new Rect(v2Screen.x, (Screen.height - (Screen.height*v2Viewport.y)-yOffset),120f,30f),sName);
|
|
|
Post by khash on Jan 19, 2016 16:25:25 GMT
I'm not really sure what exactly you are trying to accomplish.
"GUIText instance at that position is not visible" what should it do?
are you aware that old unity ui had to be updated each frame to be visible? (its redrawn each turn, if not then it dissapears)
|
|
|
Post by fuzzy on Jan 19, 2016 20:39:43 GMT
Hi, khash!
What I want is to, for example, attach a label (GUI TEXT) to a city model, instantiated at some 3d world position.
|
|
|
Post by fuzzy on Jan 19, 2016 20:40:34 GMT
As I say GUIText, it can be a Gui.Label...
|
|
|
Post by kenamis on Jan 19, 2016 21:10:49 GMT
I think what you're looking for is to set a UI object's render mode to world space, default is screen space. This changes it's position into the 3d coordinates you are familiar with. Then either attach it to your city model's prefab or have a script that modifies it's world position. Make sure to change the layer to "Actors" too otherwise it won't be visible.
|
|
|
Post by fuzzy on Jan 21, 2016 11:27:41 GMT
Hi, kenamis! Yes, that worked! Thank you very much! For those who want to create things like City Names, Unit Names, etc. above 3D Models, do as kenamis said, and here you have a Tutorial of how to instantiate 2D GUI elements in 3D World. docs.unity3d.com/Manual/HOWTO-UIWorldSpace.html
|
|
|
Post by kenamis on Jan 21, 2016 22:49:17 GMT
You're welcome. You probably want those UI objects to always face the camera too. If so you can add this script onto the UI object.
using UnityEngine; using System.Collections;
public class CameraFacingBillboard : MonoBehaviour { private Camera m_Camera;
void Awake() { m_Camera = Camera.main; }
void Update() { transform.LookAt(transform.position - m_Camera.transform.rotation * Vector3.back, m_Camera.transform.rotation * Vector3.up); } }
|
|
|
Post by khash on Jan 22, 2016 0:46:46 GMT
ah ok now I understand the question.
you can as well convert position of the object in the world to screen space and then put ui object at this position. it would let you keep pixel perfect images and unscaled /unrotated items easier. Both techniques have advantages and disadvantages.
|
|