Search Unity

reading downloaded and cached AssetBundle in a scene (Sprites, Animations, Sounds e.g.)

Discussion in 'Asset Bundles' started by freedom667, May 7, 2018.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I'm using Asset Browser of Unity and built as Android. then i downloading as cache but I don't know how to read it. I'm stuck here. I wanted to make like DLC you know.

    this is downloader as cache (using AssetBundleManager at github):

    Code (CSharp):
    1. public string bundleName = "BundleNameHere";
    2.         public string url = "file:///Users/****Desktop/Unity Projects/***/AssetBundles/";
    3.         private AssetBundleManager abm;
    4.  
    5.         private void Start()
    6.         {
    7.             abm = new AssetBundleManager();
    8.             abm.SetBaseUri(url);
    9.             abm.Initialize(OnAssetBundleManagerInitialized);
    10.         }
    11.  
    12.         private void OnAssetBundleManagerInitialized(bool success)
    13.         {
    14.             if (success) {
    15.                 abm.GetBundle(bundleName, OnAssetBundleDownloaded);
    16.             } else {
    17.                 Debug.LogError("Error initializing ABM.");
    18.             }
    19.         }
    20.  
    21.         private void OnAssetBundleDownloaded(AssetBundle bundle)
    22.         {
    23.             if (bundle != null) {
    24.                 // Do something with the bundle
    25.                 abm.UnloadBundle(bundle);
    26.             }
    27.             Debug.Log(Caching.defaultCache.path);
    28.             abm.Dispose();
    29.         }
    this is getting AssetBundle (but giving error; "instantiate is null"):

    Code (CSharp):
    1. public string url = "file:///Users/****/Library/Caches/Unity/***/****/97d6aeaead9461a2b739e4e9cac5f9ba/__data";
    2.  
    3. IEnumerator Start()
    4.     {
    5.         var uwr = UnityWebRequest.GetAssetBundle(url);
    6.         yield return uwr.SendWebRequest();
    7.  
    8.         // Get the designated main asset and instantiate it.
    9.         Instantiate(DownloadHandlerAssetBundle.GetContent(uwr).mainAsset);
    10.     }
    So I'm using this
    Code (CSharp):
    1.  
    2. public string url = "file:///Users/****/Library/Caches/Unity/***/****/97d6aeaead9461a2b739e4e9cac5f9ba/__data";
    3. IEnumerator Start()
    4.     {
    5.         while (!Caching.ready)
    6.             yield return null;
    7.  
    8.         using (var www = WWW.LoadFromCacheOrDownload(url, 5))
    9.         {
    10.             yield return www;
    11.             if (!string.IsNullOrEmpty(www.error))
    12.             {
    13.                 Debug.Log(www.error);
    14.                 yield return null;
    15.             }
    16.             var myLoadedAssetBundle = www.assetBundle;
    17.  
    18.             var asset = myLoadedAssetBundle.mainAsset;
    19.         }
    20.     }
     
    Last edited: May 7, 2018
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    I don't understand what is your problem?
    Normally you would put asset bundles somewhere online and use UnityWebRequest.GetAssetBundle(url, ...)
    The methods with extra arguments besides URL will attempt load bundle from cache and will download. if bundle isn't cached.
     
  3. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I get it but how to import and read the bundle at runtime? downloading and caching but not reading. I'm stucak at read the bundle. Also should i remove the folders that what i mean? sprites, animations, sounds will read from cache.

    Also again, UnityWebRequestGetAssetBundle does not work. giving error; "instantiate is null"
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    This is because you don't check for errors. Does UnityWebRequest succeed? Is returned AssetBundle not null?
    According error, you pass null to Instantiate(), perhaps you didn't provide main asset when building the bundle?
     
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    the error is this:
    Code (CSharp):
    1. ArgumentException: The Object you want to instantiate is null.
    2. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)
    3. UnityEngine.Object.Instantiate (UnityEngine.Object original) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:175)
    4. LoadAssets+<Start>c__Iterator0.MoveNext () (at Assets/Scripts/LoadAssets.cs:35)
    5. UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    URL path is this:
    Code (CSharp):
    1. public string url = file:///Users/****/Library/Caches/Unity/****/scenes/86d6b0bf4c733c01362a991aa82a2cae/__data
    (***) this is private. I can't show because of this
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Is this on your desktop machine. You mentioned Android, did you build AssetBundles for Android?
    Bundles built for Android can only be loaded on Android, but you URI not for Android.
     
  7. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    ah yes you're completely right I thought this actually. thanks for this. Well then How can i read this? this code works with reading? otherwise should i write another script?

    So I built it for Android, this is OK. but if i remove builded folders(sprites,animations,scenes,sounds) from Assets folder, then will it give error at Runtime? otherwise this will read builded folder as directly?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You need to build asset bundles specifically for platform you want to load them on, meaning separate bundles for different platforms.
    Normally you would put bundles on some server from where they can be downloaded. If you want to distribute them with application, then StreamingAssets folder is suitable place for bundles, you resolve it at runtime via Application.streamingAssetsPath
     
  9. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    should i delete these folder? because the sprite gameobjects could not read the sources. I asked because of it. if i don't delete, then the apk size will increase