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的坐标
} }
本文介绍了一个Unity脚本——FloatingText,该脚本用于实现游戏中UI文字跟随目标物体移动的效果。通过使用NGUI工具包,它能够在屏幕上的正确位置显示文字,并确保其始终相对于目标位置准确显示。
5101

被折叠的 条评论
为什么被折叠?



