1
NGUI 3 Support / Wrong position for child objects
« on: June 30, 2016, 02:06:43 PM »
Guys a Have a question about child components position.
My widget structure is like:
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:
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.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;
- }