Skip to content

Instantly share code, notes, and snippets.

@SolidAlloy
Created April 28, 2022 12:47
Show Gist options
  • Save SolidAlloy/4cfd24938ac5416be5c4a39f9b93e2c6 to your computer and use it in GitHub Desktop.
Save SolidAlloy/4cfd24938ac5416be5c4a39f9b93e2c6 to your computer and use it in GitHub Desktop.
"Cut Component" context menu that copies the component data and removes the component from the source object in one click.
namespace CutComponent.Editor
{
using UnityEditor;
using UnityEngine;
public static class CutComponentMenuItem
{
private const string CutComponentPath = "CONTEXT/Component/Cut Component";
[MenuItem(CutComponentPath, priority = 500)]
private static void CutComponent(MenuCommand command)
{
var sourceComponent = (Component) command.context;
UnityEditorInternal.ComponentUtility.CopyComponent(sourceComponent);
Undo.DestroyObjectImmediate(sourceComponent);
}
[MenuItem(CutComponentPath, validate = true)]
private static bool ValidateCutComponent(MenuCommand command)
{
return command.context is not Transform;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment