Author Topic: Inverted UIGrid - User Mod  (Read 2436 times)

dragagon

  • Guest
Inverted UIGrid - User Mod
« on: June 27, 2012, 05:55:21 PM »
Hey everyone, I just wanted to share a minor mod I made to the UI Grid that would enable the grid to function like the notification window of a Zynga game - namely populating from the bottom up. Basically I added a bool for inverting the direction, then within the reposition code, i use the bool to determine a -1 or 1 direction to add to (1 mean inverted, -1 is the original)

So here goes:

  1. public class UIGrid : MonoBehaviour
  2. {
  3.         .
  4.         .
  5.         .
  6.         public bool invertDirection = false;
  7.         .
  8.         .
  9.         .
  10.  
  11.         /// <summary>
  12.         /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary.
  13.         /// </summary>
  14.  
  15.         public void Reposition ()
  16.         {
  17.                 if (!mStarted)
  18.                 {
  19.                         repositionNow = true;
  20.                         return;
  21.                 }
  22.  
  23.                 Transform myTrans = transform;
  24.  
  25.                 int x = 0;
  26.                 int y = 0;
  27.                 int direction = invertDirection ? 1 : -1;
  28.                 if (sorted)
  29.                 {
  30.                         List<Transform> list = new List<Transform>();
  31.  
  32.                         for (int i = 0; i < myTrans.childCount; ++i)
  33.                         {
  34.                                 Transform t = myTrans.GetChild(i);
  35.                                 if (t) list.Add(t);
  36.                         }
  37.                         list.Sort(SortByName);
  38.  
  39.                         for (int i = 0, imax = list.Count; i < imax; ++i)
  40.                         {
  41.                                 Transform t = list[i];
  42.                                 if (!t.gameObject.active && hideInactive) continue;
  43.  
  44.                                 float depth = t.localPosition.z;
  45.                                 t.localPosition = (arrangement == Arrangement.Horizontal) ?
  46.                     new Vector3(cellWidth * x, direction * cellHeight * y, depth) :
  47.                     new Vector3(cellWidth * y, direction * cellHeight * x, depth);
  48.  
  49.                                 if (++x >= maxPerLine && maxPerLine > 0)
  50.                                 {
  51.                                         x = 0;
  52.                                         ++y;
  53.                                 }
  54.                         }
  55.                 }
  56.                 else
  57.                 {
  58.                         for (int i = 0; i < myTrans.childCount; ++i)
  59.                         {
  60.                                 Transform t = myTrans.GetChild(i);
  61.  
  62.                                 if (!t.gameObject.active && hideInactive) continue;
  63.  
  64.                                 float depth = t.localPosition.z;
  65.                                 t.localPosition = (arrangement == Arrangement.Horizontal) ?
  66.                     new Vector3(cellWidth * x, direction * cellHeight * y, depth) :
  67.                     new Vector3(cellWidth * y, direction * cellHeight * x, depth);
  68.  
  69.                                 if (++x >= maxPerLine && maxPerLine > 0)
  70.                                 {
  71.                                         x = 0;
  72.                                         ++y;
  73.                                 }
  74.                         }
  75.                 }
  76.  
  77.                 UIDraggablePanel drag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
  78.                 if (drag != null) drag.UpdateScrollbars(true);
  79.         }
  80. }
  81.