Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reverse order LayoutGroup

Discussion in 'UGUI & TextMesh Pro' started by TheDelhiDuck, Oct 16, 2014.

  1. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    34
    Hi there,
    I would like to reverse the order of the VerticalLayoutGroup. Normally of course I could just re-order the elements in the hierarchy, but I need the child items to be rendered in a specific order as there is some overlapping between the elements.
    So firstly I would suggest being able to specify the order in which the elements are arranged for LayoutGroups.
    As a work-around I've overridden VerticalLayoutGroup, reversing the order of the children before the call to CalculateLayoutInputHorizontal then reversing again.

    Code (csharp):
    1.  
    2. public class ReverseVerticalLayoutGroup : VerticalLayoutGroup
    3. {
    4.    private void ReverseChildren()
    5.    {
    6.      for (var i = 1; i < transform.childCount; i++)
    7.      {
    8.        transform.GetChild(0).SetSiblingIndex(transform.childCount - i);
    9.      }
    10.    }
    11.  
    12.    public override void CalculateLayoutInputHorizontal()
    13.    {
    14.      ReverseChildren();
    15.      base.CalculateLayoutInputHorizontal();
    16.      ReverseChildren();
    17.    }
    18. }
    19.  
    A little clugy but it works, for anyone who has the same problem, or is there a better solution? Also not sure why I only need to do this for CalculateLayoutInputHorizontal and not CalculateLayoutInputVertical?
    Thanks
     
    ZiadJ likes this.
  2. Sessional

    Sessional

    Joined:
    Apr 21, 2013
    Posts:
    45
    No clue if it will work for your purposes, but have you tried flipping your VerticalLayoutGroup object 180 degrees around an axis?
     
    EvSpentys likes this.
  3. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    34
    Ah thanks that's a good idea, of course it does mean I have to rotate all the children but that's no biggy.
     
  4. Sessional

    Sessional

    Joined:
    Apr 21, 2013
    Posts:
    45
    Yeah, there is that. Did that actually work for the reversed order?
     
  5. TheDelhiDuck

    TheDelhiDuck

    Joined:
    Aug 29, 2014
    Posts:
    34
    Yes like a charm thanks
     
  6. MTGKS

    MTGKS

    Joined:
    Jul 12, 2018
    Posts:
    2