Search Unity

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

Bug Problems using AssetBundle.LoadFromFile

Discussion in 'Scripting' started by bruseyu, Jun 7, 2023.

  1. bruseyu

    bruseyu

    Joined:
    Apr 20, 2022
    Posts:
    2
    There is no problem using AssetBundle.LoadFromFile to load .ab files in Unity2019.4.
    But after I upgrade the Unity version to 2021.3.26f1c1, using AssetBundle.LoadFromFile will return null, and the console will output Failed to read data for the AssetBundle.
    I'm sure there is no problem with the file path I loaded.
    Here is my code.

    Code (CSharp):
    1. bundleFilePath = Application.persistentDataPath + "/DownLoad/" + targetName;
    2. if (File. Exists(bundleFilePath))
    3.   {
    4.          tmpAb = AssetBundle.LoadFromFile(bundleFilePath);
    5.   }
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,057
    Have you made sure that you rebuilt the bundles in the new version?

    Also consider using Addressables. Manually loading / unloading asset bundles is tedious and Addressables does quite the heavy lifting for you.
     
  3. bruseyu

    bruseyu

    Joined:
    Apr 20, 2022
    Posts:
    2
    I'm sure.
    The bundles has been rebuilt in the new version
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,057
    I was able to load in a bundle and game object just fine.

    So perhaps what you could try is to setting the
    BuildAssetBundleOptions.ForceRebuildAssetBundle
    option before building your bundles.
    Or delete the current bundles and delete the Library folder so any cache is clean too.

    You can also try what I did in a clean project.
    All I did was creating a cube prefab "Cube" with a white material.
    Assign the folder to an asset bundle "cubebundle".
    Build the bundle through the Bundles menu.
    Added the LoadAssetBundleComponent to a game object
    Ran it in the editor and it was loading in the cube.

    Building Code:
    Code (CSharp):
    1. using System.IO;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class BuildBundlesScript : Editor
    6. {
    7.     [MenuItem("Bundles/Build")]
    8.     public static void BuildBundles()
    9.     {
    10.         var assetBundlePath = $"{Application.persistentDataPath}/downloads/";
    11.         Directory.CreateDirectory(assetBundlePath);
    12.      
    13.         var buildOptions = BuildAssetBundleOptions.ChunkBasedCompression;
    14.         BuildPipeline.BuildAssetBundles(assetBundlePath, buildOptions, BuildTarget.StandaloneWindows64);
    15.     }
    16.  
    17.     [MenuItem("Bundles/Open Folder")]
    18.     public static void OpenBundleLocation()
    19.     {
    20.         var assetBundlePath = $"{Application.persistentDataPath}/downloads/";
    21.         Application.OpenURL(assetBundlePath);
    22.     }
    23. }

    Loading Code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LoadAssetBundleComponent : MonoBehaviour
    4. {
    5.     public string BundleName = "cubebundle";
    6.     public string AssetName = "Cube";
    7.  
    8.     private AssetBundleCreateRequest bundleRequest;
    9.     private AssetBundleRequest objectRequest;
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log("Request loading bundle");
    14.         bundleRequest = AssetBundle.LoadFromFileAsync($"{Application.persistentDataPath}/downloads/{BundleName}");
    15.         bundleRequest.completed += OnBundleRequestCompleted;
    16.     }
    17.  
    18.     private void OnBundleRequestCompleted(AsyncOperation handle)
    19.     {
    20.         Debug.Log("Request asset from bundle");
    21.         objectRequest = bundleRequest.assetBundle.LoadAssetAsync<GameObject>(AssetName);
    22.         objectRequest.completed += InstantiateCube;
    23.     }
    24.  
    25.     private void InstantiateCube(AsyncOperation handle)
    26.     {
    27.         Instantiate(objectRequest.asset);
    28.         Debug.Log("Instantiated asset");
    29.     }
    30. }

    If this doesn't work for you, then I'm not sure what goes wrong.
    I haven't used manual asset bundle building / loading ever since Addressables got released