With the release of Unity 4.5 it has a new Hierarchy Window sorting which seems to sort “based on transform order instead of name”.
I was wondering if someone might be able to clarify what transform order sorting means exactly as that description seems a bit vague and could mean any number of things?
I also was wondering if there is an option to use the previous legacy sorting by name as currently the new system seems to disrupt the work flow of the project I’m currently working on due to our reliance on the name sorting.
So it looks like Unity 4.5 has new functionality that allows us the overload how the hierarchy window sorts gameobjects in code using BaseHierarchySort which is mentioned here:
Though I’m still unclear on how to implement it. Do we put our derived class in an Editor folder, or some other folder? Is there anything extra to set it up?
The “upgrade guide” at the bottom of this page links to this docs page which gets you most of the way there. I don’t see a way to actually set this class to take effect, which would be a good thing to add to the docs…
“based on transform order instead of name” means that you can order your objects around in anyway you want, and this is the new default behavior we are shipping with in 4.5.
As you saw on the Scripting Docs you can create custom sorting for the Hierarchy as well. In that page you can see an example to bring back the old behavior of alpha numeric sorting.
To make it work, just create the script inside the Editor folder, then you should see a new Icon at the top right of the Hierarchy Window.
The custom sorting is a really cool feature, though it might help if you guys have a tutorial video or explicitly state how to set this up for people who aren’t aware of this. I only realized how to do this because I was part of the Beta and I checked in those forums. Thanks for the response.
I think the upgrade guide can be easily missed, but hopefully people find this forum thread if they do miss it.
EDIT: I see, you have to write an editor script to do that. It’s a shame, I suppose, that the order got moved about on the upgrade,rather than starting out as alphabetical.
Read his post more carefully - that button appears only when you have another kind of sort somewhere in the project (it doesn’t have to be in an editor folder, either).
Here, I’ve put the sample code into a script file for script-averse types. Just download the attached script and drop it anywhere in your project. It’ll compile, that new button will appear, and you can choose the AlphaNumericSort.
There is a good point of feedback here, though - Unity’s Standard Assets should probably be updated to include AlphaNumericSort.
I for one am going to find this feature incredibly useful. Now I can sort objects based on how far through the level they are, for example, which is going to be really handy when laying out levels.
Count me among the nonplussed: the presentation of this new functionality is completely lame. The benefit of custom sorting isn’t offset by the annoyance of having to write a script to restore the previous behavior.
One of the great things about Unity is how it empowers non-coders to create games. Defaulting to transform sorting (whatever that is) without an easy way to select alphabetical sorting is a real head-scratcher. User-unfriendly and tedious to undo.
I’m sure many will appreciate the ability to create custom sorting, but it shouldn’t have defaulted to a poorly-explained default without an easy way to preserve the kind of sorting that pretty much everyone is used to.
StarManta, thanks for providing a script, by the way!
What does this actually mean? My hierarchy is reordered but I do not understand how this relates to the transform order. I want my gameObjects to be ordered based upon their “z”, it is not doing that.
So, for preparation to the new GUI system, we are now saving the index of each transform in relation to it’s parent. So the default ordering is just using this index to order the Game Objects. That means you now have two options: you can manually re-order items in your hierarchy for your own needs by dragging them around and using the default sorting in the hierarchy, or you can write a script and have the script do the order in the hierarchy for you.
We understand that it’s a confusing change for people used to the way it used to work, but unfortunately is a change that is needed going forward.
For now no system in Unity is using the transform index YET, GUI will be the first one to make use of this feature. But the API is fully exposed, so extension developers can make use of this feature for their own needs as well.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class AlphaNumericSort : BaseHierarchySort
{
public override int Compare(GameObject lhs, GameObject rhs)
{
if (lhs == rhs) return 0;
if (lhs == null) return -1;
if (rhs == null) return 1;
if (lhs.transform.position.z > rhs.transform.position.z) return 1;
return -1;
}
}
Note that while you can place this script anywhere in your project and it will work in the editor, it will probably throw some errors when building your game. So I would recommend putting those scripts inside a Editor folder.
I think just the notion of “Transform order instead of name” was really a bad way to say it, as transform order can mean a lot of things to different people.
Now when you explained it, I know you meant the order in the array.
I actually find this a welcome change. The ability to re-order is a nice feature.
Regarding manual reordering, it seems to be ok when loading and unloading gameobjects from scenes, but where it really seems to have an issue is when you LoadAdditive. Is there a way to control how the new gameobjects that come into the scene can be sorted by transform?
using UnityEngine;
using System.Collections;
using UnityEditor;
#if UNITY_4_5
public class AlphaNumericSort : BaseHierarchySort
{
public override int Compare(GameObject lhs, GameObject rhs)
{
if (lhs == rhs) return 0;
if (lhs == null) return -1;
if (rhs == null) return 1;
return EditorUtility.NaturalCompare(lhs.name, rhs.name);
}
}
#endif
Manual sort is great, maybe two lines to explain what “transform sort” is, it would be great for new users.
I’m going to have to agree with OneThree. While Unity may have good reasons to sort it in this manner, from my point of view it just went from “sorted” to “not sorted”. Whatever nice things are happening on the back end because of it, it has the appearance of taking a step backward in functionality.
Having other sorting options is fine, but please include some defaults.