Search Unity

Unity 4.5 New Hierarchy Window sorting

Discussion in 'Editor & General Support' started by Ben-BearFish, May 27, 2014.

  1. Jenzo83

    Jenzo83

    Joined:
    Oct 22, 2013
    Posts:
    35
    Thanks phil-Unity

    For the moment we use NGUI system to handle all our 2D so we are not in the need of rearranging. So thanks for clarify where to find, how to list in Alphabetically order.

    But I must ask, what happened to the search function? If I search in the Hierarchy View, for any object, select it and close the search. It doesn't expand my GameObjects anymore, so I can't actually see what I have selected. If you have a lot of GameObject nesting or searching for a certain scripts it's impossible to find the one you have searched for in the Hierarchy View!

    Just a minute later I found a way! If you press "F" it focus on the object in the Hierarchy View!
     
    Last edited: Aug 15, 2014
  2. budwheizzah

    budwheizzah

    Joined:
    Apr 3, 2014
    Posts:
    13
    Ok, let me give this a shot. Obviously the script "solution" was a "problem" as you could no longer build until you removed the script. Thanks for this just tried it and it works. Good to know some software makers still listen. Very appreciated, the other sorting feature was not viable nor was it an actual "feature" and you reacted. Cool.
     
  3. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    This is a bug and has since been resolved I'm not 100% positive if it will make it into a 4.5.X release but it for sure will work properly again in 4.6

    I think the issue you might have been seeing would be caused by the sorting script not being placed inside a Editor folder. Anything in the Editor folder will not be built/ compiled into a build. And FYI the custom scripting sorting is still there for people who want to a very custom solution but i think we've at least covered the basics again.
     
  4. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Regarding 4.6, now that the Beta is out, is there a way to setup a hierarchy script sort so that if a game object is not a child of a ui canvas object it sorts alphabetically, but otherwise it uses Unity's new default sort?
     
  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    This isnt something we support by default but should be achievable by writing your own sorting method. That being said though it would break the ability to reorder the hierarchy via drag and drop. This is a limitation of the system at the moment because its impossible to know what is the desired drop position when sorting is not Transform.
     
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Okay, I guess my only issue with the default sorting system which uses the UI is that our projects are heavily reliant on LoadSceneAdditive, and it seemed as of 4.5 that when the additive scene came in it just added the new game objects all over the place. Has there been a fix for this, or how would we properly sort the new game objects as it is in drag and drop order? It'd be nice if the new game objects just went to the bottom of the sorting list.
     
  7. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Hmm i dont think we've ever tested what happens with LoadSceneAdditive but now that you mention it i could see how it was horribly screw up :(. So to answer your first question no there hasnt been a fix as noone has brought this to our attention yet. As for your second question, I dont know of a way to manage that from your side. The issue is in each scene file the root object store their position data (where they live relative to other objects). The issue with this is that during load it applies this saved data. causing what appears to be random sorting.

    Could you please report a bug about this?
     
  8. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Sure thing, though would this be better submitted as a feature request or a bug? Thanks for the info.

    Edit:
    I submitted it as a bug and as a feature request.
     
    Last edited: Aug 26, 2014
  9. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    I've got a scene where child objects reorder themselves each time I save and reopen a scene. I have a GameObject (NOT prefab) with 11 model-prefab children. I set the order through an editor script. I feel I'm doing this correctly according to the new system (code below). After I reorder them I use another script to iterate through the children from beginning to end and give me their name and Sibling Index (code below) ... And I get what I expect:

    #[0,1,2,3,4,5,6,7,8,9,10]

    However, if I save the scene, leave, and come back, then child[0] will have changed position and become child[10].

    #[1,2,3,4,5,6,7,8,9,10,0]

    If I save THAT scene, close the scene, and reopen the the order will have shifted by 1 again.

    #[2,3,4,5,6,7,8,9,10,0,1]

    Sometimes it'll jump by 2. But the order will always remain. It just gets offset.

    Here's our ordering code**:

    Code (CSharp):
    1. GameObject activeObj = null;
    2. if (Selection.gameObjects.Length > 0)
    3. {
    4.     activeObj = (GameObject)Selection.activeObject;
    5. }
    6.  
    7. List<GameObject> childList = new List<GameObject>();
    8. List<Vector3> childListScales = new List<Vector3>();
    9. int numChildrenReordered = 0;
    10.  
    11. if (activeObj)
    12. {
    13.     if (EditorUtility.DisplayDialog("Re-Order Children of Selection", "\"" + ToolName + "\" will reorder all the children of the Scene GameObject, \"" + activeObj.name + "\".\nContinue?", "Continue"))
    14.     {
    15.         for (int i = activeObj.transform.childCount - 1; i > -1; i--)
    16.         {
    17.             GameObject child = activeObj.transform.GetChild(i).gameObject;
    18.             childList.Add(child);
    19.             childListScales.Add(child.transform.localScale);
    20.             child.transform.parent = null;
    21.         }
    22.  
    23.         List<GameObject> orderedChildList = childList.OrderBy(go => go.name).ToList();
    24.  
    25.         for (int i = 0; i < orderedChildList.Count; i++)
    26.         {
    27.             orderedChildList[i].transform.parent = activeObj.transform;
    28.             Debug.Log(orderedChildList[i].name + " order = " + orderedChildList[i].transform.GetSiblingIndex());
    29.             orderedChildList[i].transform.SetAsLastSibling();
    30.             orderedChildList[i].transform.localScale = childListScales[i];
    31.             numChildrenReordered++;
    32.         }
    33.     }
    34. }
    And here's the code** we use to iterate through the heirarchy and get the transform index:

    Code (CSharp):
    1. GameObject activeObj = null;
    2. if (Selection.gameObjects.Length > 0)
    3. {
    4.     activeObj = (GameObject)Selection.activeObject;
    5. }
    6.  
    7. if (activeObj)
    8. {
    9.     for (int i = 0; i < activeObj.transform.childCount; i++)
    10.     {
    11.         Transform child = activeObj.transform.GetChild(i);
    12.         Debug.Log(child.gameObject.name + " order = " + child.GetSiblingIndex());
    13.     }
    14. }
    **NOTE: to get to the point I leave out some details in those scripts. Such as the fact that those 2 sets of code are each wrapped inside their own functions that are tied to the unity editor's drop down menus and those functions are wrapped inside one public static class and the script is using UnityEngine, UnityEditor, System.Collections.Generic, and System.Linq.
     
    Last edited: Aug 27, 2014
  10. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    I did some more tests today. It's prefabs in general - .fbx, .prefab, etc. If you have a bunch of prefab children under a GameObject, you can't control their transform order. Save a scene and reopen it, the index order will shift by one or two. Any thoughts on this guys?
     
  11. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
  13. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    Thanks Phil. Please let me know what you find. Or if the bug becomes public. I'd like to vote for it.
     
  14. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
  15. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    So you dont think i've forgotten about you guys, i am still taking a look at these in along side all the but for the new UI system.
     
  16. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
  17. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yea i was thinking yours had been fixed but wasn't ready to commit to it being fixed and marking it as such.

    Ben's issue i know for a fact should still be a issue. If its magically fixed i'd be really surprised and very happy :).
     
  18. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    No magic here. I'm using 4.5.3f3, and it's still here.
     
  19. mboudreau

    mboudreau

    Joined:
    Feb 26, 2013
    Posts:
    1
    I'm at a lost here, from a level design workflow perspective this hierarchy makes no practical sense whatsoever. I keep a clean naming convention to group all my gameplay elements in categories (since we can't make proper folders in unity hierarchy) but this is an order that I can't possibly work with. At the very least give us proper file sorting options like windows has been doing since the '80. Anyway, gotta be more careful than that guys, this is a core tool of the game dev workflow when working on large projects and it's easy to note why this is not working if you aim for one size fits all.
     
  20. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    The idea was to give users control over the child order (the order the children will have within their parent's transform array). The community has been begging for this control for a while. And now it's even more important because of the dependency the new gui system has on child order. For the time being, if you want to be able to switch between child order and alphabetical order, then add the following script into your project.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. #if UNITY_4_5
    5.  
    6. public class AlphaNumericSort : BaseHierarchySort
    7. {
    8.     public override int Compare(GameObject lhs, GameObject rhs)
    9.     {
    10.         if (lhs == rhs) return 0;
    11.         if (lhs == null) return -1;
    12.         if (rhs == null) return 1;
    13.         return EditorUtility.NaturalCompare(lhs.name, rhs.name);
    14.     }
    15. }
    16.  
    17. #endif
    You'll notice a new icon on the upper right of your scene heirarchy window.
     
    rakkarage likes this.
  21. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    As a FYI that script is no longer needed as of 4.5.2 (i think) In Editor preferences there is now a toggle where you can by default enable the alphanumeric sorting
     
    VatelsRevenge likes this.
  22. VatelsRevenge

    VatelsRevenge

    Joined:
    Feb 10, 2012
    Posts:
    14
    Nice! That fixes that.
     
  23. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    I made an editor extension called the Hierarchy Organizer to help with this very issue. I think this is superior to going back to the old autosort view, because it gives you the best of both worlds (automatic & manual sorting) and is my preferred way to work in Unity now.

    Can you not use empty game objects to group them? That's basically the equivalent of a "folder" in Unity.
     
    Last edited: Sep 11, 2014
  24. Amazing_Bluie

    Amazing_Bluie

    Joined:
    Oct 11, 2014
    Posts:
    2
    So, I have this project I'm working on with multiple UI elements. I want to hit a key and have one of them show on top of all the others. Considering that I cannot move items on the hierarchy from code (to my knowledge), does anyone have a recommendation?
     
  25. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can easily move items in the hierarchy with code by using Transform.SetSiblingIndex.

    --Eric
     
  26. Amazing_Bluie

    Amazing_Bluie

    Joined:
    Oct 11, 2014
    Posts:
    2
    Thank you very much! I didn't see that anywhere.
     
  27. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
  28. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    nope no solution for it for 4.6 is it a majour blocking issue for you or just an annoyance?
     
  29. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    For me it's an annoyance, as I can just write a script and attach it to each of my UI roots to sort them myself, but for other non-programming Unity developers this may be blocking. If it's a low priority for Unity, maybe once I'm done with my script I'll try to generalize it so others can grab it from the wiki.
     
  30. fractiv

    fractiv

    Joined:
    May 24, 2013
    Posts:
    3
    Custom sorting is useful, but the execution on Unity's part was really poor.

    Firstly, projects upgraded from 3->4 should have their transforms sorted alphanumerically as part of the upgrade process.

    Secondly, objects duplicated via Ctrl+D in the editor should have their sibling index set automatically so they appear next to the original object. Barring that, the hierarchy window should automatically scroll to the bottom. Why is it necessary to manually scroll to the bottom of the window every time I duplicate something? That's just silly.

    Thirdly, a menu or right click option should be provided to sort the current selection alphanumerically. It should be an obvious statement that not everything people work on is GUI related, and sometimes alphanumeric sorting is the most useful way to organize a group of objects.
     
  31. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It does, or at least I've never seen it not scroll automatically.

    --Eric
     
  32. fractiv

    fractiv

    Joined:
    May 24, 2013
    Posts:
    3
    I assumed it never scrolled, as I only started using the transform sort today and every time I tried duplicating an object it didn't scroll. Now that I've been playing with it some more, I realize it does scroll, but not all the time. There appears to be no rhyme or reason to whether or not it decides to scroll. I've tried this in my existing project and in a new project, same behavior. So this appears to be a unity editor bug.
     
  33. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    158
    I wrote a script that does more or less this - except that it also works for UI objects that are children of other UI objects. I take advantage of the fact that UI objects have all a CanvasRenderer component:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class HybridSort : BaseHierarchySort
    6. {
    7.     public override int Compare(GameObject lhs, GameObject rhs)
    8.     {
    9.         if (lhs == rhs) return 0;
    10.         if (lhs == null) return -1;
    11.         if (rhs == null) return 1;
    12.  
    13.         // We detect UI objects by the fact that they have a CanvasRenderer component
    14.         if (lhs.GetComponent<CanvasRenderer>() != null || rhs.GetComponent<CanvasRenderer>() != null)
    15.         {
    16.             // UI objects are sorted by sibling index (aka "Transform index"), like in TransformSort
    17.             return (lhs.transform.GetSiblingIndex() < rhs.transform.GetSiblingIndex()) ? -1 : 1;
    18.         }
    19.         else
    20.         {
    21.             // Everything else is rendered alphabetically
    22.             return EditorUtility.NaturalCompare(lhs.name, rhs.name);
    23.         }
    24.     }
    25. }
    26.  
     
    Abaobao and Rodolfo-Rubens like this.
  34. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Hey, this works like a charm! Thank you very much!

    edit: Except that if I want to reorder UI objects I need to go back to TransformSort...
     
  35. hjacobsdesign

    hjacobsdesign

    Joined:
    Aug 4, 2014
    Posts:
    2
    I'm experiencing the same thing. Makes it really hard to find objects as they keep reordering themselves between sessions.
     
  36. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yeah, that's a problem. If you have a project where you can reproduce this bug reliably, you should submit it as a bug report.
     
  37. mikelortega

    mikelortega

    Joined:
    Feb 5, 2014
    Posts:
    47
    I'm having the same problem with Unity 5.2.3f1. Cannot order objects in hierarchy.
    I think the issue 674553 is somehow related: Scene m_RootOrder being saved as a prefabmodification within the scene file has an incorrect value
    Upvote if you experience the same.
     
    Last edited: Nov 24, 2015
  38. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    This has long ago been fixed. In preferences enable hierarchy sorting options and turn on alpha sort. Done.
     
  39. mikelortega

    mikelortega

    Joined:
    Feb 5, 2014
    Posts:
    47
    Yes, that works if you want your scene alphabetically sorted, but not if you want to customize your own order.
     
  40. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    137
    I'm also still experiencing sorting of objects that is reset on Play, I'm using Unity 5.2.2f1.
    On play, the order of some objects is changed. When stopping playback the reset order persist.
    I'll try to make a repro scene and fill a bug.
     
  41. mikelortega

    mikelortega

    Joined:
    Feb 5, 2014
    Posts:
    47
    Hi @Tortuap, the issue 674553 says it will be fixed in Unity 5.4, I think I wouldn't bother filling a bug.
     
    jwinn likes this.
  42. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    302
    As soon I started using prefabs I noticed everything in my hierachy window is auto sorting it self and never remembering anything...
     
  43. Calos1591

    Calos1591

    Joined:
    Aug 13, 2012
    Posts:
    5
    Enable Alpha Numeric Sorting
     

    Attached Files: