/// <inheritdoc /> public async Task <IPrefabContentResourceHandle> LoadAvatarPrefabAsync(long avatarId) { //If it's already available, we can just return immediately if (IsAvatarResourceAvailable(avatarId)) { return(TryLoadAvatarPrefab(avatarId)); } ContentDownloadURLResponse downloadUrlResponse = await ContentClient.RequestAvatarDownloadUrl(avatarId, AuthTokenRepo.RetrieveWithType()) .ConfigureAwait(false); //TODO: Handle failure TaskCompletionSource <IPrefabContentResourceHandle> completionSource = new TaskCompletionSource <IPrefabContentResourceHandle>(); //Asset bundle requests can sadly only happen on the main thread, so we must join the main thread. await new UnityYieldAwaitable(); //TODO: We should handle caching, versioning and etc here. UnityWebRequestAsyncOperation asyncOperation = UnityWebRequestAssetBundle.GetAssetBundle(downloadUrlResponse.DownloadURL, 0).SendWebRequest(); //TODO: We should render these operations to the loading screen UI. asyncOperation.completed += operation => { //When we first get back on the main thread, the main concern //is that this resource manager may be from the last scene //and that the client may have moved on //to avoid this issues we check disposal state //and do nothing, otherwise if we check AFTER then we just have to release the assetbundle immediately anyway. if (isDisposed) { //Just tell anyone awaiting this that it is canceled. They should handle that case, not us. completionSource.SetCanceled(); return; } //GetContent will throw if the assetbundle has already been loaded. //So to prevent this from occuring due to multiple requests for the //content async we will check, on this main thread, via a write lock. lock (SyncObj) { //We're on the main thread again. So, we should check if another //request already got the bundle if (IsAvatarResourceAvailable(avatarId)) { completionSource.SetResult(TryLoadAvatarPrefab(avatarId)); return; } //otherwise, we still don't have it so we should initialize it. this.ResourceHandleCache[avatarId] = new ReferenceCountedPrefabContentResourceHandle(DownloadHandlerAssetBundle.GetContent(asyncOperation.webRequest)); completionSource.SetResult(TryLoadAvatarPrefab(avatarId)); //we assume this will work now. } }; return(await completionSource.Task .ConfigureAwait(false)); }