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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #195 on: April 15, 2014, 10:34:55 AM »
No, I wouldn't use HUDText for that. I would just create an NGUI window that I would instantiate/position/show as needed.

TokyoDan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: NGUI: HUD Text
« Reply #196 on: April 15, 2014, 05:29:35 PM »
Thanks Aron.

Anomalous

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: NGUI: HUD Text
« Reply #197 on: May 08, 2014, 04:58:23 PM »
I'm using a smaller-than-normal game viewport rect, which was causing UIFollowTarget to believe the target was either offscreen or in the wrong position. To make UIFollowTarget work as expected, I altered its Update() to use WorldToScreenPoint() and ScreenToWorldPoint() rather than WorldToViewportPoint() and ViewportToWorldPoint(). This should also work for the previous poster who was using multiple game viewports.

Is there any reason not to do it this way?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #198 on: May 09, 2014, 06:39:28 AM »
The logic at the time is to simply eliminate the need to transform from view space to screen space. It just saved some calculations. Nothing beyond that.

shallowspace

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUI: HUD Text
« Reply #199 on: June 05, 2014, 10:58:45 AM »
Apologies if this is covered before elsewhere but I have trouble with the text continuing to be displayed even if the item moves off-screen.



In this image there are ships behind me shooting at each other but the damage is displayed (yellow boxes) even though the ships are no longer visible.

While I have your attention: Very nice work on NGUI and HUDText, NGUI frustrates me sometimes but I really couldn't imagine Unity without it!

Peace,

James

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #200 on: June 06, 2014, 02:46:09 AM »
As I recall UIFollowTarget has the "disableIfInvisible" option. You need to have it checked.

shallowspace

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUI: HUD Text
« Reply #201 on: June 06, 2014, 05:26:18 AM »
Yes that box is checked... (and is by default)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #202 on: June 06, 2014, 10:09:19 PM »
If that's the case then it should get hidden. Check UIFollowTarget's Update() function, line 93. It sets the visibility there, which iterates through all children and enables/disables them as needed. Note that for this to work it implies that it requires children to work with like so:

GameObject (UIFollowTarget)
- Label (UILabel)

If you do it like so, it won't work because this hierarchy has no children:
Label (UIFollowTarget, UILabel)

shallowspace

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: NGUI: HUD Text
« Reply #203 on: June 08, 2014, 05:17:28 PM »
Ah yes that's it, many thanks.

ArtemKoval

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: NGUI: HUD Text
« Reply #204 on: July 04, 2014, 12:19:14 AM »
Hi!

I'm trying to instantiate object with HUDText at runtime. I've got 3 problems.

1.
  1.  
  2.  var label1 = (GameObject) Instantiate(UIController.Instance.Label, Vector3.zero, Quaternion.identity);
  3.  label1.transform.localScale = new Vector3(1f, 1f, 1f);
  4.  label1.transform.parent = UIController.Instance.UIRoot.gameObject.transform;
  5.  
  6.  foreach (Transform child in enemy.GetComponentInChildren<Transform>())
  7.  {
  8.        if ("label" == child.tag)
  9.        {
  10.                  label1.GetComponent<UIFollowTarget>().target = child;
  11.         }
  12.   }
  13.  
  14. public class UIController : Singleton<UIController>
  15.     {
  16.         public GameObject Label;
  17.         public GameObject UIRoot;
  18.  
  19.         public static UIController Instance
  20.         {
  21.             get { return ((UIController) mInstance); }
  22.             set { mInstance = value; }
  23.         }
  24.     }
  25.  

Given the code above HUDText object are instantiating  under UIRoot, but they have scale (360, 360, 360). I'm not sure how to fix this correctly in code.

2. After scale of each HUDText object have been changed by hand they seems to follow objects correctly. But there is one label right in the center of the screen, which is not moving and it seems it's not under UIRoot hierarchy. I don't know what is that and how to fix it to disappear.

3. HUDText objects that are following units are jittering. Units are moving in a rows from top to bottom of the screen, with constant speed
  1.  transform.position += direction * deltaTime;
How to avoid such jittering?

Thanks, Artem.
« Last Edit: July 04, 2014, 12:33:45 AM by ArtemKoval »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #205 on: July 04, 2014, 03:17:42 PM »
1. Never use Instantiate. Use NGUITools.AddChild. Look inside the function to find out why.

2. This is because you never set the layer. NGUITools.AddChild does this among other things.

ArtemKoval

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: NGUI: HUD Text
« Reply #206 on: July 05, 2014, 12:52:48 AM »
Thanks, this helped to solve part of the problems with scale.

But there is still one label right in the center of UI camera and that label is not moving. And there is still this problem with jittering.

  1. var label1 = NGUITools.AddChild(UIController.Instance.UIRoot.gameObject, UIController.Instance.Label);
  2.  
  3.                             foreach (Transform child in enemy.GetComponentInChildren<Transform>())
  4.                             {
  5.                                 if ("label" == child.tag)
  6.                                 {
  7.                                     label1.GetComponent<UIFollowTarget>().target = child;
  8.                                 }
  9.                             }
  10.  

It seems that all labels are in the center and following objects at the same time.

Maybe this will clarify things a bit - actually my objects are followed by Label, not HUDText.
« Last Edit: July 05, 2014, 12:23:03 PM by ArtemKoval »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #207 on: July 05, 2014, 11:28:00 PM »
There should only be one UIFollowTarget script per instantiated object, and it should ideally be on a simple game object, not a widget:

GameObject (UIFollowTarget, prefab root)
- UILabel (player name)

The layer must be your UI layer, and the main camera must be set to now draw the UI layer, otherwise NGUI won't be able to determine which camera should be used. Same with the UI camera -- it must not see the world. Whenever there is an issue following objects, layers are generally the cause.

ArtemKoval

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: NGUI: HUD Text
« Reply #208 on: July 06, 2014, 08:47:33 AM »
Hi!

Thanks for you help. Believe it or not - the problem was in the Disable If Visible flag. When it was turned on the problem with labels hoarding in the center of the screen persisted. When I turned it off everything became normal - no labels in the center.

Regards, Artem.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #209 on: July 07, 2014, 04:20:53 AM »
Ah, that's because the "disable if visible" only is able to disable child objects. This is why I was suggesting using this hierarchy:

GameObject (UIFollowTarget)
- Label child