1、新建Scene,建立一个游戏物体,建立一个TestHUD.cs
2、建立NGUI,把NGUI放在2DUI layer
3、在NGUI的Camera上建一个空的GameObject,绑定上脚本HUDRoot.cs
using UnityEngine;
using System.Collections;
public class TestHUD : MonoBehaviour
{
public Transform m_target; // HUD字体出现的位置
public GameObject m_hudTextPrefab; // HUD字体 prefab,不可为空
HUDText m_hudText = null; // HUD字体
void Start()
{
if (HUDRoot.go == null)
{
GameObject.Destroy(this);
return;
}
if (m_target == null)
{
m_target = this.transform;
Vector3 mpos = this.transform.position;
m_target.position = mpos;
}
//添加hud text到HUDRoot结点下
GameObject child = NGUITools.AddChild(HUDRoot.go, m_hudTextPrefab);
//获取HUDText
m_hudText = child.GetComponent<HUDText>();
//添加UIFollow脚本
child.AddComponent<UIFollowTarget>().target = m_target;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
m_hudText.Add("+10", Color.red, 0);
}
if (Input.GetMouseButton(1))
{
m_hudText.Add("-30", Color.green, 0);
}
}
}
运行之后,按压鼠标左键,右键将看到效果!