Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - rafinha

Pages: [1]
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:
  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.  

Pages: [1]