Unity: The Benefits of Additive Scenes

Additive scenes in Unity provide a number of benefits for working with a team, particularly when it comes to organization, reusability, and avoiding conflicts in work. In this post, we'll take a look at what additive scenes are, the benefits they offer, and how to properly use them in your Unity projects.

What even is an additive scene, and when do I use one??

First, let's define what additive scenes are. In Unity, a scene is a container for all of the objects, assets, and scripts that make up a particular level or area of a game. An additive scene is a scene that is loaded in addition to the current scene, rather than replacing it. This allows multiple scenes to be loaded and active at the same time, which can be useful for a variety of purposes.

One of the primary benefits of additive scenes is that they make it much easier to work with a team. For example, if different members of a team are responsible for different areas of a game, they can work on their own scenes and then add them to the main scene when they're ready. This allows for a more modular and organized workflow, as well as makes it easier to test and debug different parts of a game.

Another benefit of additive scenes is that they can be used to create reusable game systems. For example, a scene containing a menu system can be loaded and unloaded as needed, rather than having to duplicate the menu code in multiple scenes. This can save a significant amount of development time and make it easier to update and maintain the code.

What’s the catch??

As you’ve probably already guessed, additive scenes can also have some drawbacks, and it's important to be aware of these when using them.

One issue is that if not careful, it can be difficult to keep track of which objects are in which scene, and this can lead to confusion and errors.

Additionally, additive scenes can increase the memory usage of a game, which can be a problem on lower-end devices.

How can I possibly carry all these scenes!?

To properly use additive scenes in Unity, it's important to use them in a way that is consistent with your project's goals and resources. One way to do this is to create a single scene that acts as a "hub" scene, which loads and unloads the other scenes as needed. This can help to minimize memory usage and keep the organization of the project more manageable.

You could imagine in the “hub” scene an example called “SceneSwitcher”:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.SceneUtility;
using System.Collections.Generic;

public class SceneSwitcher : MonoBehaviour
{
    private List<string> loadedScenes = new List<string>();

    public void SwitchScene(string sceneName)
    {
        if (loadedScenes.Contains(sceneName))
        {
            UnloadScene(sceneName);
        }
        else
        {
            LoadScene(sceneName);
        }
    }

    public void LoadScene(string sceneName)
    {
        // Check if the scene is valid
        if (DoesSceneExist(sceneName)) {
            SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
            loadedScenes.Add(sceneName);
        }
    }

    public void UnloadScene(string sceneName)
    {
        // Check if the scene is valid
        if (DoesSceneExist(sceneName)) {
            SceneManager.UnloadSceneAsync(sceneName);
            loadedScenes.Remove(sceneName);
        }
    }

    private bool DoesSceneExists(string name)
    {
        // Don't let invalid strings through
        if (string.IsNullOrEmpty(name)) { return false; }

        // Fetch all scenes in build settings, and identify if the any of the
        // scenes match the given scene name, if so, return true
        for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
        {
            string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
            var lastSlash = scenePath.LastIndexOf("/");
            string sceneName = scenePath.Substring(lastSlash + 1, scenePath.LastIndexOf(".") - lastSlash - 1);

            if (string.Compare(name, sceneName, true) == 0) { return true; }
        }

        // If no scene was found by the name, let people know!
        print("Scene with the name: " + name + " does NOT exists in the buildsettings");
        return false;
    }
}

This script keeps track of the loaded additive scenes in a list called loadedScenes. When the SwitchScene method is called with a scene name, it checks if that scene is already in the list. If it is, the script unloads the scene using the UnloadSceneAsync method. If the scene is not in the list, it loads the scene using the LoadScene method and adds the scene name to the list.

Our DoesSceneExists method makes sure we’re honest about checking if the expected scene was added to our project build settings, as this is required to be able to load the scene additively.

Please note that the scene names should match the name of the scene as seen in your Unity project.

You can attach it to an object in your “hub” scene and create UI elements (e.g. buttons) that call the SwitchScene method when clicked.

You can also use the LoadScene and UnloadScene methods separately as per the requirement.

In conclusion, I believe additive scenes in Unity can provide a number of benefits for working with a team, particularly when it comes to organization and reusability. However, it is also important to be aware of its drawbacks and use it in a way that is consistent with the project's goals and resources. If you want to improve your workflow and make your Unity projects more efficient and manageable, then additive scenes are worth exploring.

Previous
Previous

Unity3D: Creating a Mechanic to Inspect Objects in World Space

Next
Next

Unity3D: Optimal Mesh Deformation