Author Topic: Wrong position for child objects  (Read 1986 times)

rafinha

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
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:
  1. - UI Sprite
  2.     - Button
  3.         - Label
  4.  
  5.  

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:
  1. private void CreateButtonUI(GameObject parent, string name, int width, int height, string label, string atlas = "Atlas/ui")
  2.     {
  3.         GameObject main = NGUITools.AddChild(parent);
  4.         main.name = name;
  5.  
  6.         // Attach sprite to receive atlas
  7.         UISprite background = main.AddComponent<UISprite>();
  8.         background.height = height;
  9.         background.width = width;
  10.         background.atlas = Resources.Load<UIAtlas>(atlas);
  11.         background.depth = 1;
  12.  
  13.         // Add collider to the button
  14.         BoxCollider bc = main.AddComponent<BoxCollider>();
  15.         bc.size = new Vector3(width, height, 0);
  16.         bc.isTrigger = true;
  17.  
  18.         // Attach button component
  19.         UIButton bt = main.AddComponent<UIButton>();
  20.  
  21.         // Set sprite state
  22.         bt.tweenTarget = bt.gameObject;
  23.         bt.normalSprite = "btn_brown";
  24.         bt.hoverSprite = "btn_green";
  25.         bt.pressedSprite = "btn_red";
  26.         bt.disabledSprite = "btn_gray";
  27.  
  28.         // Set text label
  29.         GameObject btLabel = NGUITools.AddChild(main);
  30.         UILabel lb = btLabel.AddComponent<UILabel>();
  31.         lb.bitmapFont = Resources.Load<UIFont>("Fonts/calibri");
  32.         lb.depth = 1;
  33.         lb.width = 142;
  34.         lb.height = 28;
  35.         lb.text = label;
  36.         lb.alignment = NGUIText.Alignment.Center;
  37.         lb.fontSize = lb.bitmapFont.defaultSize;
  38.         lb.effectStyle = UILabel.Effect.Shadow;
  39.         lb.applyGradient = false;
  40. }
  41.  

wizardsmoke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 5
  • Posts: 40
    • View Profile
Re: Wrong position for child objects
« Reply #1 on: July 01, 2016, 07:27:43 AM »
I have not experienced this problem, but you can easily control the position of your button.  For example:

  1. main.transform.localPosition = Vector3.zero;

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Wrong position for child objects
« Reply #2 on: July 01, 2016, 11:31:14 PM »
Default pivot point is the center of a widget, thus anything you add as a child to it will also be in the center as (0, 0, 0).