Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: appminis-mike on June 02, 2013, 11:17:36 AM

Title: UIImageButton doesn't resize until mouse over
Post by: appminis-mike on June 02, 2013, 11:17:36 AM
I'm creating a UIImageButton programmatically like so:

  1. var button = NGUITools.AddChild<UIImageButton>(parent);
  2. button.name = "my image button";
  3. button.normalSprite = GuiButtonGroup.NormalSprite;
  4. button.hoverSprite = GuiButtonGroup.NormalSprite; // This is intentional. Setting hoverSprite to NormalSprite disables the hover image, which is what we want.
  5. button.pressedSprite = GuiButtonGroup.PressedSprite;
  6.  
  7. var sprite = NGUITools.AddSprite(button.gameObject, atlas, GuiButtonGroup.NormalSprite);
  8. sprite.depth = GuiButtonGroup.Depth;
  9.  
  10. button.target = sprite;
  11.  
  12. var collider = button.gameObject.AddComponent<BoxCollider>();
  13. collider.size = sprite.transform.localScale;

However, the sprite's size (it's transform's scale) is (100, 100, 1) until the user mouses over the button, which then properly resizes the sprite to its proper size (the size of the NormalPressed image I'm providing). This is not only a problem because the sprite is distorted (until the mouse over) and because the collider's size is (100, 100, 1) even after the resize, so the button doesn't click properly.

How can I get the ImageButton to resize itself to its sprite's size automatically, before creating my collider?
Title: Re: UIImageButton doesn't resize until mouse over
Post by: ArenMook on June 02, 2013, 06:27:15 PM
Don't forget to call MakePixelPerfect() if you're changing the sprites manually.
Title: Re: UIImageButton doesn't resize until mouse over
Post by: appminis-mike on June 02, 2013, 08:13:37 PM
That did it. Thanks!

Also, would you mind confirming that the above is a good way to create a UIImageButton programmatically? We're really dependent on NGUI, and I want to make sure I'm establishing good practices.