Symptoms
- The asset content of AssetBundles is loading from the disk in a synchronous manner.
Cause
The main thread is blocked while loading the assets. This can make the game have hiccups.Resolution
To load the objects of the AssetBundles in an asynchronous way, in order to avoid blocking the main thread of the computer, you need to call the AssetBundle.LoadAssetAsync method.
Here is a code example that should not block the main thread. Please note that loading the bundle and the assets in it may not lock the main thread but instancing multiple game objects from the bundle could generate hitches in performance.
using UnityEngine; using System.Collections; using System.IO;More Information
public class LoadFromFileAsyncExample : MonoBehaviour { IEnumerator Start() { var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "myassetBundle")); yield return bundleLoadRequest;
var myLoadedAssetBundle = bundleLoadRequest.assetBundle; if (myLoadedAssetBundle == null) { Debug.Log("Failed to load AssetBundle!"); yield break; }
var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync<GameObject>("MyObject"); yield return assetLoadRequest;
GameObject prefab = assetLoadRequest.asset as GameObject; Instantiate(prefab);
myLoadedAssetBundle.Unload(false); } }