How to load SubScene correctly?

Hi!
I am trying to load SubScene using the code below

protected async Task PrepareScene()
{
	
	SubScene[] subScenes = FindObjectsByType<SubScene>(FindObjectsInactive.Include, FindObjectsSortMode.None);
	await Msg($"subScenes:{subScenes.Length}");

	SceneSystem.LoadParameters loadParameters = new() { Flags = SceneLoadFlags.BlockOnStreamIn };


	for (int i = 0; i < subScenes.Length; i++)
	{
		Entity sceneEntity = SceneSystem.LoadSceneAsync(SideWorld.Unmanaged,
			new Entities.Hash128(subScenes[i].SceneGUID.Value), loadParameters);

		while (!SceneSystem.IsSceneLoaded(SideWorld.Unmanaged, sceneEntity))
		{
			await Msg($"Loading...:{i} [{subScenes[i].SceneName}]");

			SideWorld.Update();
			await Awaitable.NextFrameAsync();
		}

		await Msg($"Loaded:{i} [{subScenes[i].SceneName}]");
	}
}

But despite the messages about the loaded scene, the scene is still marked as not loaded in the Entity inspector, besides, its contents are not rendered in the camera view:


Does anyone have any ideas?
Thanks!

UPD 7:58
We managed to solve this by uploading the scene to both ServerWorld and Client World, although in the Entity inspector the NewSubScene is still displayed as not loaded in both worlds. I know I’m doing something weird, but does anyone have any ideas on how to do it right? Thanks!

I also quite often get similar errors when I try to launch PlayMode, which also disappear unexpectedly the next time I launch PlayMode


Has anyone encountered anything like this?

At the moment, I’m solving this by restarting the unity editor

UPD 7:50
It was possible to solve the problem with a try-catch wrapper.

SubScenes probably don’t work how you think - you can load the same subscene multiple times into the same world if you want. (Useful for loading per user dungeons in for a online multiplayer game for example.) So when you load a SubScene by code it doesn’t belong to any particular SubScene script, it just exists.

If you want to link it to a particular SubScene script, so it appears open in the hierarchy etc, you need to attach the SubScene script to the entity itself.

The secret is this line in SubScene.cs

var sceneEntity = SceneSystem.LoadSceneAsync(world.Unmanaged, _SceneGUID, loadParams);
EntityManager.AddComponentObject(sceneEntity, this)

this basically tells the SubScene inspector what entity the SubScene is linked to and that is the state it will show.

2 Likes

@ankofl_dev SceneLoadFlags.BlockOnStreamIn is currently broken see. ECSB-1434

1 Like