Search Unity

ECS Subscene add gameobject via code on editor

Discussion in 'Entity Component System' started by lmml, May 6, 2020.

  1. lmml

    lmml

    Joined:
    May 2, 2020
    Posts:
    6
    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.

    Code (CSharp):
    1. Scene s = subsceneGameObject.GetComponent<SubScene>().EditingScene;
    2.             SceneManager.SetActiveScene(s);
    3.  
    4.             GameObject a = new GameObject("SubsceneGameObject");
    5.             Undo.RegisterCreatedObjectUndo(a, "Created SubsceneGameObject");
     
    Dabartos and lclemens like this.
  2. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    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:

    upload_2023-1-10_16-18-12.png

    So there must be some other way to add a game-object to a subscene via an editor tool??
     
    Dabartos likes this.
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    Found it!

    Code (CSharp):
    1. GameObject instantiatedGameObj = PrefabUtility.InstantiatePrefab(myPrefab) as GameObject;
    2. UnityEditor.SceneManagement.EditorSceneManager.MoveGameObjectToScene(instantiatedGameObj, subScene.EditingScene);
    3. UnityEditor.SceneManagement.EditorSceneManager.SaveScene(subScene.EditingScene);
    4.  
     
    Last edited: Jan 11, 2023
  4. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    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:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. 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.
     
    Dabartos, toomasio and apkdev like this.