Search Unity

Loading multiple assetbundles from server

Discussion in 'Editor & General Support' started by FontouraCollide, Jan 23, 2018.

  1. FontouraCollide

    FontouraCollide

    Joined:
    Nov 15, 2016
    Posts:
    11
    I've been trying to setup a AssetBundle Loader system for our project, but the documentation about this is confusing and I'm still not sure how or if I can setup the system the way I want to.

    I want to store several prefabs in one AssetBundle each. So, AssetBundle X stores Prefab X.

    I was able to get this to work in a simple sample script:

    Code (CSharp):
    1. public class Loader : MonoBehaviour
    2. {
    3.     private AssetBundleCreateRequest bundleRequest;
    4.     private UnityWebRequest request;
    5.     private bool loaded;
    6.  
    7.     private void Start()
    8.     {
    9.         request = UnityWebRequest.GetAssetBundle(AppConfig.ASSETBUNDLE_URL + "myBundle/1");
    10.         request.SendWebRequest();
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         if (!request.isDone || loaded){
    16.             return;
    17.         }
    18.         var bundle = DownloadHandlerAssetBundle.GetContent(request);
    19.         var myGameObject = Instantiate(bundle.LoadAsset("MyPrefab")) as GameObject;
    20.         loaded = true;
    21.  
    22.     }
    23. }
    However it worked only for one bundle and only If I specify the name of the prefab, in that scene. I wanted to load all the assetsBundles in a "loading" scene to then instantiate the prefabs in the next scene.

    TLDR:
    - How do I save assetbundles for later use in the app
     
    David-Filipe likes this.
  2. STHedgeHog

    STHedgeHog

    Joined:
    Jun 24, 2015
    Posts:
    2
  3. FontouraCollide

    FontouraCollide

    Joined:
    Nov 15, 2016
    Posts:
    11
    Since I posted this I finally understood what I was doing wrong. To make Unity cache asset bundles GetAssetBundle HAS TO have the version otherwise it will not cache it.

    However, now I'm having a different issue related to the bundles. I created a coroutine to load one asset bundle and works fine. Then I called this coroutine for each bundlename in a list. When I do this only the first bundle works, when it tries to load the second one it keeps getting the reference to the first one.


    Code (CSharp):
    1. private IEnumerator GetAssetBundleFromRemote(string bundleName)
    2.     {
    3.         var bundleUrl = Path.Combine(RemotePath, bundleName);
    4.         var webReq = UnityWebRequest.GetAssetBundle(bundleUrl, 1, 0);
    5.         var handler = webReq.downloadHandler as DownloadHandlerAssetBundle;
    6.         yield return webReq.Send();
    7.  
    8.         if (webReq.responseCode == 0){
    9.             Debug.Log(bundleName + "__>" + "Loading from cache");
    10.         }else if (webReq.responseCode == 200){
    11.             Debug.Log(bundleName + "__>" + "Loading from server");
    12.         }
    13.  
    14.         Debug.Log(bundleName + "__>" + "Analizing response");
    15.         var bundle = handler.assetBundle;
    16.        
    17.         if (bundle == null){
    18.             Debug.Log(bundleName + "__>" + "No Bundle Found");
    19.             yield break;
    20.         }
    21.        
    22.         Debug.Log(bundleName + "__>" + "Bundle Found");
    23.         Debug.Log(bundleName + "__>" + "Bundle Name: " + bundle.name);
    24.         foreach (var assetName in bundle.GetAllAssetNames()){
    25.             Debug.Log("Asset: " + assetName);
    26.         }
    27.         var prefab = bundle.LoadAsset<GameObject>(bundleName);
    28.  
    29.         if (prefab != null){
    30.             Debug.Log(bundleName + "__>" + "Prefab Found");
    31.             Instantiate(prefab, transform, false);
    32.  
    33.         }
    34.         bundle.Unload(false);
    35.        
    36.         yield return new WaitForSeconds(3);
    37.     }
    When it tries to load the second bundle this code:

    Code (CSharp):
    1. foreach (var assetName in bundle.GetAllAssetNames()){
    2.             Debug.Log("Asset: " + assetName);
    3.         }
    Gives me the assets of the first bundle.

    This bundles don't have anything in common
     
  4. DropoutGamer

    DropoutGamer

    Joined:
    Jul 3, 2019
    Posts:
    5
    having same issue
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
  6. DropoutGamer

    DropoutGamer

    Joined:
    Jul 3, 2019
    Posts:
    5
    i am using WWW for downloading assetbundle from google drive. not unitywebrequest . WWW is working fine for 1 asset bundle with size less then 100mb , but when it exceeds 100mb its giving downloading error. but when i divide 1 into 2 asset bundle to reduce size problem it download both but show error if i try to get 2nd asset bundle.
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         if (Application.isEditor && removeCache)
    4.             Caching.ClearCache();
    5.  
    6.         StartCoroutine(GetAssetBundle());
    7.     }
    8.    
    9.     IEnumerator GetAssetBundle()
    10.     {
    11.         www = new WWW[url.Length];
    12.         for (indexOfDownloadableFiles=0; indexOfDownloadableFiles < url.Length; indexOfDownloadableFiles++)
    13.         {
    14.              Debug.Log("Dowloading Started");
    15.  
    16.            
    17.             www[indexOfDownloadableFiles] = WWW.LoadFromCacheOrDownload(url[indexOfDownloadableFiles], version );
    18.            
    19.             yield return www[indexOfDownloadableFiles];
    20.  
    21.             if (www[indexOfDownloadableFiles].error != null)
    22.             {
    23.                 //if error in downloading asset bundle show error popup
    24.                 if (errorDownloadDb)
    25.                     errorDownloadDb.SetActive(true);
    26.                 yield break;
    27.  
    28.                 throw new Exception("WWW download had an error: " + www[indexOfDownloadableFiles].error);
    29.             }
    30.            
    31.          
    32.  
    33.        
    34.             AssetBundle assetBundle; assetBundle = www[indexOfDownloadableFiles].assetBundle;
    35.      
    36.             if (!assetBundle && errorDownloadDb)
    37.             {
    38.                 errorDownloadDb.SetActive(true);
    39.                 yield break;
    40.             }
    41.  
    42.             if (canLoadScene)
    43.             {
    44.                 LoadSceneMethod(assetBundle);
    45.             }
    46.             else
    47.             {
    48.  
    49.                 if (prefabsName.Length > 0)
    50.                 {
    51.                    
    52.                     //instantiating prefabs
    53.                     foreach (string prefabName in prefabsName)
    54.                     {
    55.                         Debug.Log("PrefebName" + prefabName);
    56.                         AssetBundleRequest assetLoadRequest = assetBundle.LoadAssetAsync<GameObject>(prefabName);
    57.                         GameObject AssetBundleObj = Instantiate(assetLoadRequest.asset) as GameObject;
    58.                         AssetBundleObj.SetActive(true);
    59.                         Debug.Log("AssetBundleObj: " + AssetBundleObj.name);
    60.                     }
    61.                 }
    62.                 Debug.Log("BundleName: "+assetBundle.name);
    63.                
    64.                 assetBundle.Unload(false);
    65.                 www[indexOfDownloadableFiles].Dispose();
    66.  
    67.                  Debug.Log( " Spawned");
    68.  
    69.             }
    70.         }
    71.             if (showContinue && continueButtonSplash)
    72.             {
    73.                 continueButtonSplash.SetActive(true);
    74.             }
    75.  
    76.             if (loadingObjGameplay)
    77.                 loadingObjGameplay.SetActive(false);
    78.            
    79.        
    80.     }
    81.  
    82.    
     
  7. DropoutGamer

    DropoutGamer

    Joined:
    Jul 3, 2019
    Posts:
    5
    That's the error that i'm getting...
     

    Attached Files:

  8. dvapps

    dvapps

    Joined:
    Apr 16, 2019
    Posts:
    5
    I was stuck in the same problem,
    seems that you have to give a different name to the cached assetbundle, through the CachedAssetBundle structure
    Code (CSharp):
    1.  
    2. ...
    3. CachedAssetBundle cachedAssetBundle = new CachedAssetBundle();
    4.         cachedAssetBundle.name = uniqueName;
    5. ...
    6. UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(mUri, cachedAssetBundle, 0);
    7. ...
    8.  
    because internally, seems that filenames are identical (filename is used as key along the hash number)
     
    Last edited: May 27, 2020