I found this script a while ago and it put a smile on my face… hopefully it will for you to.
I think it was from here https://www.assetstore.unity3d.com/en/#!/content/18393 an asset called “Reorder My Components” which is free.
I have edited the script I am posting below, so download the asset above if you want the original. I applied the few bug fixes the people in the asset comments gave, as well as changing the GUI a bit (such as adding a scroll). I think the gui needs to be better, but I dont really know gui stuff, so maybe someone can help with that. I would have also liked if instead of going to “Window → ReorderComponents” to open the window, if there is a way to have a button right over the inspectors “Add Component” Button or something that would open this window, or better yet, not need to even open a window and just click and drag the components themselves.
I also think I ran into a few bugs and what not, but overall it was a huge time saver.
I have not used it a lot though, so I dont know if there are any scary bugs such as stuff getting deleted or something, so I guess use at your own risk, but I think there shouldnt be issues like that.
The script must be put inside a folder called Editor in your project.
To use it, have your gameobject selected and click on “Window → ReorderComponents”
There, you can click on those button looking things with your components name on them and drag them to where you want them.
Click for code
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
[ExecuteInEditMode]
public class ReorderComponents : EditorWindow
{
int activeButton;
int tempButtonIndex;
int lastButtonIndex;
bool mouseDown;
int[] newIndexes;
Texture2D buttonActiveBackground = Texture2D.blackTexture;
public Vector2 scrollPosition = Vector2.zero;
[MenuItem("Window/Reorder Components")]
private static void ShowWindow()
{
ReorderComponents windowHndle = (ReorderComponents)EditorWindow.GetWindow(typeof(ReorderComponents));
windowHndle.autoRepaintOnSceneChange = true;
}
void OnInspectorUpdate()
{
Repaint();
}
void OnGUI()
{
//This allows you to work on prefabs that are in the project view, as well as prevents error of clicking non gameobject
Transform currentTransform;
if(Selection.GetFiltered(typeof(Transform), SelectionMode.Unfiltered) != null && Selection.GetFiltered(typeof(Transform), SelectionMode.Unfiltered).Length > 0)
{
currentTransform = (Transform)Selection.GetFiltered(typeof(Transform), SelectionMode.Unfiltered)[0];
}else{
currentTransform = Selection.activeTransform;
}
if(currentTransform != null)
{
Component[] comps = currentTransform.GetComponents<Component>();
Rect currentWindowRect = EditorWindow.GetWindow(typeof(ReorderComponents)).position;
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUIStyle.none, GUI.skin.verticalScrollbar, GUILayout.Height(currentWindowRect.height));
#region graphic templates
//buttonActiveBackground.SetPixel(0, 0, Color.gray);
//buttonActiveBackground.Apply();
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
buttonStyle.alignment = TextAnchor.MiddleLeft;
buttonStyle.fixedHeight = 25;
buttonStyle.margin = new RectOffset (4, 4, 4, 4);
GUIStyle buttonDisabledStyle = new GUIStyle(GUI.skin.button);
buttonDisabledStyle.alignment = TextAnchor.MiddleLeft;
buttonDisabledStyle.fixedHeight = 25;
buttonDisabledStyle.margin = new RectOffset (4, 4, 4, 4);
buttonDisabledStyle.normal.textColor = Color.gray;
GUIStyle buttonActiveStyle = new GUIStyle();
buttonActiveStyle.alignment = TextAnchor.MiddleLeft;
buttonActiveStyle.fixedHeight = 25;
buttonActiveStyle.margin = new RectOffset (4, 4, 4, 4);
buttonActiveStyle.normal.background = buttonActiveBackground;
buttonActiveStyle.normal.textColor = Color.gray;
#endregion
// Check if mouse button gets pressed down and see which button is below mouse
if(!mouseDown && Event.current.type == EventType.MouseDown)
{
activeButton = Mathf.FloorToInt(Event.current.mousePosition.y / 29);
if(comps[activeButton].GetType().ToString() == "UnityEngine.Transform")
{
mouseDown = false;
}else{
mouseDown = true;
lastButtonIndex = activeButton;
newIndexes = new int[comps.Length];
for(int i=0; i<comps.Length; i++)
{
newIndexes[i] = i;
}
}
}
// Mouse button released
if(mouseDown && Event.current.type == EventType.MouseUp)
{
mouseDown = false;
// Reorder components
int positionsToMove = Mathf.RoundToInt(Mathf.Abs(tempButtonIndex - activeButton));
if(positionsToMove > 0)
{
int direction = (tempButtonIndex - activeButton) / Mathf.Abs(tempButtonIndex - activeButton);
for(int i=0; i<positionsToMove; i++)
{
if(direction > 0)
{
UnityEditorInternal.ComponentUtility.MoveComponentDown(comps[activeButton + i]);
}
if(direction < 0)
{
UnityEditorInternal.ComponentUtility.MoveComponentUp(comps[activeButton - i]);
}
comps = currentTransform.GetComponents<Component>();
}
}
}
// Draw buttons
for(int i=0; i<comps.Length; i++)
{
int j = i;
if(mouseDown)
{
j = newIndexes[i];
}
string componentName = comps[j].GetType().ToString();
GUIStyle style = buttonStyle;
if(mouseDown && i == tempButtonIndex) {style = buttonActiveStyle;}
if(componentName == "UnityEngine.Transform") {style = buttonDisabledStyle;}
GUILayout.Button(componentName, style);
}
// If mouse button is down, draw the extra button
if(mouseDown)
{
Rect buttonPosition = new Rect(Event.current.mousePosition.x - Screen.width/2, Event.current.mousePosition.y - 12.5f, Screen.width - 10, 25);
GUI.Button(buttonPosition, comps[activeButton].GetType().ToString(), buttonStyle);
}
// Get index for the new temp button
if(mouseDown)
{
int tmp = Mathf.FloorToInt(Event.current.mousePosition.y / 29);
if(tmp > comps.Length - 1)
{
tmp = comps.Length - 1;
}
if(tmp < 0)
{
tmp = 0;
}
if(comps[tmp].GetType().ToString() != "UnityEngine.Transform")
{
tempButtonIndex = tmp;
}
if(tempButtonIndex != lastButtonIndex)
{
int temp = newIndexes[lastButtonIndex];
newIndexes[lastButtonIndex] = newIndexes[tempButtonIndex];
newIndexes[tempButtonIndex] = temp;
lastButtonIndex = tempButtonIndex;
}
}
GUI.EndScrollView();
}
}
}