Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The manager doesn't unload a scene #48

Open
VirtualMaestro opened this issue Dec 21, 2024 · 7 comments
Open

The manager doesn't unload a scene #48

VirtualMaestro opened this issue Dec 21, 2024 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@VirtualMaestro
Copy link

Hey João,
I'm trying 3.2-Pre.1 version.
A scene is not unloaded. Here is an example:

private static readonly ILoadSceneInfo LoaderSceneInfo = new LoadSceneInfoName("Assets/Client/Scenes/Loader.unity");
private static readonly ILoadSceneInfo MainSceneInfo = new LoadSceneInfoName("Assets/Client/Scenes/Main.unity");

private ISceneManager _sceneManager;

void Start()
{
	_sceneManager = new AdvancedSceneManager(true);
}
... 2 seconds later
private void _OnConfigFileLoaded()
{
	_sceneManager.SceneLoaded += _OnSceneLoaded;
	_sceneManager.LoadSceneAsync(MainSceneInfo, true);
}

private void _OnSceneLoaded(Scene scene)
{
	_sceneManager.SceneUnloaded += _OnSceneUnloaded;
	_sceneManager.UnloadSceneAsync(LoaderSceneInfo);
}

private void _OnSceneUnloaded(Scene scene)
{
	Debug.LogWarning($"Scene unloaded: {scene.name}");
}

Here is a screenshot from the debugger to see that this scene is under the manager's control.

image

I hope this helps)
Thank you

@joaoborks
Copy link
Member

Hey @VirtualMaestro thank you again for your report.

This version is not stable yet, I'm working out on a new set of features towards v4, so these pre-releases might not fully work in the meanwhile. I'll keep you posted.

@joaoborks joaoborks added the bug Something isn't working label Dec 21, 2024
@joaoborks
Copy link
Member

The version 4.0.0-pre.1 has just been released. Please try this version and let me know if it works as you expected.
There are a some breaking changes in this version that I'll add the documentation later, but for now you should be able to update your code to something like this:

private static readonly string LoaderScenePath = "Assets/Client/Scenes/Loader.unity";
private static readonly string MainScenePath = "Assets/Client/Scenes/Main.unity";

private ISceneManager _sceneManager;

void Start()
{
	_sceneManager = new AdvancedSceneManager(true);
}

private void _OnConfigFileLoaded()
{
	_sceneManager.SceneLoaded += _OnSceneLoaded;
	_sceneManager.LoadAsync(MainScenePath, true);
}

private void _OnSceneLoaded(Scene scene)
{
	_sceneManager.SceneUnloaded += _OnSceneUnloaded;
	_sceneManager.UnloadAsync(LoaderScenePath);
}

private void _OnSceneUnloaded(Scene scene)
{
	Debug.LogWarning($"Scene unloaded: {scene.name}");
}

@VirtualMaestro
Copy link
Author

Hey João,
I'm testing the 4.0.0-pre.1. The same code as above, but 'UnloadAsync' still doesn't unload the 'LoaderScenePath' (Loader.scene is added to the build).
It says:
image

@joaoborks
Copy link
Member

🤔 I'll take a look at it.

@joaoborks
Copy link
Member

I've figured it out! This only happens because the LoaderScene is the first scene and it's added to the AdvancedSceneManager in its constructor by its sceneHandle (LoadSceneInfoScene) rather than its path. So, you'd need to unload it through the scene, rather than its path:

private static readonly string LoaderScenePath = "Assets/Client/Scenes/Loader.unity";
private static readonly string MainScenePath = "Assets/Client/Scenes/Main.unity";

private ISceneManager _sceneManager;

void Start()
{
	_sceneManager = new AdvancedSceneManager(true);
}

private void _OnConfigFileLoaded()
{
	_sceneManager.SceneLoaded += _OnSceneLoaded;
	_sceneManager.LoadAsync(MainScenePath, true);
}

private void _OnSceneLoaded(Scene scene)
{
	_sceneManager.SceneUnloaded += _OnSceneUnloaded;
	_sceneManager.UnloadAsync(SceneManager.GetSceneByPath(LoaderScenePath));
}

private void _OnSceneUnloaded(Scene scene)
{
	Debug.LogWarning($"Scene unloaded: {scene.name}");
}

You could also just transition to the target scene, that will also unload the loader scene internally:

private static readonly string MainScenePath = "Assets/Client/Scenes/Main.unity";

private ISceneManager _sceneManager;

void Start()
{
	_sceneManager = new AdvancedSceneManager(true);
}

private void _OnConfigFileLoaded()
{
	_sceneManager.TransitionAsync(MainScenePath);
}

What are your thoughts on the package allowing cross-referencing ILoadSceneInfo with loaded scenes? At the moment, the only ILoadSceneInfo that can cross-reference is the LoadSceneInfoScene, because it has all scene information (index, name and full path). I tend to think that scene transitions are already the solution to this issue.

@joaoborks
Copy link
Member

This got me thinking: what would the Unity Scene Manager do?

The answer: unload the last loaded scene that matches the reference.

That being said, I'll update the code to allow unloading a scene by any matching reference. Your code should absolutely work.

@joaoborks joaoborks self-assigned this Jan 21, 2025
@joaoborks
Copy link
Member

Just pushed the new version 4.0.0-pre.3 including this change.

Now, you are able to unload any scene by using any of its valid references: name, path or buildIndex.
Scenes loaded via Addressables can also be unloaded by their name, path, and by their addressable reference (Asset Reference or Addressable Address).

Your original code should now work as expected. This is how I tested it:

static readonly string LoaderScenePath = "Assets/Scenes/SceneA.unity";
static readonly string MainScenePath = "Assets/Scenes/SceneB.unity";

void Start()
{
    AdvancedSceneManager.SceneLoaded += _OnSceneLoaded;
    AdvancedSceneManager.LoadAsync(MainScenePath, true);
}

void _OnSceneLoaded(Scene scene)
{
    AdvancedSceneManager.SceneUnloaded += _OnSceneUnloaded;
    AdvancedSceneManager.UnloadAsync(LoaderScenePath);
}

void _OnSceneUnloaded(Scene scene)
{
    Debug.LogWarning($"Scene unloaded: {scene.handle}");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants