Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Assetbundle caching and load from file

Discussion in 'Asset Bundles' started by Roryyyyyyyyyy, Sep 14, 2018.

  1. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    20
    Hi all,
    I am trying to work out a system for downloading when the game is loaded up. I am currently using UnityWebRequest.GetAssetBundle (I am using 2017.4) to cache the assetbundle in the system. I have an asset manager that tracks everything loaded, but I'm wondering which way would be better to handle the bundles. Am I right in thinking I cannot use AssetBundle.LoadFromFile if I have downloaded it from the cache?

    Our current process is that the app gets the bundle crc from the server which replaces the bundle in the cache if so with the UnityWebRequest.GetAssetBundle. It doesn't store the bundle anywhere, it just caches it.

    How is the best way to get the bundles after this? E.g. I want to load a building model, I will want to load the building bundle from the cache, get the asset, store it in the manager then unload the bundle. Would it be best just to use the web request each time?

    Would anyone recommend me doing it a different way or is this a reasonable approach?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    1,813
    One I thing I suggest you to do is to figure out what you need exactly and see if you aren't reinventing the wheel.
    UnityWebRequest.GetAssetBundle() (the overloads with CRC/version arguments) is loading AssetBundle from cache, if given AssetBundle with given crc/version/hash is in the cache, otherwise it downloads and caches it for future use. As such you just do UnityWebRequest.GetAssetBundle() and then take the bundle via DownloadHandlerAssetBundle.GetContent(), this way you don't need to bother, whether tahe bundle was downloaded or loaded from the cache.

    If you are looking into using AssetBundle.LoadFromFile, the you shouldn't use UnityWebRequest.GetAssetBundle, you need to assign DownloadHandlerFile instead to bypass AssetBundle system at download time. But by doing so you also lose the builtin caching etc. and reinvent them yourself. So it sounds you should first make sure the builtin caching/loading is not enough for you.
     
  3. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    20
    Ah okay, so the only real difference would be I would need to create a system for caching it manually? I guess the UWR method would be much better for me then. In terms or memory usage is there much in it between UWR and LoadFromFile?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    1,813
    UWR caching stores AssetBundle on disk decompressed, so the load will be faster. In terms of memory usage there should be no significant difference between the two, only that in case of LoadFromFile you'd have to download and store the bundle first and ensure efficient memory usage during download (such as DownloadHandlerFile).
     
  5. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    20
    That makes sense, thanks for the information! I already have UWR quite heavily implemented so it seems it makes sense to just stick with that. Cheers!