Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Load Asset BundleScene from Save Location

Discussion in 'Scripting' started by James-Williams, Oct 31, 2017.

  1. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26
    Afternoon folks,

    I have recently decided I needed to learn how to use Assetbundles and have been getting my share of bumps and bruises. However, it has been fun and there is a ton of documentation to help out someone who has no idea how they work. I have watched hours of tutorials and read the recent documentation for Webrequest and its benefits. The problem I am having is that I want to load the file from the location I saved the asset bundle to. Also, I chose the request and save method used because of the size of our bundles. I want to be able to download it, and load the scene on Click of a button(not auto). I also want to be able to delete or uninstall the assetbundle.

    Maybe if I can get help figuring out how this whole thing works then I could figure out the rest.

    Anyhow, thanks for the help in advance. The below script is my attempt at putting the things together I have learned over the past 5 days. Current issue is that I get an error


    Code (CSharp):
    1.  
    2. private AssetBundleCreateRequest bundleRequest;
    3.     private UnityWebRequest request;
    4.     private DownloadHandler downloadHandler;
    5.  
    6.     public string sceneURL;
    7.     public string sceneName;
    8.     private bool sceneDownloaded = false;
    9.     private bool sceneDownloading = false;
    10.     public Slider downloadBar;
    11.     public static string dlcPath;
    12.  
    13.  
    14.     public void PlatformSaveLocationCheck()
    15.     {
    16.         print("Detected Device Type" + Application.platform);
    17.  
    18.         if (Application.platform == RuntimePlatform.Android)
    19.         {
    20.             dlcPath = Application.persistentDataPath + "/DLC/";
    21.  
    22.             if (!Directory.Exists(dlcPath))
    23.             {
    24.                 Debug.Log("Directory Created at" + dlcPath);
    25.                 Directory.CreateDirectory(dlcPath);
    26.             }
    27.         }
    28.         else
    29.         {
    30.             dlcPath = Application.dataPath + "/DLC/";
    31.  
    32.             if (!Directory.Exists(dlcPath))
    33.             {
    34.                 Debug.Log("Directory Created at" + dlcPath);
    35.                 Directory.CreateDirectory(dlcPath);
    36.             }
    37.         }
    38.     }
    39.  
    40.     IEnumerator Start()
    41.     {
    42.         PlatformSaveLocationCheck();
    43.  
    44.         string path = Path.Combine(dlcPath, sceneName + ".unity3d");
    45.  
    46.         if (File.Exists(path))
    47.         {
    48.             StartCoroutine(LoadBundleScene());
    49.             Debug.Log("Already Downloaded");  
    50.         }
    51.         else
    52.         {
    53.             //get the bundle
    54.             var request = new UnityWebRequest(sceneURL, UnityWebRequest.kHttpVerbGET);
    55.  
    56.  
    57.             request.downloadHandler = new DownloadHandlerFile(path);
    58.             sceneDownloading = true;
    59.  
    60.             //set actual wait time for server communication
    61.             yield return request.SendWebRequest();
    62.  
    63.             if (request.isNetworkError || request.isHttpError)
    64.                 Debug.LogError(request.error);
    65.             else
    66.                 Debug.Log("File successfully downloaded and saved to " + path);
    67.         }
    68.     }
    69.  
    70.     IEnumerator LoadBundleScene()
    71.     {
    72.        
    73.         bundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(dlcPath, sceneName));
    74.  
    75.         Debug.Log("Asset Already Downloaded. Loading Scene");
    76.  
    77.         if (bundleRequest == null)
    78.         {
    79.             Debug.Log("Failed to load AssetBundle!");
    80.         }
    81.         else
    82.         {
    83.             yield return null;
    84.  
    85.             if (bundleRequest.isDone)
    86.             {
    87.                 Debug.Log("Found Asset Bundle");
    88.      
    89.                 LoadScene();
    90.  
    91.             }
    92.         }
    93.     }
    94.  
    95.  
    96.     public void LoadScene()
    97.     {
    98.         if (sceneDownloaded)
    99.         {
    100.             SceneManager.LoadScene(sceneName);
    101.         }
    102.         else
    103.         {
    104.             return;
    105.         }
    106.     }
    107.  
    108.     private void Update()
    109.     {
    110.         /*if (sceneDownloading)
    111.         {
    112.             if (request.downloadProgress > 0 && request.downloadProgress < 0.99f)
    113.             {
    114.                 downloadBar.value = request.downloadProgress;
    115.             }
    116.         }*/
    117.     }
    118.    
     
  2. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26
    Nevermind the downloadbar portion at the bottom. I need to figure out how to use that since I switched from WWW which was working fine to Webrequest so I don't know where to call it where it doesnt automatically equal 1
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    From error it looks your sceneName variable is simply empty, also the path you save bundle to and the path you load from is different, because you append ".unity3d" to the former.
    Also, why aren't you using DownloadHandlerAssetBundle ?
     
  4. James-Williams

    James-Williams

    Joined:
    Aug 30, 2016
    Posts:
    26
    I was getting an error about assetbundles not being able to access raw data. I was using something like request.downloadhandler.data but I will look into it again.

    So, basically I have to pull the scenename without extension from the assetbundle?

    I may be mistaken but doesn't my start() save it to the DLC folder?

    I assigned the .unity3d after someone else told me that I would have issues fetching the data without using that extension
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    You can't access raw data in DownloadHandlerAssetBundle, not in DownloadHandlerFile, these are specifically designed to not keep large amount of data in memory.
    Why do you need raw data anyway? DownloadHandlerAssetBundle automatically loads the asset bundle for you and you can get already loaded AB out from it.