using UnityEngine;
public class FloatingText : MonoBehaviour
{
private UILabel _lbl;
private Vector3 _pos;
public GameObject _target;
public Camera worldCamera;
public Camera guiCamera;
void Awake()
{
_t = transform;//当前对象
_lbl = GetComponent<UILabel>();//获取label
if (_lbl == null)
{
Debug.LogError("Could not find the UILabel for the floating text.");
}
}
public void LateUpdate()
{
//follow the gameobject around on the screen
worldCamera = NGUITools.FindCameraForLayer(_target.layer);//获取MainCamera
guiCamera = NGUITools.FindCameraForLayer(gameObject.layer);//获取UICamera
_pos = worldCamera.WorldToViewportPoint(_target.transform.position);
_pos = guiCamera.ViewportToWorldPoint(_pos);
_pos.z = 0f;
_t.position = _pos;//设置label的坐标
} }