我们在游戏中常常会遇到这样的情况,当我们对敌人造成伤害的时候,会在敌人的位置弹出伤害的数字,如下图所示:
实现这样的效果,是使用NGUI_Hud_Text.unitypackage包里面的HUDText预制体来实现的
我们一般先给小狼的对象中添加一个空的子对象,假设名字叫做HUDTextShow,随后HUDText预制体要加上UI Follow Target脚本组件,他的作用是找到要显示的位置(小狼里面的空的子对象)。这是通过脚本来进行控制的。
private GameObject hudtextFollow; //小狼身上的空的对象
private GameObject hudtextGo;
public GameObject hudtextPrefab;
private HUDText hudtext;
private UIFollowTarget followTarget;
void Awake() {
hudtextFollow = transform.Find("HUDTextShow").gameObject;
}
void Start() {
hudtextGo= NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab);
hudtext = hudtextGo.GetComponent<HUDText>();
followTarget = hudtextGo.GetComponent<UIFollowTarget>();
followTarget.target = hudtextFollow.transform; //找到要显示的位置
followTarget.gameCamera = Camera.main;
}
显示的信息如何添加呢?
hudtext.Add("显示的信息", Color.gray, 1); //显示的信息,颜色,显示时间