Hi all
-
I have an anatomy demo (here) using GUIText to label anatomical points.
-
Currently the GUIText displays at all times
-
each GUIText is the child of a red sphere which is what’s delineating the anatomy
-
I’m trying to make it so that a GUIText component appears and disappears depending on whether or not the object it is a child of is visible to the camera.
-
I have only one camera in my game.
-
the script at the end of this message is the script attached to the GUIText object
So far the functions Renderer.isVisible and OnBecameVisible have not produced any results in my hands.
I’ve found another thread here but as a beginning C#er I don’t have the wider understanding to know which avenue to pursue. Any pointers greatly appreciated.
Here are some screenshots
using UnityEngine;
using System.Collections;
public class deltoid_tuberosity : MonoBehaviour {
public Transform target; // Object that this label should follow
public Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public bool clampToScreen = false; // If true, label will be visible even if object is off screen
public float clampBorderSize = 0.05f; // How much viewport space to leave at the borders when a label is being clamped
public bool useMainCamera = true; // Use the camera tagged MainCamera
public Camera cameraToUse ; // Only use this if useMainCamera is false
Camera cam ;
Transform thisTransform;
Transform camTransform;
public GUISkin mySkin; //apply GUIskin
private GUIText thisGui;
void OnGUI () {
// Assign the skin to be the one currently used.
GUI.skin = mySkin;
}
void Start ()
{
thisGui = GetComponent <GUIText>();
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = cam.transform;
}
void OnBecameVisible () {
Debug.Log ("OnBecameVisible");
enabled = true;
}
void OnBecameInvisible () {
Debug.Log ("OnBecameInvisible");
enabled = false;
}
void Update()
{
if (clampToScreen)
{
Vector3 relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize),
thisTransform.position.z);
}
else
{
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}