Guys a Have a question about child components position.
My widget structure is like:
- UI Sprite
- Button
- Label
Every time, when I try to create a dynamic button inside the sprite, the button "spawn" in the middle of the sprite. Why?
Why the button don't go for the position X = 0 and Y = 0 ?
Here is how I create my button:
private void CreateButtonUI(GameObject parent, string name, int width, int height, string label, string atlas = "Atlas/ui")
{
GameObject main = NGUITools.AddChild(parent);
main.name = name;
// Attach sprite to receive atlas
UISprite background = main.AddComponent<UISprite>();
background.height = height;
background.width = width;
background.atlas = Resources.Load<UIAtlas>(atlas);
background.depth = 1;
// Add collider to the button
BoxCollider bc = main.AddComponent<BoxCollider>();
bc
.size = new Vector3
(width, height,
0); bc.isTrigger = true;
// Attach button component
UIButton bt = main.AddComponent<UIButton>();
// Set sprite state
bt.tweenTarget = bt.gameObject;
bt.normalSprite = "btn_brown";
bt.hoverSprite = "btn_green";
bt.pressedSprite = "btn_red";
bt.disabledSprite = "btn_gray";
// Set text label
GameObject btLabel = NGUITools.AddChild(main);
UILabel lb = btLabel.AddComponent<UILabel>();
lb.bitmapFont = Resources.Load<UIFont>("Fonts/calibri");
lb.depth = 1;
lb.width = 142;
lb.height = 28;
lb.text = label;
lb.alignment = NGUIText.Alignment.Center;
lb.fontSize = lb.bitmapFont.defaultSize;
lb.effectStyle = UILabel.Effect.Shadow;
lb.applyGradient = false;
}