Search Unity

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

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Asset Bundle Caching questions:

Discussion in 'Asset Bundles' started by AustynPember, Jan 4, 2019.

  1. AustynPember


    Mar 14, 2018

    I have been trying to work with and understand the UnityWebRequestAssetBundle.GetAssetBundle for a while. It is not behaving like I expect it to, and was wondering if anyone could help explain some things for me.

    I have a .fbx file called CTX-med_1 in my Unity project that I package up and put into Assets/AssetBundles/ folder. I then upload the 4 files to an Azure blob "AssetBundles, AssetBundles.manifest, ctx100, ctx100.manifest" Then I delete the original model in my project before I run it or build it.

    Now - here is my code I am using to retrieve the model at runtime and instantiate it: (I edited some part of the URL to be different than what I'm actually using)

    Code (CSharp):
    1. public class LoadAssets : MonoBehaviour
    2. {
    3.     public GameObject modelParent;
    4.     public GameObject LoadingScreen;
    5.     public Slider loadingSlider;
    6.     private bool isDownload = false;
    7.     public string url = "";
    8.     public string bundleName = "CTX_med_1";
    9.     public string ModelHash = "e623581f18222c774e6485ab7b0c3408";
    10.     void Start()
    11.     {
    12.         StartCoroutine(AssetLoad(url, bundleName));
    13.     }
    15.     IEnumerator AssetLoad(string url, string bundleName)
    16.     {
    17.         using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url, Hash128.Parse(ModelHash), 0))
    18.         {
    19.             Debug.Log("Trying the bundle download");
    20.             isDownload = true;
    22.             StartCoroutine(progress(request));
    24.             yield return request.SendWebRequest();
    25.             isDownload = false;
    27.             if (request.isNetworkError || request.isHttpError)
    28.             {
    29.                 Debug.Log("Error loading");
    30.             }
    31.             else
    32.             {
    33.                 Debug.Log("Bundle downloaded");
    34.                 //save the asset bundle
    35.                 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
    37.                 //instantiate the prefab
    38.                 GameObject model = bundle.LoadAsset<GameObject>(bundleName);
    39.                 model = Instantiate(model);
    40.                 model.transform.SetParent(modelParent.transform);
    41.                 LoadingScreen.SetActive(false);
    43.             }
    44.         }
    46.         yield return null;
    47.     }
    49.     IEnumerator progress(UnityWebRequest req)
    50.     {
    51.         while (isDownload)
    52.         {
    53.             Debug.Log("Downloading : " + req.downloadProgress * 100 + "%");
    54.             loadingSlider.value = Mathf.Clamp01(req.downloadProgress);
    55.             yield return new WaitForSeconds(0.1f);
    56.         }
    57.     }
    58. }
    What happens when I run this in both the Editor and when I make a project build, is that it loads the scene, waits a second, says that it is 100% downloaded (I think this means it is already cached since if I run UnityWebRequestAssetBundle.GetAssetBundle without the Hash, it will actually go through the process of downloading it and showing me progress over the course of like 4 seconds.) This tells me that it isn't fetching the model off a server, but is somehow just grabbing it from cache which makes no sense to me.

    The next thing I do that is unexpected is that I try to make a new AssetPackage by cloning the model, deleting the old one, and packaging it up thereby getting a new Hash. I use this new Hash in the code (but I do not update the model and model.manifest on the Azure server,) and it STILL works (doesn't even go through a download process) when I make a new build with the different hardcoded hash...

    This is unexpected. First, I expect it to download a model when I make a fresh build the first time I run it. Second, I expect that when I change the Hash and make a new build, it fails to fetch the model because I'm using a Hash in the fetch that is not the Hash that is on my Azure blob.

    Where is the model being cached? I cant find it on my local disk anywhere.

    Thanks for any help!