private CustomModelLoaderCancelable CreateFromResourceManager(CustomModelLoaderCreationContext context, ILoadableContentResourceManager contentResourceManager) { Task <IPrefabContentResourceHandle> avatarPrefabAsync = contentResourceManager.LoadContentPrefabAsync(context.ContentId); return(new CustomModelLoaderCancelable(avatarPrefabAsync, Logger, prefab => { //Callback should only occur is avatarPrefabAsync has completed IPrefabContentResourceHandle handle = avatarPrefabAsync.Result; OnContentPrefabCompletedDownloading?.Invoke(this, new ContentPrefabCompletedDownloadEventArgs(handle, prefab, context.EntityGuid)); })); }
public void StartLoading() { //We NEVER want to directly reference the cancel token in an async context due to race conditions //we should only ref it from this local ref. //Local ref to the cancellation token source CurrentCancellationSource = new CancellationTokenSource(); //TODO: Asset multiple calls UnityAsyncHelper.UnityMainThreadContext.PostAsync(async() => { //This guards and prevents unwanted and wasted resources into //loading an avatar that has had its token canceled. if (IsModelChangeCanceled(CurrentCancellationSource)) { ModelChangeCancelLogging(); return; } //We need to await the resource but capture the context, because we'll need to be on the main thread. IPrefabContentResourceHandle handle = await PrefabHandleFuture .ConfigureAwait(true); //This guards and prevents unwanted and wasted resources into //loading an avatar that has had its token canceled. if (IsModelChangeCanceled(CurrentCancellationSource)) { ModelChangeCancelLogging(); return; } GameObject gameObject = await handle.LoadPrefabAsync() .ConfigureAwait(true); //This guards and prevents unwanted and wasted resources into //loading an avatar that has had its token canceled. if (IsModelChangeCanceled(CurrentCancellationSource)) { ModelChangeCancelLogging(); return; } OnAvatarPrefabReadyCallback.Invoke(gameObject); }); }
protected override void OnEventFired(object source, ContentPrefabCompletedDownloadEventArgs args) { //It's possible we don't know about the entity anymore, since this is a long async process. if (!KnownEntities.isEntityKnown(args.EntityGuid)) { if (Logger.IsInfoEnabled) { Logger.Info($"Encountered {nameof(ContentPrefabCompletedDownloadEventArgs)} for unknown Entity: {args.EntityGuid}"); } return; } //We need to check, and release if there is any, before we replace it. if (PrefabHandleMappable.ContainsKey(args.EntityGuid)) { IPrefabContentResourceHandle handle = PrefabHandleMappable.RetrieveEntity(args.EntityGuid); handle.Release(); PrefabHandleMappable.RemoveEntityEntry(args.EntityGuid); } PrefabHandleMappable.AddObject(args.EntityGuid, args.PrefabHandle); }
public SingleReleaseablePrefabContentResourceHandleDecorator([NotNull] IPrefabContentResourceHandle decoratedHandle) { DecoratedHandle = decoratedHandle ?? throw new ArgumentNullException(nameof(decoratedHandle)); }
public ContentPrefabCompletedDownloadEventArgs([NotNull] IPrefabContentResourceHandle prefabHandle, [NotNull] GameObject downloadedPrefabObject, [NotNull] NetworkEntityGuid entityGuid) { PrefabHandle = prefabHandle ?? throw new ArgumentNullException(nameof(prefabHandle)); DownloadedPrefabObject = downloadedPrefabObject ?? throw new ArgumentNullException(nameof(downloadedPrefabObject)); EntityGuid = entityGuid ?? throw new ArgumentNullException(nameof(entityGuid)); }