NGUI:HUD Text(头顶伤害漂浮文字)

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3325351.html



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

作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(8355)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
CONTENTS
点击右上角即可分享
微信分享提示