- Home /
Question by
mred-tcg · Apr 26, 2018 at 02:11 PM ·
editorscenemenuitemcontextmenu
Determine the Scene that is selected when using context menu
I have a context menu that is used to create a game object.
If I select a game object in the scene hierarchy and access the context menu I get a valid MenuCommand.context.
If I select a scene in the hierarchy and access the context menu the MenuCommand.context is null.
The same goes for Selection.activeContext, Selection.activeObject, etc.
However, if I call new GameObject() then it is created in the selected scene.
How does new GameObject() get the selected scene?
[UnityEditor.MenuItem( "GameObject/MyGameObject", false, 10 )]
static void CreateMyGameObject( UnityEditor.MenuCommand menuCommand )
{
var parent = menuCommand.context; // <-- null
var activeContext = UnityEditor.Selection.activeContext // <-- null
var activeContext = UnityEditor.Selection.activeObject // <-- null
var activeScene = SceneManager.activeScene // <-- not necessarily the selected scene
var realScene = new GameObject().scene; // <-- the scene that was selected in the hierarchy!
}
Comment