Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to load an AssetBundle prefab (or scene) with lightmaps.

Discussion in 'Asset Bundles' started by AndyRat, Nov 20, 2022.

  1. AndyRat

    AndyRat

    Joined:
    Sep 11, 2017
    Posts:
    6
    My goal is to deliver an app that rarely needs updating by having it dynamically load content (prefabs or additive scenes) from a web address.

    I can easily achieve this with AssetBundles however none of the assets contain lightmaps.
    It makes sense that bundled prefabs don't have lightmaps because the maps are stored with the scene so I believe now I have tried almost every possible method of bundling them together with the prefab but still no luck.

    I've tried putting the maps in other separate bundles that I download before the prefab that uses them. I've even tried bundling entire scenes (but unless the scene already exists in the build list they appear as completely empty scenes anyway so I don't actually see the point in bundling scenes).
    Installing the Addressables package seems like overkill to just attach a lightmap to a assetbundled prefab.
    So can anyone suggest how to solve this?
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Bundling scenes that don't exist in build should work just fine, why do you not see the point?
     
  3. AndyRat

    AndyRat

    Joined:
    Sep 11, 2017
    Posts:
    6
    Are you suggesting that instantiating a downloaded bundled scene will include all the scenes assets including lightmaps? Mine appeared completely empty when I tried unless the original scene still existed in the project.
     
  4. AndyRat

    AndyRat

    Joined:
    Sep 11, 2017
    Posts:
    6
    Solved it! Thanks nilsdr (for making me think twice).
    It does load the entire Scene with contents and lightmaps! - if you don't make any typos :)

    Here is working code to load the bundled scene:

    Code (CSharp):
    1.  
    2.     IEnumerator LoadScene()
    3.     {
    4.         www = UnityWebRequestAssetBundle.GetAssetBundle("https://website.com/assets/test.unity3d");
    5.  
    6.         yield return www.SendWebRequest();
    7.  
    8.         if (www.result != UnityWebRequest.Result.Success)
    9.         {
    10.             Debug.Log(www.error);
    11.             yield break;
    12.         }
    13.         else
    14.         {
    15.             Debug.Log("Success connecting to website. Start loading scene model");
    16.  
    17.             var bundle = DownloadHandlerAssetBundle.GetContent(www);
    18.  
    19.             string[] scenePath = bundle.GetAllScenePaths();
    20.             AsyncOperation scene = SceneManager.LoadSceneAsync(scenePath[0], LoadSceneMode.Additive);
    21.  
    22.             //Wait until we are done loading the scene
    23.             while (scene.progress < 0.9f)
    24.             {
    25.                 Debug.Log("Loading scene " + " [][] Progress: " + scene.progress);
    26.                 yield return null;
    27.             }
    28.             bundle.Unload(false);
    29.         }
    30.     }
    31.  
    Lightmaps in Prefabs is another question, but using additive scenes is perfect for me.