Author Topic: Dynamically create/show text?  (Read 4427 times)

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Dynamically create/show text?
« on: August 19, 2013, 12:00:18 AM »
I need to show some feedback text when the player gets something, for example, pickup a hidden token, and "YOU FOUND A GOLDEN TOKEN!" appears on the screen for a few seconds.
Right now, I am instantiating a Prefab which is setup this way: UIRoot-Camera-Panel-UILabel, and I destroy it after X seconds.

But, I already have a main HUD with a UIRoot-Camera-Anchor-Panel present in the scene for my normal HUD, so the way I am doing it above seems over-complicated and creates another UI Camera as well.

Does NGUI have a "CreateHUDText" function/tool I am un-aware of? What would be the better way to show a temporary on-screen message with NGUI?

Thanks for your time!
Stephane

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Dynamically create/show text?
« Reply #1 on: August 19, 2013, 01:20:24 AM »
You could just "child" the dynamically created label to your main HUD.
  1.     private void ShowHUDText(string text)
  2.     {
  3.         // Your hud panel
  4.         GameObject panel = GameObject.Find("YourMainHUDPanel");
  5.  
  6.         // The instantiated label
  7.         GameObject textLabel = Instantiate(Resources.Load("Prefab/HUDText")) as GameObject;
  8.  
  9.         // Child the label to the panel
  10.         textLabel.transform.parent = panel.transform;
  11.  
  12.         // Rescale the label
  13.         textLabel.GetComponent<UILabel>().MakePixelPerfect();
  14.     }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamically create/show text?
« Reply #2 on: August 19, 2013, 09:08:52 AM »
Never use Instantiate. Use NGUITools.AddChild -- it sets the layer, resets the local pos/rot/scale, and parents right away.

ronronmx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Dynamically create/show text?
« Reply #3 on: August 19, 2013, 11:08:20 AM »
Ah yes, I just found out about AddChild last night and I am using it now, works great!

Thanks for the example lime-green.at, I wasn't calling MakePixelPerfect and that was my problem. Using the AddChild method + MakePixelPerfect is setting everything up correctly.

Thanks guys!