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); } }
Comments
2 comments
Documentation comment:
The "Loading AssetBundles" link is broken.
Specific issue:
When I use AssetBundle.LoadAssetAsync, it still freezes up the rendering thread while loading the asset. Why does this happen? This is in Unity 2017.1.
General comment:
Developers need more information about where the data are going when asset loading operations are being performed. From reading the docs, it appears that first an AssetBundle is retrieved from storage and eventually ends up in system RAM.
Next, an asset, such as a GameObject containing meshes and textures, is retrieved from the AssetBundle. Does this create a copy of the asset in RAM? (This is where the rendering hitch occurs for me.)
Next, the GameObject is instantiated. Does this copy the data to the GPU? Is there still a copy in RAM?
This kind of information should be stated explicitly and often throughout the Unity documentation, as it would greatly reduce the amount of confusion and guesswork experienced by developers, and allow them to write more efficient games.
Hello
Please sign in to leave a comment.