ECS Subscene add gameobject via code on editor

Hello there, I am working on some amazing tools for developers that could be use on editor mode so I wanted them to have really good performance so i decided to use ECS.
The problem i have its that as u know today people still use gameobjects so i have to convert certain gameobject on entities at editor mode running (not at runtime). I read that it could be possible to use subscene as a method that convert gameobjects on entities not at runtime. I was reading some docs but there are not much info so I prefer to ask u direct.

Then, my problem is that i have to convert gameobject that are on a scene on entities on editor but i dont want developer to convert them on his own. I just want him to give me a list of the gameobject he want to convert.
Maybe u are asking what happen with certain gameobjects component that not have direct conversion. I only want certain gameobject components so maybe u can say me other method to use.

I hope u can understand me cause my english is not enought good. This is only a test.

Finally , i will write here some code i wrote before. Maybe that could help u understand what i am trying.

Scene s = subsceneGameObject.GetComponent<SubScene>().EditingScene;
            SceneManager.SetActiveScene(s);

            GameObject a = new GameObject("SubsceneGameObject");
            Undo.RegisterCreatedObjectUndo(a, "Created SubsceneGameObject");
2 Likes

Did you ever figure this out? I have an editor tool and when the developer presses a button i want it to create a gameobject and add it to a subscene. I tried setting the parent transform on instantiation, but it complains:

So there must be some other way to add a game-object to a subscene via an editor tool??

1 Like

Found it!

GameObject instantiatedGameObj = PrefabUtility.InstantiatePrefab(myPrefab) as GameObject;
UnityEditor.SceneManagement.EditorSceneManager.MoveGameObjectToScene(instantiatedGameObj, subScene.EditingScene);
UnityEditor.SceneManagement.EditorSceneManager.SaveScene(subScene.EditingScene);
4 Likes

In case anyone is wondering… this took me 5 hours to figure out.

The MoveGameObjectToScene() function will fail if the subscene is not loaded. In those situations, you need to programmatically open it by calling:

UnityEditor.SceneManagement.EditorSceneManager.OpenScene(m_subScene.EditableScenePath, UnityEditor.SceneManagement.OpenSceneMode.Additive);
This is basically the equivalent of clicking the checkbox next to the subscene. The Additive flag is very important.

And then you can close it again (if you want) via:

UnityEditor.SceneManagement.EditorSceneManager.CloseScene(m_subScene.EditingScene, true);

The true parameter is important (it doesn’t truely delete the scene despite what the name implies).

Sidenote - you don’t need LoadSceneAsync at all… I think this is a runtime-only thing and has nothing to do with the editor.
Another sidenote - mySubScene.transform.Find(“GameObjectName”) fails all the time on subscenes. I don’t know why.

3 Likes