Asset Bundle Scenes Workflow?

I’m trying to adapt to the 2017x way of doing asset bundles, and I think I’ve got a handle on “normal” bundles, but I also use scene bundles extensively, and I can’t seem to find a workflow that lets me build scene bundles and load them from my server. I get different errors depending on whether the Simulation Mode is on (if it’s on, I get a “can’t find scene with that name in that bundle” error, and with Sim Mode off, I get
NullReferenceException: Object reference not set to an instance of an object
AssetBundles.AssetBundleManager.RemapVariantName (System.String assetBundleName) (at Assets/AssetBundleManager/AssetBundleManager.cs:372).

Here’s the load script I’m using atm:

                var www = WWW.LoadFromCacheOrDownload (bundleUrl, 1); //{
                yield return www;
                if (!string.IsNullOrEmpty (www.error)) {
                    Debug.Log (www.error);
                    yield return null;
                } else {
                    AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(bundleName, sceneName, true);
                    if (request == null)
                        yield break;
                    yield return StartCoroutine(request);

For my project (see https://servicemedia.net), I build lots of scene bundles from external projects, along with “normal” bundles that contain only one prefab, across multiple platforms, and host them in the cloud. I had working build scripts and client/server code to support all this, that was kindof making me happy, until 2017.1 came along… Anyway, this might be a bug, if so I’ll submit a minimal case, but I could also be Doing It Wrong.

I did a bug report, see case 932503. Using the asset bundle manager, in 2017.1f3, I built the TestScene used in the LoadScene demo, and put it on the server, used that link in the way indicated in LoadScene.cs, got the same errors described above.

Loading scene bundles from a server seems like pretty important general use case; for me it’s integral to everything I’m doing. Very disappointing that this doesn’t seem to work anymore. Please help!

Update - scene bundles built from the same project do work, it’s scenes built from other projects that won’t load. Which would defeat the whole purpose of asset bundles.

Hmm, we don’t have any code restrictions specifically preventing this. So assuming the 2 projects have similar code & settings, it should load just fine. If they are not loading, then it is possible it’s a configuration or missing type error in the second project. I’d suggest double checking the following:

  1. Ensure Player Setting, Graphics Settings, and Build Settings are identical on both projects
  2. Ensure that any code types you are using in the scene are located in the same assembly / path as they are on the first project.
  3. Use WebExtract & Binary2Text (located in <Unity_Install_Path>/Data/Tools) on your built asset bundles and look at the generated content to see if anything stands out.

Thanks Ryanc, I’ll check again, it is tricky to get everything moved forward and sync’d.

So I’ve retested this a few times, and get the same result. @Ryan-Unity (or any Unity person), can you guys confirm you’ve actually tested loading non-trivial scenes built from an external projects? I think the settings are correct - as noted above, I’ve been doing this successfully for years, across multiple platforms, until the coming of 2017x and the Asset Bundle Manager. From where I sit this is major regression. No resolution yet on the bug report above, other than to repeat that “it should be possible”. I know it should be possible, but can you please confirm that it is actually possible in practice, not just in theory?

You pinged the wrong Ryan =P
Looking at the Player logs submitted with the bug report, there are a lot of errors related to unable to find object or script information. So that leads me to think it’s code related, so that makes me think it is related to the issue reported in Asset Bundles problem with scripts since 5.6.2 - Unity Engine - Unity Discussions

If you give me a bit, I’ll try and check your example before and after the fix for that issue and see if it’s related.

Oops, sorry! And thanks for your attention.

I found, happily, what I think is a working solution to this, without using the AssetBundleManager package:

UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(bundleUrl, 1, 0);
                    DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(bundleUrl, 1, 0);
                    request.downloadHandler = handler;
                    yield return request.Send();
                    AssetBundle bundle = handler.assetBundle;
                    string[] scenePath = bundle.GetAllScenePaths();
                    AsyncOperation async = SceneManager.LoadSceneAsync (scenePath[0], LoadSceneMode.Additive);
                    yield return async;
                    bundle.Unload (false);

Still testing, but this lets me load asset bundle scenes built from external projects; I hope that helps somebody. The bug above still stands, I think, regarding the AssetBundleManager, but that seems more oriented to build-everything-from-the-same-project scenarios anyway. Thanks to all who worked on this!

1 Like

Hey polytropoi,
I meant to post Friday that I was able to load your scenes directly, but based on your above reply, it would seem we now know the issue. In 2017.1, the WWW class was changed to be a wrapper around UnityWebRequest, and it would seem that the wrapper implementation has a bug in it somewhere. We’ll take a look at WWW and see if we can find and fix the issue.

Hi, is there any news for the bug? I have been loading scene from AssetBundle without any issues however suddenly it started to not to load if I dont include scene (bundled one) at the build settings on Unity Unity 2017.1.0f3… Application quits itself whenever I want to load a scene from AssetBundle… Do you have any ideas?

Tested again and it seems that there is an unreliability. As I download the scene from FTP server, that was bundled from the same project, now I can open the scene as before… however, today I got a problem for a while that when I was trying to open the scene, the application quit without any reason, no errors neither any exception… How come it can be? All this happened at the same version (Unity 2017.1.0f3).

Edit: Now I disabled the Stripe Code feature at the Player settings and it is working… But it was enabled before and all worked fine until today, why does it happen?

That sounds like the engine was stripping out code that you need because it did not see that you are using it in an asset bundle. To use code stripping with asset bundles, you will need to build the player directly using a script so you can pass in a custom BuildPlayerOptions struct that points to where you built your asset bundles: https://docs.unity3d.com/ScriptReference/BuildPlayerOptions.html

If I understood correctly, then my future bundles (that would be downloaded from FTP server) will not be included inside the BuildPlayerOptions.

I plan to release the game with download manager, at the first run players will be automatically downloading scenes from server that are not at the build options. Then at the next phases, whenever I want to release a new level map (a new scene) I will bundle and upload it to the server (bundled from the same project and code will be exactly same as it was released so no new code or changes), then players will download them as mini updates.

Now with BuildPlayerOptions, I will not be able to add my future bundles since it compiles. Am I correct?

is there a way to use BuildPlayerOptions in cloud build ?

.

Thank you very much. Your solution worked for me. However I’ve got another problem.
First time I hit a button, it loads a scene Asset Bundle from a server fine. Then I hit back to home screen. When I try to hit the button a second time, it gives me an error : “scenex.unity3d can´t be loaded because another asset bundlewith same file already loaded”

You need to Unload the bundle with bundle.Unload(false) function after you are done with the Assetbundle. Furthermore, when you want to download a new version from server in the future, you will need to clean the cache since the previous bundle version is cached in memory so it will load the correct version you downloaded…