0

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.


Dan_
  • 101
  • 1
  • 2
    Welcome to a stack exchange site. If you think that your question has been closed without a good reason, please add comment on the original question explaining why you think so, and vote for reopen. Creating a new question for the same topic is not the way to go here as it creates more duplicate content, something we're trying to avoid. – Vaillancourt Mar 30 '20 at 11:30
  • @Vaillancourt Apologies, the app doesn't say as such, in fact, it says to open a new question. https://imgur.com/a/yMuAqf2 – Dan_ Mar 30 '20 at 11:34
  • @Vaillancourt Also, I can not vote to re-open the question on this Stack Exchange due to not having the reputation. It is like you guys are intentionally driving people elsewhere? – Dan_ Mar 30 '20 at 11:38
  • Thanks for reporting this. That's an oddity and it conflicts with the help center (last paragraph); I assume this is due to the reduce space on the mobile app. – Vaillancourt Mar 30 '20 at 11:40
  • 2
    If you can't vote to reopen your own question might also be an issue. I'll try and see about it. Meanwhile you can ping a mod or the user that closed the question and try and get them to reopen the question. – Vaillancourt Mar 30 '20 at 11:43
  • 1
    I checked for the issue with the text on the mobile app, and it is being discontinued (hasn't been updated since 2017). See here. It is suggested to use the web site and enable the mobile design. – Vaillancourt Mar 30 '20 at 12:26
  • 1
    And for the reopening, it seems you're stuck with what's here. Note that when you edit your (closed) question, it goes into a "reopen" vote queue, so your question will be seen by others and potentially reopened. – Vaillancourt Mar 30 '20 at 12:35
  • @Vaillancourt Thanks for getting back to me on these issues, I hadn't realised the app was discontinued and will be sure to use the mobile site in the future. As for the question reopening, I will probably head over to unity answers in the meantime and ask there instead. Hope you have a good day. – Dan_ Mar 30 '20 at 12:39

0 Answers0