#How to export scenes with subscenes into AssetBundle

1 messages · Page 1 of 1 (latest)

drowsy oasis
#

The short version is - I want to ship DLC via assetbundles, and some of this DLC needs to contain an entity subscene. My export/import process works fine, except for this subscene requirement. When I import my assetbundle and load the scene (main scene), all the gameobjects load, and I can see there should be a subscene, but the SceneAsset property of the subscene has become unlinked from the actual subscene itself.

The exporter just uses BuildPipeline.BuildAssetBundles, which automatically finds all dependencies refereneced in the scene (including assets), but doesn't seem to work for subscenes in some way :/

Code in replies...

#
private void BuildBundle(string name, string destinationFolder)
    {
        name = name.ToLowerInvariant();

        destinationFolder = Path.Combine(destinationFolder, "export");

        if (Directory.Exists(destinationFolder))
            FileUtil.DeleteFileOrDirectory(destinationFolder);

        List<string> assetPaths = new List<string>();

        // Include main scene path
        assetPaths.Add(scenePath);

        // My attempt to add subscenes to the bundle, but the SubScene is no longer linked to its scene asset when importing
        foreach (var subscenePath in GetSubscenePaths())
        {
            // Explicitly load the subscene and add it as a dependency
            var subscene = EditorSceneManager.OpenScene(subscenePath, OpenSceneMode.Additive);
            if (subscene.IsValid())
            {
                assetPaths.Add(subscenePath);
                SceneManager.MergeScenes(subscene, SceneManager.GetActiveScene());
            }
            else
            {
                Debug.LogError("Failed to load subscene: " + subscenePath);
            }
        }

        var buildMap = new AssetBundleBuild[] 
        {
            new AssetBundleBuild() 
            {
                assetBundleName = $"{name}.bundle",
                assetNames = assetPaths.ToArray(),
            },
        };

        if (!Directory.Exists(destinationFolder))
            Directory.CreateDirectory(destinationFolder);

        var bundleBuildOptions = BuildAssetBundleOptions.None;

        if (!compressBundles) bundleBuildOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;

        BuildPipeline.BuildAssetBundles(destinationFolder, buildMap, bundleBuildOptions, BuildTarget.StandaloneWindows64);
        Debug.Log(destinationFolder);

        CleanupBuild(destinationFolder, new[] { $"{name}.bundle" });
    }

Importing:

#
        AssetBundle bundle = BundleUtility.getBundle(bundleFile);

        string[] scenePaths = bundle.GetAllScenePaths();

        SceneManager.LoadScene(scenePaths[0], LoadSceneMode.Single);

        if (scenePaths.Length > 1)
        {
            subscenePath = scenePaths[1];   
        }
        else
        {
            subscenePath = "";
        }
misty fable
#

You can't put subscenes in asset bundles

#

You have to use content management instead