Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Last active January 22, 2024 21:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/862da3bc22ee13aff61a7606ece6fdd3 to your computer and use it in GitHub Desktop.
Save kurtdekker/862da3bc22ee13aff61a7606ece6fdd3 to your computer and use it in GitHub Desktop.
Scene helper for Unity additive loading.
/*
The following license supersedes all notices in the source code.
Copyright (c) 2021 Kurt Dekker/PLBM Games All rights reserved.
http://www.twitter.com/kurtdekker
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the Kurt Dekker/PLBM Games nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class SceneHelper
{
public static void LoadScene( string s, bool additive = false, bool setActive = false)
{
if (s == null)
{
s = UnityEngine.SceneManagement.SceneManager.GetActiveScene ().name;
}
UnityEngine.SceneManagement.SceneManager.LoadScene (
s, additive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : 0);
if (setActive)
{
// to mark it active we have to wait a frame for it to load.
// Get the CallAfterDelay code at https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e
CallAfterDelay.Create( 0, () => {
UnityEngine.SceneManagement.SceneManager.SetActiveScene(
UnityEngine.SceneManagement.SceneManager.GetSceneByName( s));
});
}
}
public static void UnloadScene( string s)
{
UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(s);
}
}
@mrpacogp
Copy link

Hello, i just found this in your thread in unity forum
This could load the occlusion data from any scene? is not required to make occlusion before of all scene in the main active like documentation explain?
https://docs.unity3d.com/es/2019.4/Manual/occlusion-culling-scene-loading.html

@kurtdekker
Copy link
Author

kurtdekker commented Dec 17, 2021

To use SceneHelper to load one scene:

SceneHelper.LoadScene ("JustOneScene");

To additively load multiple scenes:

SceneHelper.LoadScene( "Scene1");
SceneHelper.LoadScene( "Scene2", additive: true);
SceneHelper.LoadScene( "Lighting", additive: true, setActive: true);

@gnaegit
Copy link

gnaegit commented Oct 11, 2023

I have a problem with additive loading scenes and activating them. I always want the newly loaded scene to be the active one: When I call CallAfterDelay.Create( 0, ...) with 0 I still get an error that the scene is not yet loaded and can not be set active. I increased this number to 0.1 which solved this problem, but then I have the problem that the scene is loaded and the updates are started before the scene is active. Some updates are concerning the active scene which is then in the beginning not the scene which is newly loaded. Do you have any ideas how this can be solved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment