Author Topic: Add Child On UIGrid problem: Arrangement is wrong  (Read 2494 times)

Cheong

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 8
    • View Profile
Add Child On UIGrid problem: Arrangement is wrong
« on: September 10, 2015, 02:58:11 AM »
Hi readers,

I am using NGUItools.AddChild(Parent, Prefab).

But when I was trying to add prefabs into the parent with array conditions, the arrangement seems to go wrong(the last object was added into the parent first instead of the first object).

Here the example code of how I do it:

public GameObject parent;
public GameObject prefab;

for(int i = 0; i<TotalNumberOfObjects; i++)
{
NGUItools.AddChild(parent,prefab);
prefab.name = "Object" + i;
}

The list of the prefabs added into the parent are as listed such as below:
1.)Object5;
2.)Object1;
3.)Object2;
4.)Object3;
5.)Object4;

Can you help me resolve this? Am I doing something wrong instead?

Thanks.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Add Child On UIGrid problem: Arrangement is wrong
« Reply #1 on: September 10, 2015, 12:56:31 PM »
I don't think this i a NGUI issue. I am not sure, but the order of children as it is displayed in the scene hierarchy in the editor might not necessarily be the same as Unity's internal order of the children. You could keep track of your children in a separate list or use them in another way so you do not have to rely on whats visible in the scene hierarchy.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Add Child On UIGrid problem: Arrangement is wrong
« Reply #2 on: September 10, 2015, 02:25:56 PM »
your code looks fine.  try adding reposition after your loop...

  1. grid.Reposition();
  2.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Add Child On UIGrid problem: Arrangement is wrong
« Reply #3 on: September 13, 2015, 05:52:27 AM »
Note that the sorting is alphabetic, not alphanumeric. Ie:

Object1
Object10 <-- note where this will end up
Object2
Object3
Object4
...etc

You need to name them like so:

Object01
Object02
Object03
...
Object09
Object10

Cheong

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Add Child On UIGrid problem: Arrangement is wrong
« Reply #4 on: September 22, 2015, 10:41:06 PM »
Ok, just saw this replies recently... not sure why I didn't noticed.
Problem solved btw, Thanks!