Author Topic: how do you use nguitools.draw?  (Read 2490 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
how do you use nguitools.draw?
« on: January 01, 2016, 03:18:52 PM »
I wanted to display simple info and label shows in hierarchy but nothing is drawn, font is miising...do you need to setup ui that you want to show with nguitools.draw beforehand? And if so how it is exactly done?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how do you use nguitools.draw?
« Reply #1 on: January 01, 2016, 07:17:00 PM »
Here's an example of me putting an on-screen health bar in Windward.
  1. UISprite fg = NGUITools.Draw<UISprite>("fg", delegate(UISprite sp)
  2. {
  3.         sp.depth = 1;
  4.         sp.atlas = Resources.Load<UIAtlas>("UI/Atlas - Windward");
  5.         sp.spriteName = "Flat";
  6.         sp.type = UISprite.Type.Sliced;
  7.         sp.color = new Color(0.25f, 1f, 0f, 1f);
  8.         sp.pivot = UIWidget.Pivot.TopLeft;
  9.         sp.SetAnchor(0.5f, -200,
  10.                 0.8f, -10,
  11.                 0.5f, 200,
  12.                 0.8f, 10);
  13.  
  14.         UISprite bg = sp.gameObject.AddWidget<UISprite>(0);
  15.         bg.atlas = sp.atlas;
  16.         bg.spriteName = "Flat";
  17.         bg.type = UISprite.Type.Sliced;
  18.         bg.color = new Color(0.2f, 0.2f, 0.2f, 1f);
  19.         bg.pivot = UIWidget.Pivot.TopLeft;
  20.         bg.SetAnchor(0f, 0,
  21.                 0f, 0,
  22.                 1f, 0,
  23.                 1f, 0);
  24.  
  25.         UILabel lbl = sp.gameObject.AddWidget<UILabel>(2);
  26.         lbl.name = "Name";
  27.         lbl.pivot = UIWidget.Pivot.BottomLeft;
  28.         lbl.bitmapFont = Resources.Load<UIFont>("UI/Font - Qlassik22");
  29.         lbl.fontSize = lbl.bitmapFont.defaultSize;
  30.         lbl.effectStyle = UILabel.Effect.Shadow;
  31.         lbl.gradientBottom = new Color(0.5f, 0.5f, 0.5f, 1f);
  32.         lbl.SetAnchor(0f, 5,
  33.                 1f, 2,
  34.                 0f, sp.width - 10,
  35.                 1f, 30);
  36.  
  37.         UISlider slider = sp.gameObject.AddComponent<UISlider>();
  38.         slider.backgroundWidget = bg;
  39.         slider.foregroundWidget = sp;
  40. });
The delegate function there is executed only once -- when the widget is first created. When that happens it adds a bunch of child widgets to itself and sets up the anchoring of everything.

I then have code that uses the "fg.value". It's the only thing that needs to change every update.

So in short -- NGUITools.Draw lookup is very quick, so it's safe to put it in Update(), and the delegate is executed once, so put init code there.

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: how do you use nguitools.draw?
« Reply #2 on: January 01, 2016, 07:50:06 PM »
ok i understand now how it's created, but how do you later access slider? you only have reference to the sprite you created?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how do you use nguitools.draw?
« Reply #3 on: January 01, 2016, 09:23:32 PM »
  1. sp.GetComponent<UISlider>();