Last active
January 24, 2022 05:08
-
-
Save shivaduke28/b11e6d620a7ac21fc98ca0e605a39581 to your computer and use it in GitHub Desktop.
Create Object from MenuItem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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