Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: greyhoundgames on April 22, 2014, 07:02:31 PM

Title: UIGrid multi line and sorting
Post by: greyhoundgames 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!
Title: Re: UIGrid multi line and sorting
Post by: Darkmax 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.
Title: Re: UIGrid multi line and sorting
Post by: greyhoundgames 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
Title: Re: UIGrid multi line and sorting
Post by: ArenMook 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.
Title: Re: UIGrid multi line and sorting
Post by: greyhoundgames 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);