Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by vbbartlett · Mar 21, 2013 at 02:54 PM · transformchildrenorder

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vbbartlett · Mar 21, 2013 at 03:52 PM 0
Share

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.

avatar image Bunny83 · Mar 21, 2013 at 04:02 PM 0
Share

Hmm, damn, too late :D I just edited my answer and wanted to convert my answer to a comment...

avatar image vbbartlett · Mar 21, 2013 at 04:12 PM 0
Share

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.

avatar image Bunny83 · Mar 21, 2013 at 04:29 PM 0
Share

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.

avatar image vbbartlett · Mar 21, 2013 at 04:34 PM 0
Share

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!

Show more comments
avatar image
36

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joe_User · Jan 07, 2015 at 05:40 AM 0
Share

This works very well. Thanks!

avatar image Kiwasi · Jan 07, 2015 at 07:10 AM 0
Share

$$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.

avatar image sumeetkhobare · Aug 30, 2015 at 08:00 AM 0
Share

Good Answer

avatar image fafase · Aug 30, 2015 at 08:16 AM 0
Share

Weird that this came as an answer since the question is about NGUI while this is uGUI.

avatar image christianstrang · Jan 14, 2016 at 01:22 PM 0
Share

works like a charm!

avatar image
4

Answer by TheFrozenRaven · Jul 23, 2016 at 02:53 PM

How about using: transform.SetAsFirstSibling() and transform.SetAsLastSibling() ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vbbartlett · Mar 21, 2013 at 03:53 PM 0
Share

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.

avatar image Lohoris2 · Jul 08, 2016 at 04:45 PM 0
Share

No longer true

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.Find always returns null 1 Answer

use list of gameobjects transform in raycast 0 Answers

transform.childCount == 0, but a child shows up in the hierarchy. How is this possible? 2 Answers

Pre-Emptive Optimisation Help 0 Answers

I have a gun model made up of multiple parts, attached to a rotating transform. Parts are transforming separately. (Images) 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges