Search Issue Tracker

By Design

Votes

0

Found in

2018.4

2018.4.5f1

2019.2

2019.3

2020.1

Issue ID

1174706

Regression

No

Calling AssetBundle.LoadFromMemoryAsync doesn't load bundles if calling from non-main thread in the Editor

Scripting

-

How to reproduce:
1. Open Unity project, attached to the user's email
2. Load InitialScene scene
3. Press Build -> Build Win64 in the Top Menu
4. Play in the Editor

Expected result: Bundle is loaded and TestSceneInBundle is played.
Actual result: TestSceneInBundle isn't loaded.

Workaround: In AssetbundleSystem.cs, line 402, uncomment line 403
m_ReadyToLoadFromMemory = true, and comment line 404
CompleteLoadFromMemory()

Reproduced with: 2018.4.7f1, 2019.2.3f1, 2019.3.0b1, 2020.1.0a1

Notes:
In 2017.4.32f1 after entering Play mode error appears. For the user, this version works properly.
Works properly on a build.

  1. Resolution Note (2020.1.X):

    By design. AssetBundle.LoadFromMemoryAsync has to be called on the main thread.

    Unity throws an exception when it is called on a non-main thread, but this exception is getting silently caught by the C# thread pool task triggered async FileStream.BeginRead call.

    By adding a try/catch around AssetBundle.LoadFromMemoryAsync and logging the exception, the following is output:

    UnityException: LoadFromMemoryAsync_Internal can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    UnityEngine.AssetBundle.LoadFromMemoryAsync (System.Byte[] binary)
    AssetBundleInfo.CompleteLoadFromMemory () (at Assets/AssetBundleSystem.cs:470)
    UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
    UnityEngine.DebugLogHandler:LogException(Exception, Object)
    UnityEngine.Logger:LogException(Exception, Object)
    UnityEngine.Debug:LogException(Exception)
    AssetBundleInfo:CompleteLoadFromMemory() (at Assets/AssetBundleSystem.cs:477)
    AssetBundleInfo:FinishLoadingBundleFromMemory(Boolean) (at Assets/AssetBundleSystem.cs:404)
    AssetBundleInfo:GetBytesFromFileDone(IAsyncResult) (at Assets/AssetBundleSystem.cs:456)
    System.IO.ReadWriteTask:InvokeAsyncCallback(Object)
    System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)
    System.Threading.ExecutionContext:Run(ExecutionContext, ContextCallback, Object, Boolean)
    System.IO.ReadWriteTask:System.Threading.Tasks.ITaskCompletionAction.Invoke(Task)
    System.Threading.Tasks.Task:FinishContinuations()
    System.Threading.Tasks.Task:FinishStageThree()
    System.Threading.Tasks.Task:FinishStageTwo()
    System.Threading.Tasks.Task:Finish(Boolean)
    System.Threading.Tasks.Task:ExecuteWithThreadLocal(Task&)
    System.Threading.Tasks.Task:ExecuteEntry(Boolean)
    System.Threading.Tasks.Task:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    System.Threading.ThreadPoolWorkQueue:Dispatch()
    System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.