Search Unity

Very confused on proper usage of Asset Bundles

Discussion in 'Asset Bundles' started by hedgeh0g, Jun 3, 2020.

  1. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102
    Hello folks,

    I'm starting to develop the whole "asset bundle management" thing. And I already caught several doubts.
    My user is supposed to choose from a list any level he wishes. If that level exists, download, load and play it. The documentation doesn't seem to cover the entire process, that's why I have doubts on how to proceed.

    What I got so far:

    Code (CSharp):
    1. public class AssetBundleWebLoader : MonoBehaviour
    2. {
    3.     IEnumerator LoadBundle(string bundleUrl, string assetName)
    4.     {
    5.         using (UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl))
    6.         {
    7.             yield return www.SendWebRequest();
    8.             if (www.isNetworkError || www.isHttpError)
    9.             {
    10.                 Debug.Log(www.error);
    11.             }
    12.             else
    13.             {
    14.                 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
    15.                 // Is this correct to check if the download is complete?
    16.                 if (www.downloadProgress >= 1.0f)
    17.                 {
    18.                     //bundle.LoadAllAssets(); // Perhaps don't load EVERYTHING here.
    19.                 }
    20.             }
    21.         }
    22.     }
    23. }
    Download a bundle from a URL. Fine.

    1. On the last line, I am downloading the bundle. How do I check if the bundle has been fully downloaded so I can deploy it?
    2. Can I let the user keep the bundle in memory so he doesn't have to download it again after he reboots the game? How?
    3. If the bundle consists in a single scene, every 3D object, material and stuff will be included in the bundle. EXCEPT for scripts that were not included in the original build. Am I correct?
    4. If I mark a folder as Asset Bundle, will everything inside become part of the bundle?

    I think I will add more questions the more I work on it.

    Thanks in advance for the help.
     
    Last edited: Jun 3, 2020
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    When you "yield return www.SendWebRequest()" it will stop the coroutine until the download is complete. The only bit that remains is fully integrating and loading the asset bundle, which happens when you get a reference to it. Just check that returned AssetBundle is not null.

    Not in memory but on disk.
    AssetBundle system has buitin caching system. Check the documentation for UnityWebRequestAssetBundle.GetAssetBundle, the overload you currently use does not cache and will download every time, but there are overloads with more parameters that will download once and load from cache next time.

    Scripts are compiled and shipped with your game, you can't include them in asset bundles.
     
    hedgeh0g likes this.
  3. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102
    This is the part in the documentation I don't understand. Because:

    Code (CSharp):
    1. public static Networking.UnityWebRequest GetAssetBundle(string uri, uint version, uint crc);
    Look like WWW.LoadFromCacheOrDownload: check if it is in the cache, if it is not, download it. But, as far as I understand, it goes in the cache. Will it be deleted on the next reboot?

    Code (CSharp):
    1. public static Networking.UnityWebRequest GetAssetBundle(string uri, CachedAssetBundle cachedAssetBundle, uint crc);
    Is the same but you are allowed to specify a cache path. Again, deleted on the reboot?

    Thanks for the reply.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    No, it is stored on disk for future use. It can be deleted explicitly using Caching class.