Author Topic: Request: UIButton alignment options  (Read 1733 times)


  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Request: UIButton alignment options
« on: July 17, 2013, 01:06:29 PM »
EDITED for clarity:

There are three UIWidget-derived objects in NGUI: UILabel, UISprite, and UITexture. The UIWidgetInspector class utilizes this fact and offers a pivot alignment control within the Unity Inspector, which is awesome for when I want to align these against one edge, then change my mind and align them against a different edge.

The UIButton and UIImageButton classes do NOT derive from UIWidget, however, and I wonder how difficult it would be to create the same kind of pivot alignment controls for those. I find myself resizing buttons (by resizing the UISprite and UILabels within them) in the editor, which requires adjustment to the parent GameObject and its BoxCollider each time.

In order to manually set a button's pivot to the bottom-right, I need to:
  • Scale the child UISprite to the desired size, then offset it by -1/2 its x scale and 1/2 its y scale (the sprite will appear too high and too far left)
  • Use the same x and y positions for the UILabel as the UISprite (the label should appear to be centered over the UISprite, which is up and left of the main UIButton's center point)
  • On the parent UIButton's BoxCollider, match both the UISprite's x and y positions as well as the x and y scales (the collider should now align over the top of the sprite)
  • Now, the button's pivot should be in the bottom-right corner of the button

.. ...
Once the button's pivot is in the bottom-right corner, if I decide to increase the offset from the edge of the panel, there's no need to recalculate the UISprite's dimensions (plus the edge offset) each time: I can simply set the GameObject's position directly to the desired offset.

But if I want to change the pivot (like if I decide the button should go in a different corner, or even centered in the panel), I have to repeat steps 1-4 again with the new offsets.

Could I request a similar "one-button solution" like we have with the UIWidgetInspector's alignment tools, for UIButtons an UIImageButtons? That would be awesome!
« Last Edit: July 18, 2013, 01:22:11 AM by OnlineCop »


  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request: UIButton alignment options
« Reply #1 on: July 18, 2013, 04:32:03 AM »
I resolved this properly in the upcoming UI system, but there is no easy way to do it correctly in NGUI as it relies on transform.scale for widget size. I may back-port this feature to NGUI in the near future, but my only concern is backwards compatibility.