- Home /
Can you change to order of children? (Insert a new child)
Im using NGUI which in some of its components displays its items by the order of its children (UITable) I want to add a child into a specific location the list of children. Anyone think of a way to do this?
Only one I can come up with is to get a list of the children, null their parents and then add them back in the order I want. Not a great solution by any means.
Answer by Bunny83 · Mar 21, 2013 at 03:43 PM
Abomb's solution should work, but UITable just sorts it's children once at start and caches the references in an internal List.
The Good news are that they provide the "children" property which returns the direct reference to that List, so you can sort it as you wish in code.
If you just want to order them at edit time, just use Abomb's solution since NGUI doesn't provide a GUI for changing the order.
edit
Unfortunately NGUI trashes it's internal list whenever it repositions it's elements. That means it sorts them again just by name. So unless you change the NGUI framework (which i wouldn't recommend) the name is the only way to define an order.
I need to look at the code more closely :) Seeing children I had assumed that it was the transform children not a list that I have complete control over... Feel pretty dumb at the moment.
Hmm, damn, too late :D I just edited my answer and wanted to convert my answer to a comment...
Actually I have in my case I have overridden the table class with a specific column class to better control this. Also notice that it is not by name unless the Sorted is checked. It is by the transform children order.
But the all presents the same problem. I am dynamically adding elements to this(column/table) by parenting them which then needs to reposition the contents because something has moved (elements are dynamically sizing) and it is clearing its internal list and recreating it based off of children which is ordered based on when the GameObject was parented...
I guess I need to override that functionality and control that myself... yuck.
Still you helped me realize that 1) I can't control the order in the transform but there is a list that might provide me what I want, i just have to watch for when it is modified as well as when children are added to the transform and correctly add them to the list.
Of course ;) You can't really rely on the order of GameObjects, that's why you always should sort them manually. One possible edit would be to provide a sorting delegate which defaults to "SortByName" but you can set your own sorting function from outside. That wouldn't break "too much" the concept / interface of NGUI i guess.
Not a bad IDEA!!! I have already had to change the NGUI code to allow me to override the UITable so extending it a little more won't hurt. (Just Comment it well so a merge to upgrade is some what painless) THAN$$anonymous$$S!
Answer by Rayne · Dec 14, 2014 at 02:49 PM
Transform class provides SetSiblingIndex method, also there is methods SetAsFirstSibling and SetAsLastSibling. It might be helpful.
$$anonymous$$oved accepted answer here, the various sibling methods were not available when this question was asked, but they are now the best way to sort the hierarchy.
Weird that this came as an answer since the question is about NGUI while this is uGUI.
Answer by TheFrozenRaven · Jul 23, 2016 at 02:53 PM
How about using: transform.SetAsFirstSibling() and transform.SetAsLastSibling() ?
Answer by Abomb · Mar 21, 2013 at 03:28 PM
By prefixing the names of the children with numbers, i.e. 01_ 02_ 03_ etc, you can basically "order" the children. Not ideal, but at least reliable.
Sorry it wasn't clear, but I thought that insert a new child and talking about code made it clear, this is happening at run time.