Author Topic: UIGrid multi line and sorting  (Read 8344 times)

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
UIGrid multi line and sorting
« on: April 22, 2014, 07:02:31 PM »
If I have a horizontal UIGrid with multiple rows, and the sorting is on none, it works as expected with the exception of I cannot insert things into the middle of the grid, they slide to the end. This makes sense. If I use horizontal sorting, it works fine until you hit the 2nd row. In this case it checks the horizontal position of things in the top row and the bottom row without considering their y position. Do I need a custom sort function here based on x and y or am I missing something?

Thanks!

Darkmax

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 93
    • View Profile
Re: UIGrid multi line and sorting
« Reply #1 on: April 22, 2014, 07:41:16 PM »
I'm not sure if this solve your problem, but maybe you need to use the UITable instead of UIGrid.

But maybe another user more expert on this will help you.

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: UIGrid multi line and sorting
« Reply #2 on: April 22, 2014, 09:37:59 PM »
I poked at table, but the grid seemed to layout things in a flow pattern more like what i was going for. Its likely i just need to change the sorting logic to care about more then just x when its horizontal and there are multiple rows

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid multi line and sorting
« Reply #3 on: April 23, 2014, 05:31:12 AM »
If you need multi-column sorting based on height, then yes you will need to set the custom sorting function.

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: UIGrid multi line and sorting
« Reply #4 on: April 23, 2014, 12:01:15 PM »
If anyone needs this later, this modification allows UIGrid to have sorted children in multiple rows.

The sorting enum changes to
  1.     public enum Sorting
  2.         {
  3.                 None,
  4.                 Alphabetic,
  5.                 Horizontal,
  6.                 Vertical,
  7.                 Custom,
  8.         HorizontalDown
  9.         }
  10.  

Next to all the sorting statics add

  1.     static public int SortHorizontalDown(Transform a, Transform b)
  2.     {
  3.         float delta = a.localPosition.y - b.localPosition.y;
  4.         if(Mathf.Abs(delta) < a.parent.gameObject.GetComponent<UIGrid>().cellHeight / 2)
  5.         {
  6.             return a.localPosition.x.CompareTo(b.localPosition.x);
  7.         }
  8.         else
  9.         {
  10.             return delta > 0 ? -1 : 1;
  11.         }
  12.     }
  13.  

and then finally where it picks the sorting function add
  1. else if (sorting == Sorting.HorizontalDown) list.Sort(SortHorizontalDown);