SGSceneManager

Scene management at run-time.

In-App Settings:

image-1595077954273.png

 

Unity Build Settings:

image-1595078006037.png

SGSceneManager provides only methods for loading scenes, unloading scenes and number of scenes in the In-App Settings. Use the rest of the methods, events and properties through UnityEngine.SceneManagment.SceneManager. 

SGSceneManager and SceneManager work together. But you cannot use SceneManager methods to load and unload scenes that are specified in In-App Settings.

You can load scenes that are in the Unity Build Settings via the standard SceneManager.

Note that the Unity Build Settings and In-App Settings scene indexes are independent!

SceneManager events will work for SGSceneManager too.

Also you can use the following SceneManager methods after loading the scene via SGSceneManager:

You can read more about SceneManager here.


SceneCountInSettings


The number of Scenes which have been added to the Scenes in the In-App Settings. The Editor will contain Scenes that were open before entering playmode.


LoadScene

public static void LoadScene(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, LoadSceneMode mode = LoadSceneMode.Single);\

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the In-App Settings to load.
mode Allows you to specify whether or not to load the Scene additively. See LoadSceneMode for more information about the options.

Description

Loads the Scene by its name or index in the In-App Settings.

In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is: LoadSceneAsync.

When using SGSceneManager.LoadScene, the scene loads in the next frame, that is it does not load immediately. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.

Because loading is set to complete in the next rendered frame, calling SceneManager.LoadScene forces all previous AsyncOperations to complete, even if SGAsyncOperation.allowSceneActivation is set to false. To avoid this, use LoadSceneAsync instead.

The given sceneName can either be the Scene name only, without the .unity extension.

Note that sceneName is case insensitive.

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        // Only specifying the sceneName or sceneIndex will load the Scene with the Single mode
        SGSceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
    }
}

The following two script examples show how LoadScene can load Scenes from the In-App Settings. LoadSceneA uses the name of the Scene to load. LoadSceneB uses the number of the Scene to load. The scripts work together.

LoadSceneA file.

// SceneA.
// SceneA is given the sceneName which will
// load SceneB from the In-App Settings

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment;

public class LoadScenesA : MonoBehaviour
{
    void Start()
    {
        Debug.Log("LoadSceneA");
    }

    public void LoadA(string scenename)
    {
        Debug.Log("sceneName to load: " + scenename);
        SGSceneManager.LoadScene(scenename);
    }
}

LoadSceneB file.

// SceneB.
// SceneB is given the sceneIndex of 0 which will
// load SceneA from the In-App Settings

using UnityEngine;
using SIDGIN.Patcher.Unity.SceneManagment;

public class LoadScenesB : MonoBehaviour
{
    void Start()
    {
        Debug.Log("LoadSceneB");
    }

    public void LoadB(int sceneANumber)
    {
        Debug.Log("sceneIndex to load: " + sceneANumber);
        SGSceneManager.LoadScene(sceneANumber);
    }
}

public static Scene LoadScene(int sceneBuildIndex, LoadSceneParameters parameters);
public static Scene LoadScene(string sceneName, LoadSceneParameters parameters);

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the In-App Settings to load.
parameters Various parameters used to load the Scene.

Returns

Scene A handle to the Scene being loaded.

Description

Loads the Scene by its name or index in the In-App Settings.

An example using two scenes called Scene1 and Scene2. ExampleScript1.cs is for scene1 and ExampleScript2.cs is for scene2.

using UnityEngine;
using UnityEngine.SceneManagement;
using SIDGIN.Patcher.Unity.SceneManagment;
// This is scene1.  It loads 3 copies of scene2.
// Each copy has the same name.

public class ExampleScript1 : MonoBehaviour
{
    private Scene scene;

    private void Start()
    {
        var parameters = new LoadSceneParameters(LoadSceneMode.Additive);

        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 1 of scene2: " + scene.name);
        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 2 of scene2: " + scene.name);
        scene = SGSceneManager.LoadScene("scene2", parameters);
        Debug.Log("Load 3 of scene2: " + scene.name);
    }
}

Scene2:

using UnityEngine;

// create a randomly placed cube

public class ExampleScript2 : MonoBehaviour
{
    private void Start()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = new Vector3(Random.Range(-5.0f, 5.0f), 0.0f, Random.Range(-5.0f, 5.0f));
    }
}

LoadSceneAsync


public static SGAsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Single);
public static SGAsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single);
public static SGAsyncOperation LoadSceneAsync(string sceneName, LoadSceneParameters parameters);
public static SGAsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneParameters parameters);

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the Build Settings to load.
mode If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
parameters Struct that collects the various parameters into a single place except for the name and index.

Returns

SGAsyncOperation Use the SGAsyncOperation to determine if the operation has completed.

Description

Loads the Scene asynchronously in the background.

The given Scene name can either be the full Scene path, the path shown in the In-App Settings window or just the Scene name. If only the Scene name is given this will load the first Scene in the list that matches.

Examples of supported formats:
"Scene1"
"Scene2"

Note: The name of the Scene to load is case sensitive.

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using SIDGIN.Patcher.Unity;
using SIDGIN.Patcher.Unity.SceneManagment;

public class Example : MonoBehaviour
{
    void Update()
    {
        // Press the space key to start coroutine
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Use a coroutine to load the Scene in the background
            StartCoroutine(LoadYourAsyncScene());
        }
    }

    IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in the In-App Settings.

        SGAsyncOperation asyncLoad = SGSceneManager.LoadSceneAsync("Scene2");

        // Wait until the asynchronous scene fully loads
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }
}
Example with progress:
  IEnumerator LoadGameAsync()
        {
            var asyncOperation = SGSceneManager.LoadSceneAsync(0);
            while (!asyncOperation.isDone)
            {
                Debug.Log($"Loading progress:{asyncOperation.progress * 100}%");
                yield return null;
            }
        }

UnloadSceneAsync


public static SGAsyncOperation UnloadSceneAsync(int sceneBuildIndex);
public static SGAsyncOperation UnloadSceneAsync(string sceneName);
public static SGAsyncOperation UnloadSceneAsync(Scene scene);
public static SGAsyncOperation UnloadSceneAsync(int sceneBuildIndex, UnloadSceneOptions options);
public static SGAsyncOperation UnloadSceneAsync(string sceneName, UnloadSceneOptions options);
public static SGAsyncOperation UnloadSceneAsync(Scene scene, UnloadSceneOptions options);

 

Parameters

sceneBuildIndex Index of the Scene in BuildSettings.
sceneName Name or path of the Scene to unload.
scene Scene to unload.
options Scene unloading options.

Returns

SGAsyncOperation Use the SGAsyncOperation to determine if the operation has completed.

Description

Destroys all GameObjects associated with the given Scene and removes the Scene from the SGSceneManager.

The given Scene name can either be the full Scene path, the path shown in the In-App Settings window or just the Scene name. If only the Scene name is given this will load the first Scene in the list that matches.

Examples of supported formats:
"Scene1"
"Scene2"

This is case-sensitive and due to it being async there are no guarantees about completion time.

Assets from Resources(unity) are currently not unloaded. In order to free up asset memory call Resources.UnloadUnusedAssets.

It is not possible to UnloadSceneAsync if there are no scenes to load. For example, a project that has a single scene cannot use this static member.