Hello there,
I’m trying to pre-load a bunch of addressable prefabs during gameplay phases, so that when the player reaches a new level he won’t have to wait for the assets to load. Each chapter is made with about 20 prefabs
However I can’t manage to load the assets in the background without freezing the game.
I have tried using asset.LoadAssetAsync(), Addressables.LoadAssetsAsync, from the Start or inside a coroutine. It keeps freezing the game and I can’t find a solution for that. It feels like the LoadAsync is still trying to load each asset in a single frame, can’t we do this in another thread?
Thank you
Can you post a full code example of how you load these into memory? Have you tried async code?
Sure, here is what I have now (loading async of the assetReferences):
[SerializeField] private AssetReferenceGameObject[] _assetsToPreload;
[SerializeField] private UnityEvent _preloadingEnded;
public void LoadAssets()
{
StartCoroutine(PreloadAssetsRoutine());
}
private IEnumerator PreloadAssetsRoutine()
{
AsyncOperationHandle handleInit = Addressables.InitializeAsync();
yield return handleInit;
if (handleInit.Status == AsyncOperationStatus.Succeeded)
{
int loadedCount = 0;
foreach (var asset in _assetsToPreload)
{
AsyncOperationHandle<GameObject> handle = asset.LoadAssetAsync<GameObject>();
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
loadedCount++;
}
else
{
Debug.LogError("Failed to load asset: " + asset.RuntimeKey);
}
}
if (loadedCount == _assetsToPreload.Count())
{
_preloadingEnded?.Invoke();
}
}
else
{
Debug.LogError("Failed to initialize Addressables.");
}
}
And here is another thing that I tried without coroutines:
public void LoadAssets()
{
Addressables.InitializeAsync().Completed += LoadingReady;
}
private void LoadingReady(AsyncOperationHandle<IResourceLocator> handle)
{
Addressables.LoadAssetsAsync<GameObject>("ChapterLabel", null);
}
I also tried setting Application.backgroundLoadingPriority = ThreadPriority.Low;
Any update on this? We are also having this issue. Pretty much same code as above. Waiting for Addressables.InitializeAsync to complete causes a massive hitch.
No need to use addressable. LoadSceneAsync freeze in Unity 6. I tested it with 6000.0.34, 6000.0.38,
6000.1.0b. The game in editor freeze until the download is done.
Same code works flawlessly with unity 2022.3.44.
private void MyLoad()
{
StartCoroutine(MyLoadCo());
}
private IEnumerator MyLoadCo()
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(SceneIndex);
asyncOperation.allowSceneActivation = false;
while (!asyncOperation.isDone)
{
//Output the current progress
Debug.Log(asyncOperation.progress);
m_Text.text = (asyncOperation.progress * 100) + "%";
// Check if the load has finished
if (asyncOperation.progress >= 0.9f)
{
//Change the Text to show the Scene is ready
m_Text.text = "Press the space bar to continue";
//Wait to you press the space key to activate the Scene
if (Input.GetKeyDown(KeyCode.Space))
//Activate the Scene
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}