Search Unity

AssetBundles problems

Discussion in 'Getting Started' started by mahito2932, Jan 18, 2022.

  1. mahito2932

    mahito2932

    Joined:
    Nov 15, 2020
    Posts:
    115
    Hi everyone.
    I'm trying to figure out what I'm doing wrong.
    My apk is over 150mb so I decided to use AssetBundles to download (url) scenes from the server. On the PC it works fine and load the scene but when I try to build on Android nothing happens and the progress bar is always stay at zero it does not give me any error on the log.
    Script that i use to download and load:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. using System.IO;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class BundleWebLoader : MonoBehaviour
    9. {
    10.     public string url;
    11.     public int downloaded = 0;
    12.     AssetBundle bundle;
    13.     public System.Object test;
    14.     public Slider progressbar;
    15.     public Text percent;
    16.     public float progress;
    17.  
    18.     WWW www;
    19.  
    20.  
    21.     void Update()
    22.     {
    23.         progress = www.progress;
    24.         progressbar.value = progress;
    25.         percent.text = progress.ToString() + "%";
    26.     }
    27.  
    28.     private void Start()
    29.     {
    30.         ClearCache();
    31.         StartCoroutine(LoadFromServer());
    32.     }
    33.  
    34.  
    35.     IEnumerator LoadFromServer()
    36.     {
    37.         yield return new WaitForEndOfFrame();
    38.             if (downloaded == 0)
    39.             {
    40.                 using (www = WWW.LoadFromCacheOrDownload(url, 1))
    41.                 {
    42.                     yield return www;
    43.                     if (www.error != null)
    44.                         throw new Exception("WWW download had an error:" + www.error);
    45.                     if (www.error == null)
    46.                     {
    47.                         bundle = www.assetBundle;
    48.                     }
    49.                 }
    50.                 if (Caching.ready == true)
    51.                 {
    52.                     downloaded = 1;
    53.                     string[] scenePath = bundle.GetAllScenePaths();
    54.                     Debug.Log(scenePath[0]);
    55.                     SceneManager.LoadScene(scenePath[0]);
    56.                 }
    57.            
    58.  
    59.  
    60.         }
    61.  
    62.     }
    63.  
    64.     void ClearCache()
    65.     {
    66.         Directory.CreateDirectory("Cache1");
    67.         Directory.CreateDirectory("Cache2");
    68.         Directory.CreateDirectory("Cache3");
    69.  
    70.         Caching.AddCache("Cache1");
    71.         Caching.AddCache("Cache2");
    72.         Caching.AddCache("Cache3");
    73.  
    74.         bool success = Caching.ClearCache();
    75.  
    76.         if (!success)
    77.         {
    78.             Debug.Log("Unable to clear cache");
    79.         }
    80.     }
    81. }