1
\$\begingroup\$

This is the closest I've gotten:

    EditorSceneManager.OpenScene(
        AssetDatabase.GUIDToAssetPath(
            AssetDatabase.FindAssets(
                "t:SceneAsset " + MenuSceneName,
                new string[] { "Assets" }
            )[0]
        )
    );

However this generates an error at runtime:

InvalidOperationException: This cannot be used during play mode, please use SceneManager.LoadScene()/SceneManager.LoadSceneAsync() instead.

I can't follow its suggestion because those functions require the scene to be in build settings or an asset bundle. So, is there any other option?

I wonder how (and if) Addressables achieve this? Note: I'm not willing to switch to Addressables - I'm quite enjoying working with AssetBundles.

Why?

My game uses AssetBundles for general loading of EVERYTHING, so there's only one blank scene in build settings. However, during development I'd rather not have to continuously build and load from AssetBundles, so I'm trying to dynamically load scenes WITHOUT having to add them to build settings.

\$\endgroup\$
2
\$\begingroup\$

Unfortunately, the only ways to load scenes are the two options you already mentioned. However, just because it’s in the list of scenes, doesn’t mean it needs to end up in the built game. There are two options here.

The first option is to manually uncheck the checkboxes in the build settings next to all of the scenes that you don’t want to include whenever you do a build. Simply click on scene 1, then shift-click on the last scene, and toggle any of the checkboxes.

The alternative is to write a small script which automatically modifies the build settings whenever you do a build. This shouldn’t be too difficult, but if you are not doing a lot of builds it may not be worth the effort.

\$\endgroup\$
  • \$\begingroup\$ I'm not sure how to have a script run when I do a build (and after the build completes to undo the action). Do you know how to achieve those? \$\endgroup\$ – Bilal Akil Sep 17 at 3:41
  • 1
    \$\begingroup\$ Actually that's not too hard, with I(Pre|Post)processBuildWithReport. Thanks! \$\endgroup\$ – Bilal Akil Sep 17 at 4:09
  • \$\begingroup\$ Turns out there's a (maybe new) method doing exactly what I need! See my answer \$\endgroup\$ – Bilal Akil Sep 17 at 6:04
2
\$\begingroup\$

Turns out there's EditorSceneManager.LoadSceneInPlayMode which does exactly what I'm looking for! Awesome.

I had asked on the Unity Forum and found the answer there

\$\endgroup\$
  • \$\begingroup\$ Woah, nice! Must be a new feature. \$\endgroup\$ – Ed Marty Sep 17 at 6:55
  • \$\begingroup\$ Yeah the docs hint to Addressables, so it probably came around with that. Awesome stuff. \$\endgroup\$ – Bilal Akil Sep 17 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.