Here's an example of me putting an on-screen health bar in Windward.
UISprite fg = NGUITools.Draw<UISprite>("fg", delegate(UISprite sp)
{
sp.depth = 1;
sp.atlas = Resources.Load<UIAtlas>("UI/Atlas - Windward");
sp.spriteName = "Flat";
sp.type = UISprite.Type.Sliced;
sp
.color = new Color
(0
.25f, 1f, 0f, 1f
); sp.pivot = UIWidget.Pivot.TopLeft;
sp.SetAnchor(0.5f, -200,
0.8f, -10,
0.5f, 200,
0.8f, 10);
UISprite bg = sp.gameObject.AddWidget<UISprite>(0);
bg.atlas = sp.atlas;
bg.spriteName = "Flat";
bg.type = UISprite.Type.Sliced;
bg
.color = new Color
(0
.2f, 0
.2f, 0
.2f, 1f
); bg.pivot = UIWidget.Pivot.TopLeft;
bg.SetAnchor(0f, 0,
0f, 0,
1f, 0,
1f, 0);
UILabel lbl = sp.gameObject.AddWidget<UILabel>(2);
lbl.name = "Name";
lbl.pivot = UIWidget.Pivot.BottomLeft;
lbl.bitmapFont = Resources.Load<UIFont>("UI/Font - Qlassik22");
lbl.fontSize = lbl.bitmapFont.defaultSize;
lbl.effectStyle = UILabel.Effect.Shadow;
lbl
.gradientBottom = new Color
(0
.5f, 0
.5f, 0
.5f, 1f
); lbl.SetAnchor(0f, 5,
1f, 2,
0f, sp.width - 10,
1f, 30);
UISlider slider = sp.gameObject.AddComponent<UISlider>();
slider.backgroundWidget = bg;
slider.foregroundWidget = sp;
});
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.