Search Unity

How do I clear cached Asset Bundles when using Unity Web Requests to download them ?

Discussion in 'Asset Bundles' started by hotrodfuel, Jun 23, 2020.

  1. hotrodfuel

    hotrodfuel

    Joined:
    May 8, 2020
    Posts:
    10
    Hey Everyone I'm stuck and really unsure how to proceed. I'm downloading Asset bundles from Amazon Web Services.

    I have stored cached versions on my local disk for my PC. I try to use Caching.Clear() to remove the bundle but it doesn't work and when I try to actually use Caching.ClearOtherCachedVersions() nothing is happening . When I try to download using UnityWebRequestassetbundle.GetAssetBundle(versionnumber) while incorporating a bundle I get a download success but Unity throws a
    ASSETBUNDLE CRC MISMATCH error


    Are there any other suggestions to fix this ?
    This is my Unity Web Request download code at the moment

    Code (CSharp):
    1. var uwr = UnityWebRequestAssetBundle.GetAssetBundle(loadedPath, oBundle, 0); //var uwr = UnityWebRequestAssetBundle.GetAssetBundle(loadedPath,oVersionNum);
    2. yield return uwr.SendWebRequest();

    3. cachedBundle = DownloadHandlerAssetBundle.GetContent(uwr);
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    So where are you loading the bundle from? Is loadedPath local or remote? Caching.Clear will clear Unity's internal caching mechanism, but won't delete bundles that you've downloaded / copied to the filesystem yourself
     
  3. hotrodfuel

    hotrodfuel

    Joined:
    May 8, 2020
    Posts:
    10

    We are downloading the bundles from Amazon Web services .

    the loaded bundle path is local if the assets are already stored to the disc otherwise we try to download them from amazon web services .

    Do you have any suggestions for how I can delete bundles downloaded to the file system ? I seem to be stuck there specifically?

    thank you so much !
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    I'm pretty sure you're overthinking the problem. To use Unity's caching mechanism, you don't have to bother downloading bundles to the filesystem and then loading them from there. Check out the docs below:

    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestAssetBundle.GetAssetBundle.html

    If you download a bundle from your AWS fileserver and pass a hash or version number to the UnityWebRequestAssetBundle it will cache the bundle in Unity's internal cache for you. The next time you initiate a download for the same bundle (identified by the last 'segment' of the URL) it will first check its internal cache for a previously downloaded bundle with the same hash and return that instead, thus requiring no actual download. Caching.ClearCache will then clear the internal cache and you won't have to bother manually deleting files.

    Perhaps the name UnityWebRequest is a bit deceptive, as it also handles loading local files.
     
  5. hotrodfuel

    hotrodfuel

    Joined:
    May 8, 2020
    Posts:
    10
    I'd like ot think so but when I pull in the bundles using the UnityWebRequestassetBundle.GetassetBundle() function I get one of two results

    Either I get a loaded bundle that has the incorrect older naming conventions ( Which I take as a sign that the older bundle keeps getting reloaded )

    OR when I update the CRC number I get an error that states Error while downloading Asset Bundle: CRC Mismatch. Provided 64, calculated 201b5fe3 from data. Will not load AssetBundle

    It seems like the request is foregoing the new assets on our server for the locally stored cached assets or breaking when I try to force am ore updated bundle version load.
     
  6. hotrodfuel

    hotrodfuel

    Joined:
    May 8, 2020
    Posts:
    10
    Actually I think the hash was the missing part , so If I grab the hash from the manifest file before downloading the asset I should be fine and ALWAYS cache the correct version of the file .

    I think that's what I was missing , thank you !
     
  7. jalajshah

    jalajshah

    Joined:
    Mar 5, 2018
    Posts:
    62
    Caching.ClearCache();

    is worked in my case to remove all old caching and download a new version.