Tuesday, February 4, 2014

NGUI HUD without Extension

NGUI is a great tool for Unity, no doubt about it. But, it's a shame that they want 20$ for the HUD extension. Save your money, here's an easier way to get HUD Text without having to buy the extension.

First off, If you do not already have NGUI up and running, create a simple 2D UI, as we will copy some of the existing components.

Here's what you do:

  • Under UIRoot, copy the camera, and place it under your main camera (as a child)
  • Set it's position and rotation to 0-0-0 (So that it has the same as your main_camera)
    • Maybe rename it to something like HUD_Camera
  • Create a new Layer - Let's call it "HUD" (or whatever you like)
  • Set the Culling mask of the HUD_Camera to HUD (and HUD ONLY!)
  • Copy the UI_Root and put it under the object you want to have HUD text above (as a child)
  • Set it's Layer to "HUD"
  • Position it so that it hovers above the GameObject
  • Now, create your HUD-UI (Labels, Sprites, whatever you want)
That's basically it. It's now a 3D HUD. Which means if you are behind it, it's backwards, if you are above it, it's invisible. To change this, apply this Script to your UIRoot:

1:  using UnityEngine;  
2:  using System.Collections;  
4:  public class HUDFaceCamera : MonoBehaviour  
5:  {  
6:    void Update ()  
7:    {  
8:      transform.LookAt(Camera.main);  
9:      //Attention: This only works if your  
10:      //HUD_Camera is in the same position  
11:      //as your main_camera  
12:    }  
13:  }  

Or just use the LookAtTarget (or whatever it's name was) script that comes with Unity and set the target to your HUD_Camera.
That's it, have fun! As I said, 20 Bucks is a bit much, since this can be done this way (which goes probably even faster than downloading, installing and setting up the extension)
As always, thanks for reading!

No comments:

Post a Comment