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.
Moved 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.
Abomb’s solution should work, but UITable just sorts it’s children once at start and caches the references in an internal List< Transform >.
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.
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) THANKS!
@mwk888: Most likely it can be used to rearrange the order of GameObjects, yes. This however is a quite new feature. It's now possible to drag gameobjects of different positions in the hierarchy. SetSiblingIndex is probably the script interface for that functionality.
This works very well. Thanks!
– Joe_UserMoved 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.
– KiwasiGood Answer
– sumeetkhobareWeird that this came as an answer since the question is about NGUI while this is uGUI.
– fafaseworks like a charm!
– christianstrang