Skip to content

Instantly share code, notes, and snippets.

@Nesh108
Created April 17, 2020 14:37
Show Gist options
  • Save Nesh108/11a356e3c2f81267dbe4559ca468c287 to your computer and use it in GitHub Desktop.
Save Nesh108/11a356e3c2f81267dbe4559ca468c287 to your computer and use it in GitHub Desktop.
Unity - How to create custom menu for Components
using UnityEditor;
using UnityEngine;
public class CustomComponentMenu : MonoBehaviour
{
// Object is for any component type
[MenuItem("CONTEXT/Object/Move Top")]
static void MoveTop(MenuCommand command)
{
// Making sure it goes to the top
for (int i = 0; i < 30; i++)
{
UnityEditorInternal.ComponentUtility.MoveComponentUp((Component)command.context);
}
}
[MenuItem("CONTEXT/Object/Move Bottom")]
static void MoveBottom(MenuCommand command)
{
// Making sure it goes to the bottom
for (int i = 0; i < 30; i++)
{
UnityEditorInternal.ComponentUtility.MoveComponentDown((Component)command.context);
}
}
// Specifically for Transform components only
[MenuItem("CONTEXT/Transform/Teleport To Player")]
static void TeleportToPlayer(MenuCommand command)
{
((Component)command.context).transform.position = FindObjectOfType<Player>().transform.position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment