Author Topic: UIImageButton doesn't resize until mouse over  (Read 3421 times)

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
UIImageButton doesn't resize until mouse over
« 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIImageButton doesn't resize until mouse over
« Reply #1 on: June 02, 2013, 06:27:15 PM »
Don't forget to call MakePixelPerfect() if you're changing the sprites manually.

appminis-mike

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: UIImageButton doesn't resize until mouse over
« Reply #2 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.