Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Last active January 24, 2022 05:08
Show Gist options
  • Save shivaduke28/b11e6d620a7ac21fc98ca0e605a39581 to your computer and use it in GitHub Desktop.
Save shivaduke28/b11e6d620a7ac21fc98ca0e605a39581 to your computer and use it in GitHub Desktop.
Create Object from MenuItem
[MenuItem("GameObject/Foo", false, -1)]
public static void CreateFoo(MenuCommand command = null)
{
// When called from MenuItem, this method is executed for every selected objects in the Hierarchy.
if (command != null)
{
var context = command.context;
if (context != null && context != Selection.activeObject) return;
}
var parents = Selection.transforms;
foreach(var parent in parents)
{
Create(parent);
}
if (parents.Length == 0)
{
Create(null);
}
void Create(Transform parent)
{
// Create what you want here.
var go = new GameObject("bar");
go.transform.SetParent(parent, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment