Search Unity

Unity UI Layout Group: Position elements at opposite sides

Discussion in 'UGUI & TextMesh Pro' started by SimRuJ, Oct 12, 2018.

  1. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    I've got this panel with a layout group, a "previous" and a "next" button:



    Bigger version: click

    Is it possible to position the "previous" button at the very left and the "next" button on the very right using the layout group?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Is there a reason that you need the layout group at all, rather than just using anchors? If you remove the layout group and just anchor each button to a different side of the parent, that would be simpler and more efficient.

    Even if you're going to have other objects that you want the layout group to arrange in the remaining space, you might want to make the layout group into a child that only takes up the space between the side buttons, or give the side buttons LayoutElement components with "ignore layout" checked so that they won't be controlled by the layout group (and then give the layout group enough padding on the left and right so that it won't overlap the buttons).

    But if there's some reason that you still need these buttons to be controlled by the layout group, you'll probably need to insert a third object between the buttons to take up the extra space. I think you'll want to turn off "child force expand", turn on "child controls size", and add a LayoutElement to each child, with a preferred width for the buttons and a flexible width for the center piece that takes up the slack.
     
    SimRuJ likes this.
  3. SimRuJ

    SimRuJ

    Joined:
    Apr 7, 2016
    Posts:
    247
    Thanks for your answer!
    To be honest, I was just wondering if it was even possible because I like layout groups but I'm not a fan of setting fixed lengths for obvious reasons.
    I've solved it with anchors now, thanks.