Hi I’ve been looking for a script that makes a GUI follow a character for name, health, etc.
I found this script on the Unify site:
http://www.unifycommunity.com/wiki/index.php?title=ObjectLabel
Thing is that this only creates text. I want pictures and boxes.
How can I modify the script to contain different GUI functions?
I’m using the javascript one(theres two on the page.)
I think the script can also be used for GUI Texture. Give it a try ?
Yes it does work but GUITexture still isn’t really what I’m looking for.
Am I right in thinking it is only for images?
I really want it to use a GUI script I’ve written myself.
Any ideas?
The code is rough (one of the first things I wrote when I started working with Unity), but you could do something like this to make normal GUI elements follow a 3D object in the scene.
Vector3 transformPoint = CameraManager.Instance.GameCamera.WorldToScreenPoint(this.collider.bounds.center + (new Vector3(0, this.collider.bounds.extents.y, 0)));
if (transformPoint.z > 0){
float newLeft = (transformPoint.x - (_lineWidth / 2));
float newTop = (Screen.height - transformPoint.y) - _lineHeight - _displayVertOffset;
_lineRect = new Rect(newLeft, newTop, _lineRect.width, _lineRect.height);
GUI.Box(_lineRect, _oneLiner, GUI.skin.GetStyle("OneLiner"));
_lineWidth = GUI.skin.GetStyle("OneLiner").CalcSize(_lineContent).x;
_lineHeight = GUI.skin.GetStyle("OneLiner").CalcSize(_lineContent).y;
if (_oneLinerSized == false){
ResizeOneLiner();
_oneLinerSized = true;
}
}
void ResizeOneLiner()
{
_lineContent.text = _oneLiner;
_lineRect = new Rect(Input.mousePosition.x, (Screen.height - Input.mousePosition.y) - _lineHeight, _lineWidth, _lineHeight);
}
What I do is to calculate de 2D position with “camera.WorldToScreenPoint (target.position);”
And then calculate the position to modify the rect that make the GUI, modify rect.x, rect.y to place the rect GUI in the screen above the object.
If you want the code i will look for it
Try this:
var transformPosition = Camera.main.WorldToScreenPoint(transform.position);
//the camera must be tagged as MainCamera.
GUI.Box (Rect (transformPosition.x-50, -transformPosition.y+750, 100, 25), “Hello!”);
Now attach the script to an object then the GUI Box will follow the object.
Hope this helps!