Search Unity

Question How to get scene GUID and load scene in Entities 1.0.0

Discussion in 'Entity Component System' started by EmmaPrats, Dec 19, 2022.

  1. EmmaPrats

    EmmaPrats

    Joined:
    Apr 19, 2019
    Posts:
    8
    I have been trying to migrate a project from Entities 0.51.0 to Entities 1.0.0, and I'm having trouble with scene loading.

    This is how I used to load and unload the game subscene in Entities 0.51.0:

    Code (CSharp):
    1.     public static class SceneManager
    2.     {
    3.         public const string GAME_SUBSCENE_NAME = "Assets/Path/To/Gameplay Subscene.unity";
    4.  
    5.         public static async Task LoadGameScene()
    6.         {
    7.             var sceneSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<SceneSystem>();
    8.             var guid = sceneSystem.GetSceneGUID(GAME_SUBSCENE_NAME);
    9.             sceneSystem.LoadSceneAsync(guid);
    10.  
    11.             await Task.Delay(100);
    12.         }
    13.  
    14.         public static async Task UnloadGameScene()
    15.         {
    16.             var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    17.             var allEntities = entityManager.GetAllEntities();
    18.             foreach (var entity in allEntities)
    19.             {
    20.                 if (entityManager.HasComponent<RequestSceneLoaded>(entity))
    21.                     entityManager.RemoveComponent<RequestSceneLoaded>(entity);
    22.             }
    23.             await Task.Delay(500);
    24.         }
    25.     }
    I think I need to replace
    sceneSystem.LoadSceneAsync(guid);
    with
    SceneSystem.LoadSceneAsync(World.DefaultGameObjectInjectionWorld, guid);


    And then, use
    SceneSystem's
    static method

    public static unsafe Hash128 GetSceneGUID(ref SystemState state, string scenePath)


    But it expects a
    SystemState
    parameter. How do I get it from a regular static class that is not a
    SystemBase
    or
    ISystem
    ?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    upload_2022-12-19_10-42-41.png

    In your case
    upload_2022-12-19_10-43-46.png

    But as I highlighted - it's slow.
    Proper usage is bake scenes GUIDs
    upload_2022-12-19_10-44-33.png

    Or having some MonoBehaviour referencing SubScene which contains public SceneGUID property
    upload_2022-12-19_10-45-17.png
    upload_2022-12-19_10-45-30.png
     
    heartingNinja likes this.
  3. EmmaPrats

    EmmaPrats

    Joined:
    Apr 19, 2019
    Posts:
    8
    Thank you!