Author Topic: NGUI: HUD Text  (Read 164010 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #75 on: February 14, 2013, 09:33:19 PM »
2.3.3b has the fix.

Enzign

  • Guest
Re: NGUI: HUD Text
« Reply #76 on: February 18, 2013, 10:43:47 AM »
I'm getting the following error: "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."
This happens when i call the HUDText.Add function, and the error comes from this line:

t.parent = parent.transform;

in the function "static public GameObject AddChild (GameObject parent)" in NGUITools.cs.

Here is my code for handling this. (the dmgTextPrefab is a prefab that is added to my script in the editor):

NGUITools.AddChild(GameObject.Find("UI Root (2D)"), dmgTextPrefab);
dmgText = dmgTextPrefab.GetComponent<HUDText>();
dmgText.Add(-dmg, Color.red, 0.5f);

When i Instantiate the prefab (i end up with double prefabs in the scene) and use the instantiated prefab instead it works fine. Any idea what is causing this?


EDIT: Seems like the AddChild function is instantiating the gameobject as well, so no need for me to do it. All good.
« Last Edit: February 18, 2013, 10:54:58 AM by Enzign »

Grondhammar

  • Guest
Re: NGUI: HUD Text
« Reply #77 on: February 18, 2013, 03:45:36 PM »
I just got HUDText today after using NGUI for a while -- very helpful, thanks for making both.

I am having a problem, though, getting HUDText to play nicely with dynamically created objects. It works fine when I use it statically with objects created in the Inspector/Design screen, but not when HUDText is being added dynamically. Here's the failing code (C#):

  1. labelBase = new GameObject("labelbase" + name);
  2. labelBase.AddComponent(typeof(HUDText));
  3. HUDText ht = labelBase.GetComponent("HUDText") as HUDText;
  4. ht.Add("Hi There",Color.white,4.0f);
  5.  

When I run this block, Unity reports:

  1. NullReferenceException: Object reference not set to an instance of an object
  2. HUDText.Update () (at Assets/Libraries/HUD Text/Scripts/HUDText.cs:227)

This error comes when it tries executing ht.Add... I don't want to start digging around in that code, so I'm hoping someone here can help? Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #78 on: February 18, 2013, 04:42:58 PM »
You need to use NGUITools.AddChild to instantiate UI-related components, providing a valid game object as parent. This game object needs to be in the UI hierarchy. You can't just create a random game object in the middle of nowhere and expect it to work properly with the UI.

Look at how I use HUDRoot.

Enzign

  • Guest
Re: NGUI: HUD Text
« Reply #79 on: February 19, 2013, 02:46:42 AM »
Is there a way to easily change the speed at which the text flows upwards and not having the numbers add up so much? One text per number would be preferable in my case i think.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #80 on: February 19, 2013, 09:08:02 AM »
The speed is controllable by one of the curves on HUDText. To make numbers not add up, just add them as a string (yourNumber.ToString()).

Grondhammar

  • Guest
Re: NGUI: HUD Text
« Reply #81 on: February 19, 2013, 04:20:03 PM »
You need to use NGUITools.AddChild to instantiate UI-related components, providing a valid game object as parent. This game object needs to be in the UI hierarchy. You can't just create a random game object in the middle of nowhere and expect it to work properly with the UI.

Look at how I use HUDRoot.

After eleven hours of work on this, I now have text on the screen. It's staying in the middle of the screen, following no object even though UIFollowTarget has been added and target set (and validated that it's set in the HUD code).

Surely I can't be the only one thinking there should be documentation or at least a video tutorial on how to deal with dynamically created objects and NGUI HUDText? Seems like a very obviously missing piece.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #82 on: February 19, 2013, 07:56:34 PM »
This is what your hierarchy should look like:

UIRoot
- UICamera
-- UIAnchor
--- UIPanel (can have HUDRoot on it)
---- UIFollowTarget (with UI and game camera fields set to correct cameras)
----- HUDText
----- UILabel (unit's name)
----- Slider (unit's health)

Enzign

  • Guest
Re: NGUI: HUD Text
« Reply #83 on: February 22, 2013, 09:46:56 AM »
Thanks ArenMook! Wonderful piece of software. Well worth the money. Both NGUI and the HUD Text. I've used EZ Gui a lot before, and he does have great support as well, but the software is so much harder to use than NGUI imho.

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Re: NGUI: HUD Text
« Reply #84 on: February 24, 2013, 10:39:25 AM »
The following shows cumulative damage similar to the OnClick() version in ExampleTwo of the package.

  1.         void OnClick ()
  2.         {
  3.                 if (mText != null)
  4.                 {
  5.                         if (UICamera.currentTouchID == -1) mText.Add(-10f + Random.value * -10f, Color.red, 0f);
  6.                         else if (UICamera.currentTouchID == -2) mText.Add(10f + Random.value * 10f, Color.green, 0f);
  7.                 }
  8.         }

By modifying the above code to the following it doesn't show cumulative damage, but each damage individually.

  1.         public void DisplayDamage ()
  2.         {
  3.                 if (mText != null)
  4.                 {
  5.                        
  6.                         totalDamage = totalDamage + damageRecieved;
  7.                         mText.Add("-" + totalDamage , Color.red, 0f);
  8.                        
  9.                 }
  10.         }

What would you suggest to get all the damage to add up as in the example?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #85 on: February 24, 2013, 11:00:35 AM »
If you add a string, it won't add up. If you add a float, it will add up. Change it to this:
  1. mText.Add(-totalDamage , Color.red, 0f);

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Re: NGUI: HUD Text
« Reply #86 on: February 24, 2013, 11:09:10 AM »
That did it.  Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #87 on: February 24, 2013, 12:07:15 PM »
One of the most common questions I get is how to add more than just text to the HUD UIs created with HUDText.

Well... here's a video!

http://www.youtube.com/watch?v=idVr8-jfdAI

invitado123

  • Guest
Re: NGUI: HUD Text
« Reply #88 on: March 01, 2013, 09:11:05 AM »
Hello there, first of all kudos on NGUI, it's made my life so much easier

I've been using HUDText to display the score granted to the player whenever they pick up a star, for this I have this code when the player hits the star

  1.         GameObject nGUI = GameObject.Find("GUI");
  2.         GameObject scoreLabel = NGUITools.AddChild(nGUI, scoreLabelPrefab);
  3.         UIFollowTarget follow = scoreLabel.GetComponent<UIFollowTarget>();
  4.         follow.target = transform;
  5.         follow.gameCamera = Camera.main;
  6.         HUDText hudText = scoreLabel.GetComponent<HUDText>();
  7.         hudText.Add(starValue, Color.red, 0.0f);
  8.  

However my text is not showing up, changing the Disable If Invisible flag does nothing.

the GUI object is a UI created using the UI creation tool of nGUI. The scoreLabelPrefab is a gameObject that contains the UIFollowTarget and HUDText scripts.

Can you please help?

Thank you

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #89 on: March 01, 2013, 09:34:18 AM »
You never set the uiCamera property of UIFollowTarget, so it may be finding the wrong one. When you select the instantiated object, where does it appear to be?