http://www.unitymanual.com/thread-14635-1-1.html
用NGUI 显示游戏物体的名字,当然也可以显示物体的血条 状态信息 最终效果 第一步 首先找到场景中的camera,设置一下 Clear Flags 的属性为Depth only culling Mask的属性为Everything 标签设置为MainCamera 第2步 制作一个预制物 在场景内创建一个空物体,在空物体上创建一个NGUI的panel, panel下创建Label ,这个Label就是显示名字的, 注意把3物体的位置都设成0, 然后把它制作成一个预制物,取名NameTiao, 保持到这个下路径 Assets/prefab/NameTiao.prefab 第三步 用NGUI ->create -> 2Dui ,创建一个NGUI 的摄像机在场景中 效果如图 同样设置一下 NGUi camera,设置一下[size=14.399999618530273px]Clear Flags 的属性为 Depth only culling Mask的属性为Everything UICamera(Script)中 Event type的属性为word Event Mask的属性为Everything 把Camera设置一个标签为:NamGua 方便我们找到他 说明:我们所有游戏物体的名字都挂在Camera下 第四步 显示名字的脚本 XianShiName.cs把此脚本拖到要显示名字的物体上 ,通过 public string name; 改名就可以了
using UnityEngine;
using UnityEditor; using System.Collections; using System.Collections.Generic; public class XianShiName : MonoBehaviour { public string name; GameObject NameTiao;//获取. Camera MainCamera;//获取主摄像机. UILabel Label;//名字. GameObject NameGua; void Awake () { if(name == "") name = "未命名"; NameTiao = (GameObject)AssetDatabase.LoadAssetAtPath("Assets/prefab/NameTiao.prefab",typeof(GameObject)); NameTiao =(GameObject) Instantiate (NameTiao,NameTiao.transform.position, NameTiao.transform.rotation); MainCamera = GameObject.FindWithTag("MainCamera").camera; Label = NameTiao.transform.FindChild("Panel/Label").gameObject.GetComponent<UILabel>(); NameGua= GameObject.FindWithTag("NameGua"); aToB(NameGua,NameTiao); } //A变成B的子物体. void aToB(GameObject fu,GameObject child) { child.transform.parent = fu.transform; } Vector3 pos; void Update() { Label.text = name; // if(是人物) // { // pos = MainCamera.WorldToScreenPoint(人物头顶的坐标); // } pos = MainCamera.WorldToScreenPoint(transform.position); pos.z = 0f; //z一定要为0. //2. 使用UI摄像机转换到NGUI的世界坐标. Vector3 pos2 = NameGua.camera.ScreenToWorldPoint(pos); //3. 赋值给NGUI控件. //temp为NGUI控件. NameTiao.transform.position = pos2; } } |