User philip previously marked this as a duplicate question of this, but failed to actually read either question it seems. The question that this was marked as a duplicate of, does not address the Additive scene loading using Unity SceneManager
.
Let me premise this with, this is not intended to be an "x vs y" opinion piece, I am simply looking for information on what is more optimal and what is the preferred way.
tldr;
I am new to unity and over the past week built a very small 2D 'Shooter' game to help me learn how to interact with the engine. When trying a new project and looking in to persisting game managers between scenes, I initially found the DontDestroyOnLoad
syntax.
DontDestroyOnLoad vs LoadSceneMode
- Which is more optimal for large games/scenes?
- Both methods appear to do the same thing, except you create the scene yourself of additive scene loading.
- New to Unity looking in to methods of persisting objects and in-memory data between scenes.
- People mentioned the Unity Docs stating to not use
DontDestroyOnLoad
but I can't find this.
Essentially, I had the following on (at the time) two 'manager' scripts. When then calling SceneManager.LoadScene(#);
in other parts of the code and in other scenes, these managers persisted between scenes.
using UnityEngine;
using UnityEngine.SceneManagement;
public class MyBehaviour : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
}
It worked, no issues there. However when looking in to other methods to ensure what I was doing is 'good practice' and 'optimal' I had come across a few places mentioning not to use this method, a few people mentioned that the unity docs for DontDestroyOnLoad also reiterate this, I could not find such text. Instead, they mention having a 'Manager Scene' with all of the objects to persist being within that scene.
To implement this, I had to add a third manager, 'CustomSceneManager' that handles unloading the current scene and loading a new one, while persisting the current 'ManagerScene'.
using UnityEngine;
using UnityEngine.SceneManagement;
public class CustomSceneManager : MonoBehaviour
{
private int _currentScene = -1;
// This is in the manager scene, so the manager scene always gets pulled in to scenes
// when a scene is switched via this method.
public void SwitchScene(int sceneId) {
if (_currentScene != -1) {
SceneManager.UnloadSceneAsync(_currentScene);
}
_currentScene = sceneId;
SceneManager.LoadScene(sceneId, LoadSceneMode.Additive);
}
}
This method also works, with the overhead of a further empty GameObject / Manager to handle scene logic (I would likely need this to be a manager in the future anyway as I instance parts of the game) and the fact that anywhere I switch scene, I need to go via this manager, which means finding it/creating a reference where I want to use it vs just calling Unitys built in scene manager methods.