Changing the order of Components
In some cases the order affects the end result. For instance when stacking a blur on a noise, or the other way around. Does Unity have an ability to change the order? I've been looking around in the docs and Google, but couldn't find it. I was even a bit surprised that I couldn't find anything :)
Answer by pigzlz · Nov 13, 2012 at 11:15 PM
Since Unity 4, you can move components up and down by right-clicking on them (or clicking on the small gear in the right corner) and selecting the appropriate option in the context menu that is popping up.
Can we do this programmatically? Like sorting components by a property value?
There is an option (only in the editor), though it is not documented:
UnityEditorInternal.ComponentUtility.$$anonymous$$oveComponentDown(Component);
UnityEditorInternal.ComponentUtility.$$anonymous$$oveComponentUp(Component);
There are also other functions in there, for copying and pasting components.
Thanks, this what useful for me, though it is a pain in the ass when you have dozens of components and need to move the last one among the firsts. Now, that's usability, indeed...
Answer by dyankov · Jun 09, 2014 at 09:51 AM
New plugin on the Asset Store, doing just what you need with a drag and drop interface:
Answer by Eran-Yaacobi · Oct 11, 2015 at 05:22 PM
Posting this as an answer (instead of as a reply to a somewhat different answer), along with an example for an editor script that can be used to re-order components by type.
There is an option to sort components programmatically (only in the editor), though it is not documented:
UnityEditorInternal.ComponentUtility.MoveComponentDown(Component); UnityEditorInternal.ComponentUtility.MoveComponentUp(Component);
There are also other functions in there, for copying and pasting components.
Here is an example for a useful script which provides a menu item (shortcut alt+ctrl+a) that sorts the components in the currently selected game-object according to type:
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEngine.Networking;
public class ComponentsSorter : ScriptableObject
{
private class ComponentComparer : IComparer<Component>
{
private static readonly Type[] TypesOrder =
{
// Transform is always first (though that doesn't really matter, as we can't
// move it anyway).
typeof (Transform),
// Add your types here in the order you want them to be in the inspector.
typeof (MeshFilter),
typeof (MeshRenderer),
typeof (SpriteRenderer),
typeof (Rigidbody2D),
typeof (Collider2D),
};
private Int32 GetIndex(Component Component)
{
var Type = Component.GetType();
Type BestMatch = typeof(UnityEngine.Object);
var BestIndex = Int32.MaxValue;
for (int Index = 0; Index < TypesOrder.Length; Index++)
{
// If we found the exact type in the list, then this is the right index.
var TypeOrder = TypesOrder[Index];
if (Type == TypeOrder)
return Index;
// If we found a parent, then we switch to its place if it is more
// "recent" (in the inheritance tree) than previously found parents.
if (Type.IsSubclassOf(TypeOrder))
{
if (TypeOrder.IsSubclassOf(BestMatch))
{
BestMatch = TypeOrder;
BestIndex = Index;
}
}
}
return BestIndex;
}
public int Compare(Component First, Component Second)
{
return Comparer<Int32>.Default.Compare(GetIndex(First), GetIndex(Second));
}
}
[MenuItem("Edit/Sort Components %&a")]
private static void SortComponents()
{
var GameObject = Selection.activeGameObject;
var SortedComponents = GameObject.GetComponents<Component>()
.Where(Component => Component.GetType() != typeof(Transform)).ToList();
SortedComponents.Sort(new ComponentComparer());
for (var Index = 0; Index < SortedComponents.Count; Index++)
{
var SortedComponent = SortedComponents[Index];
var Components = GameObject.GetComponents<Component>()
.Where(Component => Component.GetType() != typeof (Transform)).ToList();
var CurrentIndex = Components.IndexOf(SortedComponent);
if (CurrentIndex < Index)
{
for (var MoveIndex = CurrentIndex; MoveIndex < Index; MoveIndex++)
UnityEditorInternal.ComponentUtility.MoveComponentDown(SortedComponent);
}
else
{
for (var MoveIndex = CurrentIndex; MoveIndex > Index; MoveIndex--)
UnityEditorInternal.ComponentUtility.MoveComponentUp(SortedComponent);
}
}
}
}
The code is a little ugly, but it works. Just add it under an 'Editor' folder and it should appear under the 'Edit' menu.
Answer by ransomink · Jan 15, 2018 at 02:02 AM
Now, you can simply click & drag a component to change its order...
Answer by CiberX15 · Oct 11, 2015 at 04:25 PM
Actually You can do this now. I'm not sure about previous Unity versions but in Unity 5.2 you can right click the component and select "Move Up" or "Move Down" to move the component wherever you need it.