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

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #15 on: September 28, 2012, 09:31:44 AM »
I just bought this component and I am using it with the full version of NGUI (2.2.1).  I am unsure of how to apply this in my context, so would like a bit of help please.

I want to have a score float up above a Star object whenever the user collects them.  The player is moving up the screen rapidly (think doodle jump) and trying to collect stars along the way.  The stars are in the main scene rendered by the main camera.  I have created the following UI hierarchy:

UI Root (2D)
-Camera
--Anchor - Top Left (score labels)
--Anchor - Top Right (high score labels)
--HudText - contains only HudText and UIFollowScripts

In the collision detection script for my player, I am trying to make the HudText appear above the star like this:
  1.         if(collision.gameObject.tag=="Star") {
  2.                 // Get Collect Star script and trigger it
  3.                 var coll : StarCollected = collision.gameObject.GetComponent(StarCollected);
  4.                 // Setup Hudtext for collection
  5.                 follow.target = collision.gameObject.transform;
  6.                 hudText.Add("100", Color.white, 0.5f);
  7.                 // And tell the star to collect itself
  8.                 coll.CollectStar();
  9.         }

But nothing is appearing on screen.  I have set the Font for the text in the HudText component.

Am I missing something?  Is this the best way?  Also a bit concerned about if the users collects 2 stars in a row too quickly, what would then happen.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #16 on: September 28, 2012, 11:42:51 AM »
You need multiple HUDTexts for this. Each star would have its own HUDText element. What I do in Windward is have each game object that needs this kind of text instantiate a HUDText prefab object under the HUDRoot and set its follow target to be that object.

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #17 on: September 28, 2012, 02:39:13 PM »
Thanks, that will work.

However, why is this not working at all?  I get nothing on screen for it.  What am I missing?

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #18 on: September 28, 2012, 03:11:32 PM »
Never mind, found it.  Needed to be on a UIAnchor.

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #19 on: September 28, 2012, 03:48:23 PM »
Sorry about this.  Can you post a small sample of the code you have for instantiating the prefab.  I am using the following, however despite the HUDText appearing in the folder and being in the right layer, it doesn't appear.  Even zooming into it in the Editor show it position right but invisible.

  1.         // Create a hud text for the score
  2.         var hudObj : GameObject;
  3.         hudObj = Instantiate(hudTextPrefab, transform.position, Quaternion.identity);
  4.         hudObj.transform.parent = hud.transform;
  5.         //hudObj.transform.localScale = Vector3.one;
  6.         // Get the HudText  and UIFollow components
  7.         var hudText : HUDText = hudObj.GetComponent(HUDText);
  8.         var follow : UIFollowTarget = hudObj.GetComponent(UIFollowTarget);
  9.         // Set the target to the transfor of the star
  10.         follow.target=transform;
  11.         // Now add the score text
  12.         hudText.Add(score.ToString(), Color.white, 0.5f);
  13.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #20 on: September 28, 2012, 09:46:01 PM »
Use NGUITools.AddChild. It properly parents things for you, resets the position/rotation/scale, and sets the correct game object layer.

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #21 on: September 29, 2012, 07:31:49 AM »
Thanks.  Using that method now but still no text appears.

If I pause the game, and use the Widget tool to create a label under the Hud object it appears just fine.  However all the HUDText (Clone) prefabs that appear under that display nothing.

I've tried everything.  Pausing the game and moving the transforms of the HUdText or sub text object that gets greated, enabling and disabling components.  Nothing works.  It only works if I setup a quick test scene and don't spawn anything using prefabs.

As I said, even zooming right in on the editor and there is nothing visible. It's like the text is blank.

I notice that if I pause the game immediately after issueing hudText.Add the little label that gets created under the hudtext is disabled.  I've tried enabling this too and still nothing appears.  Completely lost here.

I've attached a screenshot of what it looks like running.  My other GUI elements work and as stated above manually creating a label also works:
 

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #22 on: September 29, 2012, 07:51:51 AM »
The problem appears to be an issue with the UIFollowTarget script.

If I comment out the line follow.target=transform, then it works but the text just appears in the middle of the screen.

I've check the script picked up the right cameras for MainCamera and GUICamera and it did, so not sure what is going on here.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #23 on: September 29, 2012, 10:53:50 AM »
Where is your HUDText instantiated? Is it under the HUDRoot / UIRoot?

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #24 on: September 29, 2012, 03:01:01 PM »
You can see it in the screenshot above.  It is under the Hud GameObject which is a UIAnchor, which is under the camera which is under the UIRoot.

Like I say, works if I create a label manually and also works if I don't set the follow but just appears in the middle of the screen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #25 on: September 29, 2012, 03:15:52 PM »
Odd then, it sounds like it's not finding the correct cameras to transform from 3D to on-screen coordinates. Is your game camera able to see your UI layer?

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #26 on: September 29, 2012, 03:30:58 PM »
No it can't.  The only camera that can see my GUI layer is the one under the UIRoot.

It isn't finding the wrong cameras.  I've checked that by making the mGameCamera and mUICamera public in the follow script and they are set to the Main Camera and UI Camera as they should be.

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #27 on: September 29, 2012, 03:57:02 PM »
To simplify this problem, I have manually created the HUDText object under the HUD prior to starting the scene then created a simple script on the same object that just calls Add once and shows it for 10 seconds. 

I have noticed the following behaviour:
1. If I don't set the follow target, the HUDText appears and works as expected on the screen.  The label that is created at runtime by the component has it's active property enabled in the Inspector.
2. If I set the follow target to any object at all, the HUDText does not appear.  The label that is created at runtime by the component has it's active property set to disabled in the Inspector.

I have changed the UIFollowTarget script as follows as a test, and it never displays Not Visible:

  1.                 // If visible, update the position
  2.                 if (isVisible)
  3.                 {
  4.                         transform.position = mUICamera.ViewportToWorldPoint(pos);
  5.                         pos = mTrans.localPosition;
  6.                         pos.x = Mathf.RoundToInt(pos.x);
  7.                         pos.y = Mathf.RoundToInt(pos.y);
  8.                         pos.z = 0f;
  9.                         mTrans.localPosition = pos;
  10.                 } else {
  11.                         Debug.Log("Not visible");
  12.                 }
  13.  

Really scratching my head with this.  There is obviously something wrong with the follow script.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: HUD Text
« Reply #28 on: September 29, 2012, 04:08:18 PM »
If it wasn't working right then examples wouldn't work either. I just had a look at it, and moving the "Pivot" object moves the HUDText labels exactly as you'd expect it.

steddyman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: NGUI: HUD Text
« Reply #29 on: September 29, 2012, 04:16:13 PM »
Found a work around.

I have to set the DisableIfInvisible flag to true.  If I do that, then follow works.  If I don't do that, then it doesn't and the label is always disabled.  Seems the opposite of what it should be doing.