Author Topic: UIGrid questions  (Read 4352 times)

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
UIGrid questions
« on: June 10, 2014, 07:21:30 PM »
Although it's possible to set the Pivot to 'Top Right', I'm wondering if there's a way to force a UIGrid to organize the items from right to left, rather than left to right. Using alphabetical ordering and adjusting names of the children accordingly won't work; that merely reorganizes the children, left to right, but with the children in the opposite order. What I am trying to accomplish is a row of health icons attached to the right side of the screen, limited to 6 per column. The problem is, the new column starts at the left end of the new row.

Besides my problems with left to right, I'm having some anomalous behavior where the 6th icon on the second row decides to start it's own new row. I'm not manually adjusting anything, I'm leaving everything up to the UIGrid component, so I'm not sure what's causing it. Below is the entire code that interfaces with the UIGrid, I add and remove icons by enabling or disabling their UISprite to make them invisible. I am renaming them so that Alphabetic sorting will organize them according to their index in the list of UISprites to show/hide; however, this causes even more unusual behavior including random gaps in the rows.

  1. public void CreateIcons (int current, int maximum)
  2. {
  3.         grid = GetComponent<UIGrid>();
  4.  
  5.         for (int i = 0; i < maximum; i++)
  6.         {
  7.                 GameObject newIcon = NGUITools.AddChild(transform.gameObject, armorIcon);
  8.                 icons.Add(newIcon.GetComponent<UISprite>());
  9.                 newIcon.name = i.ToString();
  10.                 grid.AddChild(newIcon.transform);
  11.         }
  12.         DisplayIcons(current);
  13. }
  14.  

Edit: I believe I've solved the problem with mysterious alphabetical sorting order; there are twelve icons. 10 comes after 1. The sorting is strictly alphabetical, not lexicographical. I'll need to prefix extra 0's before single digit numbers.
« Last Edit: June 10, 2014, 07:38:03 PM by spooky »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIGrid questions
« Reply #1 on: June 11, 2014, 05:37:47 AM »
Note the comment above UIGrid's AddChild function:
  1.         /// <summary>
  2.         /// Convenience method -- add a new child.
  3.         /// Note that if you plan on adding multiple objects, it's faster to GetChildList() and modify that instead. <-- this right here
  4.         /// </summary>
In any case, those methods are there for convenience, and currently you calling GetChildList() always rebuilds the list anyway, so there is also no point in using AddChild to begin with. There used to be... but it turned out that the order in which transforms are stored in Unity is not consistent.

So get rid of the AddChild part altogether, and simply call UIGrid.Reposition() afterwards.

spooky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 16
    • View Profile
Re: UIGrid questions
« Reply #2 on: June 11, 2014, 12:08:37 PM »
Thanks, that worked. They're behaving much more rationally now.
I still wish the grid could be organized right to left, and that might even make sense as an extension to localization for right to left languages. I thought about flipping them the grid object over but that's a tacky solution and rotation on other axes is disabled for NGUI objects. I may (attempt to) add the functionally myself, but that'll make updating NGUI tricky, so it would be nice to see as an official feature.