Search Unity

Can't Manually Order a Vertical Layout Group

Discussion in 'Editor & General Support' started by afw3000, Dec 10, 2019.

  1. afw3000

    afw3000

    Joined:
    Mar 4, 2016
    Posts:
    8
    I've got a store page in my app that lists a series of cards. I'd like these to appear in a particular order, but take advantage of the automatic spacing of the vertical layout group. The cards are of varying heights, but take up the entire width of the scroll view.

    My problem is that every time I place the cards in the correct order in the hierarchy (1, 2, 3, 4), the cards are then automatically reordered in the wrong order (3, 2, 4, 1) after I play the game or reopen the scene. I can't figure out what is causing this to happen, but it always reorders them in the same way. It's not ordering them by size, id, or alphabetical though, so there seems to be some other arbitrary sort in use.

    Is there a way to manually order items in a vertical layout group, or will it always order cards in some arbitrary way? I realize I might be able to get this to work through a script, but the artists on my team would prefer they be able to control the order in the editor hierarchy.

    I'm running Unity 2019.2.15 on Win 10 64-bit
     
    Botilada likes this.
  2. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    Exactly the same problem here. I need literally the opposite ordering of what Vertical Layout Group does, as my objects need to be drawn last to first, bottom-most to top-most, for their overlaps to work correctly.

    Presently, it appears to be impossible? I'm dropping the Vertical Layout Group and writing my own solution, for lack of any better idea.
     
  3. spajus

    spajus

    Joined:
    Jun 10, 2015
    Posts:
    47
    I bet you have prefabs in there. Here's a workaround, with reasoning:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. ///<summary>
    4. ///This is necessary to prevent prefab + layout group reordering bug
    5. ///due to which layout group always reorders children from prefabs according to
    6. //prefab's transform position, making it impossible to predict the order
    7. ///</summary>
    8. [ExecuteInEditMode]
    9. public class LayoutGroupReorder : MonoBehaviour {
    10.     [SerializeField] private Transform[] childOrder;
    11.  
    12.     void Start() {
    13.         for (int i = 0; i < childOrder.Length; i++) {
    14.             childOrder[i].SetSiblingIndex(i);
    15.         }
    16.     }
    17. }