Skip to content

Instantly share code, notes, and snippets.

@veyselerCS
Created April 18, 2023 11:08
Show Gist options
  • Save veyselerCS/78ca620388012db4ee606f5100169acf to your computer and use it in GitHub Desktop.
Save veyselerCS/78ca620388012db4ee606f5100169acf to your computer and use it in GitHub Desktop.
Unity Inspector Componenet Move To Top MenuItem
using UnityEditor;
using UnityEngine;
public class MoveToTopMenuItem : MonoBehaviour
{
[MenuItem("CONTEXT/Component/Move To Top")]
private static void MoveToTop(MenuCommand command)
{
Component component = command.context as Component;
if (component == null)
{
Debug.LogWarning("Component is null");
return;
}
var componentType = component.GetType();
var allComponents = component.transform.GetComponents<Component>();
var componentIndex = 0;
for (; componentIndex < allComponents.Length; componentIndex++)
{
var currentComponent = allComponents[componentIndex];
if (currentComponent.GetType() == componentType)
{
break;
}
}
var moveCount = componentIndex - 1;
for (int i = 0; i < moveCount; i++)
{
UnityEditorInternal.ComponentUtility.MoveComponentUp(component);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment