NGUI:HUD Text

本文介绍了Unity中NGUI的HUDText插件使用方法。通过一个实战案例详细讲解了如何设置HUDText及其跟随目标,包括创建场景、添加脚本、响应鼠标事件等步骤。

NGUI: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;


[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,就能出现HUD 字体了,漂亮!

 image

-------------------------------------------------- NGUI: HUD Text Copyright 漏 2012-2014 Tasharen Entertainment Version 1.11 http://www.tasharen.com/forum/index.php?topic=997.0 -------------------------------------------------- Thank you for buying NGUI HUD Text! This version of HUDText has been tested with NGUI 3.4.9 If you have NGUI 2.7.0 or earlier, delete the Examples and Scripts folders, then import the contents of the hudtext_ngui270.unitypackage instead. ---------------------------------------------- !! IMPORTANT NOTE !! ---------------------------------------------- Upon importing this package into a brand-new project, you will get compile errors unless that project already has NGUI present! You'll need to import NGUI as well. If you don't have NGUI, but still want to use HUDText, then I am guessing you didn't read the package's description! But... you can still use HUDText. Get in touch with me via support@tasharen.com and I will hook you up. ---------------------------------------------- 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. You can also tweak the splines on the HUDText script, changing the motion of the text as you see fit. Video: http://www.youtube.com/watch?v=diql3UP1KQM ---------------------------------------------- Example Usage: ---------------------------------------------- HUDText hudText = GetComponent<HUDText>(); // This will show damage of 123 in red, and the message will immediately start moving. hudText.Add(-123f, Color.red, 0f); // This will show "Hello World!" and make it stay on the screen for 1 second before moving hudText.Add("Hello World!", Color.white, 1f); // If you don't want your numeric damage values to be added up, pass them as a string instead: float myDamage = 123f; hudText.Add(myDamage.ToString(), Color.red, 1f); ---------------------------------------------- If you have any questions, suggestions, comments or feature requests, please drop by the forums, found here: http://www.tasharen.com/forum/index.php?topic=997.0
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值