/// <summary>
        /// Load an asset via its location.  The asset will actually be loaded via the AssetDatabase API.
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <param name="location"></param>
        /// <returns></returns>
        internal VBAsyncOperation <object> LoadAssetAsync(ProvideHandle provideHandle, IResourceLocation location)
        {
            if (location == null)
            {
                throw new ArgumentException("IResourceLocation location cannot be null.");
            }
            if (m_BundleLoadOperation == null)
            {
                return(new VBAsyncOperation <object>().StartCompleted(location, location, null, new ResourceManagerException("LoadAssetAsync called on unloaded bundle " + m_Name)));
            }

            if (!m_BundleLoadOperation.IsDone)
            {
                return(new VBAsyncOperation <object>().StartCompleted(location, location, null, new ResourceManagerException("LoadAssetAsync called on loading bundle " + m_Name)));
            }
            VirtualAssetBundleEntry assetInfo;
            var assetPath = location.InternalId;

            if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
            {
                assetPath = mainPath;
            }

            //this needs to use the non translated internal id since that was how the table was built.
            if (!m_AssetMap.TryGetValue(assetPath, out assetInfo))
            {
                return(new VBAsyncOperation <object>().StartCompleted(location, location, null, new ResourceManagerException(string.Format("Unable to load asset {0} from simulated bundle {1}.", location.InternalId, Name))));
            }

            var op = new LoadAssetOp(location, assetInfo, provideHandle);

            m_AssetLoadOperations.Add(op);
            return(op);
        }
            public void Start(ProvideHandle provideHandle)
            {
                provideHandle.SetProgressCallback(ProgressCallback);
                subObjectName      = null;
                m_ProvideHandle    = provideHandle;
                m_RequestOperation = null;
                List <object> deps = new List <object>(); // TODO: garbage. need to pass actual count and reuse the list

                m_ProvideHandle.GetDependencies(deps);
                AssetBundle bundle = LoadBundleFromDependecies(deps);

                if (bundle == null)
                {
                    m_ProvideHandle.Complete <AssetBundle>(null, false, new Exception("Unable to load dependent bundle from location " + m_ProvideHandle.Location));
                }
                else
                {
                    var assetPath = m_ProvideHandle.ResourceManager.TransformInternalId(m_ProvideHandle.Location);
                    if (m_ProvideHandle.Type.IsArray)
                    {
                        m_RequestOperation = bundle.LoadAssetWithSubAssetsAsync(assetPath, m_ProvideHandle.Type.GetElementType());
                    }
                    else if (m_ProvideHandle.Type.IsGenericType && typeof(IList <>) == m_ProvideHandle.Type.GetGenericTypeDefinition())
                    {
                        m_RequestOperation = bundle.LoadAssetWithSubAssetsAsync(assetPath, m_ProvideHandle.Type.GetGenericArguments()[0]);
                    }
                    else
                    {
                        if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
                        {
                            subObjectName      = subKey;
                            m_RequestOperation = bundle.LoadAssetWithSubAssetsAsync(mainPath, m_ProvideHandle.Type);
                        }
            void LoadImmediate()
            {
                string assetPath = m_ProvideHandle.ResourceManager.TransformInternalId(m_ProvideHandle.Location);
                object result    = null;

                if (m_ProvideHandle.Type.IsArray)
                {
                    result = ResourceManagerConfig.CreateArrayResult(m_ProvideHandle.Type, AssetDatabase.LoadAllAssetsAtPath(assetPath));
                }
                else if (m_ProvideHandle.Type.IsGenericType && typeof(IList <>) == m_ProvideHandle.Type.GetGenericTypeDefinition())
                {
                    result = ResourceManagerConfig.CreateListResult(m_ProvideHandle.Type, AssetDatabase.LoadAllAssetsAtPath(assetPath));
                }
                else
                {
                    if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
                    {
                        var objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(mainPath);
                        foreach (var o in objs)
                        {
                            if (o.name == subKey)
                            {
                                if (m_ProvideHandle.Type.IsAssignableFrom(o.GetType()))
                                {
                                    result = o;
                                    break;
                                }
                            }
                        }
                    }
示例#4
0
        public bool Locate(object key, Type type, out IList <IResourceLocation> locations)
        {
            locations = null;
            if (ResourceManagerConfig.ExtractKeyAndSubKey(key, out string mainKey, out string subKey))
            {
                if (!m_Addressables.GetResourceLocations(mainKey, type, out IList <IResourceLocation> locs))
                {
                    if (type == typeof(Sprite))
                    {
                        m_Addressables.GetResourceLocations(mainKey, typeof(SpriteAtlas), out locs);
                    }
                }

                if (locs != null && locs.Count > 0)
                {
                    locations = new List <IResourceLocation>(locs.Count);
                    foreach (var l in locs)
                    {
                        CreateDynamicLocations(type, locations, key as string, subKey, l);
                    }
                    return(true);
                }
            }
            return(false);
        }
            public bool Load(long localBandwidth, long remoteBandwidth, float unscaledDeltaTime)
            {
                if (IsDone)
                {
                    return(false);
                }
                var now = m_LastUpdateTime + unscaledDeltaTime;

                if (now > m_LastUpdateTime)
                {
                    m_BytesLoaded   += (long)Math.Ceiling((now - m_LastUpdateTime) * localBandwidth);
                    m_LastUpdateTime = now;
                }
                if (m_BytesLoaded < m_AssetInfo.Size)
                {
                    return(true);
                }
                if (!(Context is IResourceLocation))
                {
                    return(false);
                }
                var    location  = Context as IResourceLocation;
                var    assetPath = m_provideHandle.ResourceManager.TransformInternalId(location);
                object result    = null;

                var pt = m_provideHandle.Type;

                if (pt.IsArray)
                {
                    result = ResourceManagerConfig.CreateArrayResult(pt, AssetDatabaseProvider.LoadAssetsWithSubAssets(assetPath));
                }
                else if (pt.IsGenericType && typeof(IList <>) == pt.GetGenericTypeDefinition())
                {
                    result = ResourceManagerConfig.CreateListResult(pt, AssetDatabaseProvider.LoadAssetsWithSubAssets(assetPath));
                }
                else
                {
                    if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
                    {
                        var objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(mainPath);
                        foreach (var o in objs)
                        {
                            if (o.name == subKey)
                            {
                                if (pt.IsAssignableFrom(o.GetType()))
                                {
                                    result = o;
                                    break;
                                }
                            }
                        }
                    }
            private void BeginAssetLoad()
            {
                if (m_AssetBundle == null)
                {
                    m_ProvideHandle.Complete <AssetBundle>(null, false, new Exception("Unable to load dependent bundle from location " + m_ProvideHandle.Location));
                }
                else
                {
                    var assetPath = m_ProvideHandle.ResourceManager.TransformInternalId(m_ProvideHandle.Location);
                    if (m_ProvideHandle.Type.IsArray)
                    {
#if !UNITY_2021_1_OR_NEWER
                        if (AsyncOperationHandle.IsWaitingForCompletion)
                        {
                            GetArrayResult(m_AssetBundle.LoadAssetWithSubAssets(assetPath, m_ProvideHandle.Type.GetElementType()));
                            CompleteOperation();
                        }
                        else
#endif
                        m_RequestOperation = m_AssetBundle.LoadAssetWithSubAssetsAsync(assetPath, m_ProvideHandle.Type.GetElementType());
                    }
                    else if (m_ProvideHandle.Type.IsGenericType && typeof(IList <>) == m_ProvideHandle.Type.GetGenericTypeDefinition())
                    {
#if !UNITY_2021_1_OR_NEWER
                        if (AsyncOperationHandle.IsWaitingForCompletion)
                        {
                            GetListResult(m_AssetBundle.LoadAssetWithSubAssets(assetPath, m_ProvideHandle.Type.GetGenericArguments()[0]));
                            CompleteOperation();
                        }
                        else
#endif
                        m_RequestOperation = m_AssetBundle.LoadAssetWithSubAssetsAsync(assetPath, m_ProvideHandle.Type.GetGenericArguments()[0]);
                    }
                    else
                    {
                        if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
                        {
                            subObjectName = subKey;
#if !UNITY_2021_1_OR_NEWER
                            if (AsyncOperationHandle.IsWaitingForCompletion)
                            {
                                GetAssetSubObjectResult(m_AssetBundle.LoadAssetWithSubAssets(mainPath, m_ProvideHandle.Type));
                                CompleteOperation();
                            }
                            else
#endif
                            m_RequestOperation = m_AssetBundle.LoadAssetWithSubAssetsAsync(mainPath, m_ProvideHandle.Type);
                        }
示例#7
0
        /// <inheritdoc/>
        public override void Provide(ProvideHandle providerInterface)
        {
            var atlas = providerInterface.GetDependency <SpriteAtlas>(0);

            if (atlas == null)
            {
                providerInterface.Complete <Sprite>(null, false, new System.Exception($"Sprite atlas failed to load for location {providerInterface.Location.PrimaryKey}."));
                return;
            }

            var key = providerInterface.ResourceManager.TransformInternalId(providerInterface.Location);

            ResourceManagerConfig.ExtractKeyAndSubKey(key, out string mainKey, out string subKey);
            string spriteKey = string.IsNullOrEmpty(subKey) ? mainKey : subKey;
            var    sprite    = atlas.GetSprite(spriteKey);

            providerInterface.Complete(sprite, sprite != null, sprite != null ? null : new System.Exception($"Sprite failed to load for location {providerInterface.Location.PrimaryKey}."));
        }
            public bool Load(long localBandwidth, long remoteBandwidth, float unscaledDeltaTime)
            {
                if (IsDone)
                {
                    return(false);
                }
                var now = m_LastUpdateTime + unscaledDeltaTime;

                if (now > m_LastUpdateTime)
                {
                    m_BytesLoaded   += (long)Math.Ceiling((now - m_LastUpdateTime) * localBandwidth);
                    m_LastUpdateTime = now;
                }
                if (m_BytesLoaded < m_AssetInfo.Size)
                {
                    return(true);
                }
                if (!(Context is IResourceLocation))
                {
                    return(false);
                }
                var    location  = Context as IResourceLocation;
                var    assetPath = m_AssetInfo.m_AssetPath;
                object result    = null;

                var pt = m_provideHandle.Type;

                if (pt.IsArray)
                {
                    result = ResourceManagerConfig.CreateArrayResult(pt, AssetDatabaseProvider.LoadAssetsWithSubAssets(assetPath));
                }
                else if (pt.IsGenericType && typeof(IList <>) == pt.GetGenericTypeDefinition())
                {
                    result = ResourceManagerConfig.CreateListResult(pt, AssetDatabaseProvider.LoadAssetsWithSubAssets(assetPath));
                }
                else
                {
                    if (ResourceManagerConfig.ExtractKeyAndSubKey(location.InternalId, out string mainPath, out string subKey))
                    {
                        result = AssetDatabaseProvider.LoadAssetSubObject(assetPath, subKey, pt);
                    }
        public override void Provide(ProvideHandle pi)
        {
            Type t          = pi.Type;
            bool isList     = t.IsGenericType && typeof(IList <>) == t.GetGenericTypeDefinition();
            var  internalId = pi.ResourceManager.TransformInternalId(pi.Location);

            if (t.IsArray || isList)
            {
                object result = null;
                if (t.IsArray)
                {
                    result = ResourceManagerConfig.CreateArrayResult(t, Resources.LoadAll(internalId, t.GetElementType()));
                }
                else
                {
                    result = ResourceManagerConfig.CreateListResult(t, Resources.LoadAll(internalId, t.GetGenericArguments()[0]));
                }

                pi.Complete(result, result != null, result == null ? new Exception($"Unable to load asset of type {pi.Type} from location {pi.Location}.") : null);
            }
            else
            {
                if (ResourceManagerConfig.ExtractKeyAndSubKey(internalId, out string mainPath, out string subKey))
                {
                    var    objs   = Resources.LoadAll(mainPath, pi.Type);
                    object result = null;
                    foreach (var o in objs)
                    {
                        if (o.name == subKey)
                        {
                            if (pi.Type.IsAssignableFrom(o.GetType()))
                            {
                                result = o;
                                break;
                            }
                        }
                    }
                    pi.Complete(result, result != null, result == null ? new Exception($"Unable to load asset of type {pi.Type} from location {pi.Location}.") : null);
                }
            void LoadImmediate()
            {
                if (m_Loaded)
                {
                    return;
                }
                m_Loaded = true;
                string assetPath = m_ProvideHandle.ResourceManager.TransformInternalId(m_ProvideHandle.Location);
                object result    = null;

                if (m_ProvideHandle.Type.IsArray)
                {
                    result = ResourceManagerConfig.CreateArrayResult(m_ProvideHandle.Type, LoadAssetsWithSubAssets(assetPath));
                }
                else if (m_ProvideHandle.Type.IsGenericType && typeof(IList <>) == m_ProvideHandle.Type.GetGenericTypeDefinition())
                {
                    result = ResourceManagerConfig.CreateListResult(m_ProvideHandle.Type, LoadAssetsWithSubAssets(assetPath));
                }
                else
                {
                    if (ResourceManagerConfig.ExtractKeyAndSubKey(assetPath, out string mainPath, out string subKey))
                    {
                        result = LoadAssetSubObject(mainPath, subKey, m_ProvideHandle.Type);
                    }
 public void ResourceManagerConfigExtractKeyAndSubKey_WhenPassedKey_ReturnsExpectedValue(object key, bool expectedReturn, string expectedMainKey, string expectedSubKey)
 {
     Assert.AreEqual(expectedReturn, ResourceManagerConfig.ExtractKeyAndSubKey(key, out string mainKey, out string subKey));
     Assert.AreEqual(expectedMainKey, mainKey);
     Assert.AreEqual(expectedSubKey, subKey);
 }