NGUI:HUD Text(頭頂傷害漂浮文字)


HUD Text

很早之前就有聞於NGUI中的HUD Text插件,今天得以嘗試,看了會兒官方的文檔,楞是沒給看明白,官方的ReadMe.txt寫的使用方法如下:

官網Usage

1. Attach the HUDText script to a game object underneath your UIRoot and set the font it should use.
2. To make it follow an object drawn with another camera, attach UIFollowTarget to the same object and set its target.
3. From code, use HUDText's Add() function to add new floating text entries.

使用方法

看過它的Example之后自己動手寫了個簡單的demo

操作步驟

1、新建Scene,創建一個Cube,添加腳本UseHUD.cs

2、建立NGUI,把NGUI放在2DUI layer

3、在NGUI的Camera上建一個空的GameObject,綁定上腳本HUDRoot.cs

4、打開UseHUD.cs,代碼如下:

示例代碼

using UnityEngine;
using System.Collections;

/// <summary>
/// 類名描述:如何使用HUD Text
/// 作用:把這個腳本綁定在需要使用HUD的GameObject上面,
/// 日期:2013-09-16
/// 版本:v0.1
/// 創建者:趙青青
/// 修改者:趙青青
/// </summary>

[AddComponentMenu("GameName/UseHUDExample")]
public class UseHUD : 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;
            mpos.y += 2;
            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.GetMouseButton (0)) {
            m_hudText.Add ("+100", Color.red, 0);
        }
        if (Input.GetMouseButton (1)) {
            m_hudText.Add ("-30", Color.green, 0);
        }
        if (Input.GetMouseButton (2)) {
            m_hudText.Add ("漂亮!", Color.cyan, 0);    
        }
    }

    void OnClick ()
    {
        if (m_hudText != null) {
            m_hudText.Add ("HUD TEXT", Color.red, 1.0f);
        }
    }
}

飄字效果

點擊運行,點擊屏幕上的cube,看到cube頭頂飄出HUD 字體,Good!

 image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM