Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: ronronmx on August 19, 2013, 12:00:18 AM

Title: Dynamically create/show text?
Post by: ronronmx 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
Title: Re: Dynamically create/show text?
Post by: lime-green.at 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.     }
Title: Re: Dynamically create/show text?
Post by: ArenMook 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.
Title: Re: Dynamically create/show text?
Post by: ronronmx 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!