Ejemplo n.º 1
0
        internal AsyncOperationHandle <TObject> GetAssetAsync <TObject>(AssetTableEntry entry) where TObject : Object
        {
            if (!entry.AsyncOperation.HasValue)
            {
                // Empty entries are treated as null.
                if (string.IsNullOrEmpty(entry.Guid))
                {
                    entry.AsyncOperation = ResourceManager.CreateCompletedOperation <TObject>(null, null);
                }
                else
                {
                    entry.AsyncOperation = AddressablesInterface.LoadAssetFromGUID <TObject>(entry.Guid);
                }
            }

            var operation = entry.AsyncOperation.Value;

            try
            {
                return(operation.Convert <TObject>());
            }
            catch (InvalidCastException)
            {
                // If we preloaded then the operation will be of type AsyncOperationHandle<Object[]> however we now
                // need to extract the asset and convert to AsyncOperationHandle<TObject>.

                if (operation.IsDone)
                {
                    if (operation.Status != AsyncOperationStatus.Succeeded)
                    {
                        return(ResourceManager.CreateCompletedOperation <TObject>(null, operation.OperationException.Message));
                    }

                    // Extract the asset from the array of preloaded sub objects.
                    if (operation.Result is Object[] subObjects)
                    {
                        foreach (var obj in subObjects)
                        {
                            if (obj is TObject target)
                            {
                                var convertedCompletedOperation = ResourceManager.CreateCompletedOperation(target, null);
                                entry.AsyncOperation = convertedCompletedOperation;
                                AddressablesInterface.Release(operation); // Release the old operation
                                return(convertedCompletedOperation);
                            }
                        }
                    }
                    throw new InvalidCastException($"Could not convert asset of type {operation.Result.GetType().Name} to {typeof(TObject).Name}.");
                }

                // Wait for the operation to complete before attempting again
                var convertedOperation = ResourceManager.CreateChainOperation(operation, (op) => GetAssetAsync <TObject>(entry));
                entry.AsyncOperation = convertedOperation;
                AddressablesInterface.Release(operation); // Release the old operation
                return(convertedOperation);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Releases all assets that have been preloaded or cached resets the preload state so it can be performed again.
        /// Note: This is called automatically by <see cref="LocalizedAssetDatabase"/> when the <see cref="LocalizationSettings.SelectedLocale"/> is changed.
        /// </summary>
        public void ReleaseAssets()
        {
            if (m_PreloadOperationHandle.HasValue)
            {
                AddressablesInterface.Release(m_PreloadOperationHandle.Value);
                m_PreloadOperationHandle = null;
            }

            foreach (var entry in Values)
            {
                if (entry.AsyncOperation.HasValue)
                {
                    AddressablesInterface.Release(entry.AsyncOperation.Value);
                    entry.AsyncOperation = null;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Release an asset for a single entry that have been preloaded  or cached
        /// </summary>
        /// <param name="entry">A reference to the entry in the table.</param>
        /// <returns></returns>
        public void ReleaseAsset(AssetTableEntry entry)
        {
            if (entry == null)
            {
                return;
            }

            if (m_PreloadOperationHandle.HasValue)
            {
                AddressablesInterface.Release(m_PreloadOperationHandle.Value);
                m_PreloadOperationHandle = null;
            }

            if (entry.AsyncOperation.HasValue)
            {
                AddressablesInterface.Release(entry.AsyncOperation.Value);
                entry.AsyncOperation = null;
            }
        }