Esempio n. 1
0
        /// <summary>
        /// 预加载资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="OnPreloadFinishCallback"></param>
        /// <returns></returns>
        internal ResourceState PreloadAsset(string assetName, Action <string, Object> OnPreloadFinishCallback)
        {
            AssetInfo assetInfo = cacheMgr.Cache.GetAssetInCache(assetName);

            if (assetInfo != null && assetInfo.AssetValid)
            {
                bool assetValid = assetInfo.GetAsset(OnPreloadFinishCallback);

                return(ResourceState.OK);
            }

            string             assetBelongBundleName = ResourceMgr.Instance.GetBundleName(assetName);
            BundleInfoResource cachedBundleInfo      = cacheMgr.Cache[assetBelongBundleName];

            //Bundle已经加载,但没加载资源
            if (cachedBundleInfo != null)
            {
                cacheMgr.AddCallbackToDic(assetName, OnPreloadFinishCallback);

                cacheMgr.DoEnqueuePending(cachedBundleInfo.Name, LoadPriority.Default);

                return(ResourceState.Wait);
            }
            else
            {
                PreloadBundle(assetBelongBundleName, (bundleName, assetBundle) =>
                {
                    PreloadAsset(assetName, OnPreloadFinishCallback);
                });
                return(ResourceState.Wait);
            }
        }
        /// <summary>
        /// 在AssetBundle已加载的条件下加载资源
        /// </summary>
        /// <param name="bundleName"></param>
        /// <param name="priority"></param>
        /// <param name="bundle"></param>
        public void DoEnqueuePending(string bundleName, LoadPriority priority, AssetBundle bundle = null)
        {
            BundleInfoResource  BundleInfoResource = cache[bundleName];
            BundleIndexItemInfo bundleInfo         = ResourceMgr.Instance.GetBundleInfo(bundleName);

            if (BundleInfoResource != null && BundleInfoResource.AssetBundle)
            {
                AssetBundleAssetLoadingInfo info = new AssetBundleAssetLoadingInfo(bundleInfo);
                info.AssetBundle = BundleInfoResource.AssetBundle;
                info.IsAsync     = true;
                info.Priority    = priority;
                EnqueuePendingAssetBunlde(info);
            }
            else
            {
                if (bundle == null)
                {
                    throw new NullReferenceException("Cannot find Bundle for " + bundleName);
                }

                AssetBundleAssetLoadingInfo info = new AssetBundleAssetLoadingInfo(bundleInfo);
                info.AssetBundle = bundle;
                info.IsAsync     = true;
                info.Priority    = priority;
                EnqueuePendingAssetBunlde(info);
            }
        }
        /// <summary>
        /// 获取加载信息
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="assetBelongBundleName"></param>
        /// <param name="assetInBundle"></param>
        /// <param name="isAsyncLoad"></param>
        /// <returns></returns>
        private AssetRequestInfo GetPendingAssetRequestInfo(string assetName, string assetBelongBundleName, AssetBundle assetInBundle)
        {
            BundleInfoResource cachedBundleInfo = cache[assetBelongBundleName];

            if (cachedBundleInfo != null)
            {
                AssetInfo cachedAssetInfo = cachedBundleInfo[assetName];
                if (cachedAssetInfo != null && cachedAssetInfo.AssetValid)
                {
                    AssetRequestInfo requestInfo = new AssetRequestInfo(assetName, cachedAssetInfo.Asset);

                    return(requestInfo);
                }
                else
                {
                    AssetRequestInfo requestInfo = new AssetRequestInfo(assetName, assetInBundle);

                    return(requestInfo);
                }
            }
            else
            {
                AssetRequestInfo requestInfo = new AssetRequestInfo(assetName, assetInBundle);

                return(requestInfo);
            }
        }
Esempio n. 4
0
        internal override bool RemoveAsset(string name, bool force = true, bool removeInBundleAsset = true)
        {
            string             bundleName     = ResourceMgr.GetBundleName(name);
            BundleInfoResource bundleResource = cacheMgr.Cache[bundleName];

            if (bundleResource != null)
            {
                return(bundleResource.RemoveAsset(name, force, removeInBundleAsset));
            }

            return(false);
        }
Esempio n. 5
0
 public bool CacheAssetBundle(BundleInfoResource bundle)
 {
     if (!cachedBundles.ContainsKey(bundle.Name))
     {
         cachedBundles[bundle.Name] = bundle;
         cachedSize += bundle.Size;
         bundle.TouchOnLoadBundle();
         needGC = true;
         return(true);
     }
     return(false);
 }
 void MarkBundleDependency(BundleInfoResource res)
 {
     foreach (var i in res.ResourceBelongBundleInfo.BundleDependencyArray)
     {
         var bundle = cache[i];
         if (bundle != null)
         {
             res.AddDependency(bundle);
         }
         else
         {
             DebugUtil.LogError(string.Format("Cannot find dependency bundle {0} for bundle {1}.", i, res.Name));
         }
     }
 }
        void StartLoadAssets(AssetBundleAssetLoadingInfo info)
        {
            try
            {
                BundleIndexItemInfo assetBundleInfo = info.BundleIndexInfo;
                var bundle = info.AssetBundle;

                if (!bundle)
                {
                    DebugUtil.LogError(string.Format("AssetBundle {0} is null", info.BundleName));
                    return;
                }
                info.Requests = new List <AssetRequestInfo>();
                bool shouldAsync = true;
                info.IsAsync = shouldAsync;

                BundleInfoResource cachedBundleInfo = cache[info.BundleName];


                //是否全部加载
                if (bundleLoadedCallbacks.ContainsKey(info.BundleName) || info.IsLoadAllAsset)
                {
                    AssetRequestInfo bundleAllAssetsRequest = new AssetRequestInfo(info.BundleIndexInfo.BundleAssetsArray, info.AssetBundle);
                    bundleAllAssetsRequest.RequestLoadAsset(info.IsAsync);
                    info.Requests.Add(bundleAllAssetsRequest);
                }
                else
                {
                    HashSet <string> pendingBundleAssets = null;
                    if (this.m_pendingAssetIndexDict.TryGetValue(info.BundleName, out pendingBundleAssets))
                    {
                        foreach (string pendingAsset in pendingBundleAssets)
                        {
                            AssetRequestInfo requestInfo = GetPendingAssetRequestInfo(pendingAsset, info.BundleName, info.AssetBundle);
                            requestInfo.RequestLoadAsset(info.IsAsync);
                            info.Requests.Add(requestInfo);
                        }

                        this.m_pendingAssetIndexDict[info.BundleName].Clear();
                    }
                }
                assetLoading.Add(info);
            }
            catch
            {
                DebugUtil.LogError("Cannot load assetbundle:" + info.BundleName);
            }
        }
Esempio n. 8
0
        public void CacheSceneBundleDependency(BundleInfo bundle)
        {
            BundleInfoResource newRes = new BundleInfoResource();

            newRes.BundleInfo    = bundle;
            newRes.IsSceneBundle = true;
            foreach (var i in bundle.DependsOn)
            {
                BundleInfoResource res;
                if (cachedBundles.TryGetValue(i, out res))
                {
                    newRes.AddDependency(res);
                }
            }
            CacheAssetBundle(newRes);
        }
Esempio n. 9
0
        public bool CacheAssetBundle(BundleInfoResource bundle)
        {
            if (!cachedBundles.ContainsKey(bundle.Name))
            {
                cachedBundles[bundle.Name] = bundle;
                cachedSize += bundle.Size;
                bundle.Touch();

                /*#if DEBUG_BUNDLE_CACHE
                 *              DebugUtil.Log(string.Format("{2:0.##}:Bundle {0} cached, new CacheSize={1:0.##}MB", bundle.Name, cachedSize / 1024f / 1024f, Time.realtimeSinceStartup));
                 #endif*/
                needGC = true;
                return(true);
            }
            return(false);
        }
        public void OnLoadAssetBundle(string name, AssetBundle bundle, bool dependencyResource, LoadPriority priority)
        {
#if DEBUG_BUNDLE_CACHE
            ResourceMgr.Instance().SampleLoadEvent(name, 4);
#endif
            AssetBundleAssetLoadingInfo info = new AssetBundleAssetLoadingInfo();
            info.AssetBundle = bundle;
            info.BundleName  = name;
            info.Priority    = priority;
            if (!dependencyResource)
            {
                EnqueuePendingAssetBunlde(info);
            }
            else
            {
                BundleInfoResource res = new BundleInfoResource();
                res.AssetBundle = info.AssetBundle;
                res.BundleInfo  = ResourceMgr.Instance().GetBundle(info.BundleName);
                cache.CacheAssetBundle(res);
                InvokeBundleLoaded(info);
                MarkBundleDependency(res);
            }
        }
Esempio n. 11
0
        internal void GetAsset(string name, Action <string, UnityEngine.Object> callback, LoadPriority priority = LoadPriority.Default)
        {
            if (string.IsNullOrEmpty(name))
            {
                return;
            }


            if (HasLoaded(name))
            {
                if (callback != null)
                {
                    GetAssetInCache(name, callback, priority);
                }
                return;
            }

            /*
             #if UNITY_EDITOR
             *          UnityEngine.Object obj = LoadFromPrefab(name, typeof(UnityEngine.Object));
             *          if (obj != null)
             *          {
             *              SetAsset(name, obj).Reference = 1;z
             *              UnityEngine.Object ins = AssetInfo.IsNeedInstance(obj, name) ? GameObject.Instantiate(obj) : obj;
             *              if (callback != null)
             *                  callback(name, ins);
             *              return;
             *          }
             #endif*/
            string bundleName = ResourceMgr.GetBundleName(name);

            if (bundleName == string.Empty)
            {
                DebugUtil.LogError("can not find asset: " + name);
                callback?.Invoke(name, null);           //update: guangze song: 资源没找到时,回调正常调用,防止有的资源(LazyImageLoader)状态还在加载中
                return;
            }

            cacheMgr.AddCallbackToDic(name, callback);
            BundleInfoResource cachedBundleInfo = cacheMgr.Cache[bundleName];

            if (cachedBundleInfo != null)
            {
                cacheMgr.DoEnqueuePending(bundleName, LoadPriority.Default, cachedBundleInfo.AssetBundle);
            }
            else
            {
                if (!cacheMgr.HasLoadingInfo(bundleName))
                {
                    Resource res = this.GetDownloadResource(bundleName);
                    if (res == null)
                    {
                        res = this.CreateResource(bundleName, priority);
                        res.LoadRes();
                    }
                    if (res.InvalidBundle)
                    {
                        res.InvalidBundle = false;
                    }
                    //逻辑加载时,提高优先级//
                    if (res.Loader.Priority < priority)
                    {
                        this.ResourceMgr.GOELoaderMgr.SetLoaderPriority(res.Loader, priority);
                    }
                }
                else
                {
                    cacheMgr.CheckAssetLoading(bundleName, name, priority);
                }
            }
        }
Esempio n. 12
0
 public void AddDependency(BundleInfoResource bundle)
 {
     dependsOn.Add(bundle);
     bundle.referencedBy.Add(this);
     bundle.ResourceBelongBundleInfo.IsDependency = true;//被依赖的包永不能卸载AssetBundle
 }
        internal void Update()
        {
#if DEBUG_BUNDLE_CACHE
            Profiler.BeginSample("GOEAssetBundleCacheManager.Update");
#endif
            while (assetLoading.Count < MaximalLoadingAssetBundles && pendingBundle.Count > 0)
            {
                var info = pendingBundle.First.Value;
                pendingBundle.RemoveFirst();

                StartLoadAssets(info);
            }

            for (int i = 0; i < assetLoading.Count; i++)
            {
                AssetBundleAssetLoadingInfo info = assetLoading[i];

                if (info.AllDone)
                {
#if DEBUG_BUNDLE_CACHE
                    ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 6);
#endif
                    BundleInfoResource res = cache[info.BundleName];
                    bool isNewBundle       = false;

                    if (res == null)
                    {
                        RuntimeBundleInfo resourceBelongBundleInfo = ResourceMgr.Instance.GetBundleInfo(info.BundleName);
                        res             = new BundleInfoResource(resourceBelongBundleInfo);
                        res.AssetBundle = info.AssetBundle;
                        cache.CacheAssetBundle(res);
                        isNewBundle = true;
                    }
                    else
                    {
                        if (!res.AssetBundle)
                        {
                            res.AssetBundle = info.AssetBundle;
                        }
                    }

                    bool isAsync = info.IsAsync;

                    for (int j = 0; j < info.Requests.Count; j++)
                    {
                        AssetRequestInfo req = info.Requests[j];

                        for (int k = 0; k < req.Assets.Length; ++k)
                        {
                            UnityEngine.Object obj       = req.Assets[k];
                            string             assetName = req.RequestAssetsName[k];

                            if (!obj)
                            {
                                Debug.LogError($"can't load asset: {assetName}");
                                continue;
                            }


                            /*if (!assetName.StartsWithFast(obj.name))
                             *  Debug.LogError($"get object not match objname:{obj.name},request name:{assetName}");
                             * else
                             * {*/
                            AssetInfo asset = res[assetName];
                            if (asset == null)
                            {
                                asset = OnGotAsset(assetName, obj, info.BundleName);
                                res.AddAsset(assetName, asset);
                            }

                            if (asset.AssetValid && obj != asset.Asset)
                            {
                                throw new Exception($"{assetName} not valid");
                            }

                            if (obj)
                            {
                                asset.Asset = obj;
                            }

                            asset.Priority = info.Priority;
                            if (obj)
                            {
                                if (asset.NeedInstance && asset.PoolCount == 0)
                                {
                                    if (assetLoadedCallbacks.ContainsKey(assetName))
                                    {
                                        EnqueueInstantiateAsset(asset);
                                    }
                                }
                                else
                                {
                                    InvokeAssetLoaded(asset);
                                }
                            }
                            /*}*/
                        }
                    }

                    if (isNewBundle)
                    {
                        InvokeBundleLoaded(info);
                        MarkBundleDependency(res);
                    }

                    assetLoading.RemoveAt(i);
#if DEBUG_BUNDLE_CACHE
                    ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 7);
                    Profiler.EndSample();
#endif
                    break;//一个Update只删除一个已完成Bundle
                }
            }

            if (instantiationQueue.Count > 0)
            {
                var asset = instantiationQueue.First.Value;

                if (!InvokeAssetLoaded(asset))
                {
                    instantiationQueue.RemoveFirst();
                }
            }

            //不在加载资源时GC
            bool hasLoading = assetLoading.Count > 0 || pendingBundle.Count > 0 || instantiationQueue.Count > 0;
            if (!hasLoading)
            {
                cache.DoGC();
            }
#if DEBUG_BUNDLE_CACHE
            Profiler.EndSample();
#endif
        }
        internal void Update()
        {
#if DEBUG_BUNDLE_CACHE
            Profiler.BeginSample("GOEAssetBundleCacheManager.Update");
#endif
            while (assetLoading.Count < MaximalLoadingAssetBundles && pendingBundle.Count > 0)
            {
                var info = pendingBundle.First.Value;
                pendingBundle.RemoveFirst();

                StartLoadAssets(info);
            }
            for (int i = 0; i < assetLoading.Count; i++)
            {
                AssetBundleAssetLoadingInfo info = assetLoading[i];

                if (info.AllDone)
                {
#if DEBUG_BUNDLE_CACHE
                    ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 6);
#endif
                    BundleInfoResource res = cache[info.BundleName];
                    bool isNewBundle       = false;
                    if (res == null)
                    {
                        res             = new BundleInfoResource();
                        res.AssetBundle = info.AssetBundle;
                        res.BundleInfo  = ResourceMgr.Instance().GetBundle(info.BundleName);
                        cache.CacheAssetBundle(res);
                        isNewBundle = true;
                    }
                    else
                    {
                        if (!res.AssetBundle)
                        {
                            res.AssetBundle = info.AssetBundle;
                        }
                    }
                    bool isAsync = info.IsAsync;
                    for (int j = 0; j < info.Requests.Count; j++)
                    {
                        AssetRequestInfo   req = info.Requests[j];
                        UnityEngine.Object obj = isAsync && req.Request != null ? req.Request.asset : req.Asset;

                        string    assetName = req.AssetName;
                        AssetInfo asset     = null;
                        if (isNewBundle)
                        {
                            asset = OnGotAsset(assetName, obj, info.BundleName);
                            res.AddAsset(assetName, asset);
                        }
                        else
                        {
                            asset = res[assetName];
                            if (asset.AssetValid && obj != asset.Asset)
                            {
                                throw new NotSupportedException();
                            }
                            if (obj)
                            {
                                asset.Asset = obj;
                            }
                        }
                        asset.Priority = info.Priority;
                        if (obj)
                        {
                            if (asset.NeedInstance && asset.PoolCount == 0)
                            {
                                if (assetLoadedCallbacks.ContainsKey(assetName))
                                {
                                    EnqueueInstantiateAsset(asset);
                                }
                            }
                            else
                            {
                                InvokeAssetLoaded(asset);
                            }
                        }
                    }
                    if (isNewBundle)
                    {
                        InvokeBundleLoaded(info);
                        MarkBundleDependency(res);
                    }

                    assetLoading.RemoveAt(i);
#if DEBUG_BUNDLE_CACHE
                    ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 7);
                    Profiler.EndSample();
#endif
                    break;//一个Update只删除一个已完成Bundle
                }
            }

            if (instantiationQueue.Count > 0)
            {
                var asset = instantiationQueue.First.Value;

                if (!InvokeAssetLoaded(asset))
                {
                    instantiationQueue.RemoveFirst();
                }
            }

            //不在加载资源时GC
            bool hasLoading = assetLoading.Count > 0 || pendingBundle.Count > 0 || instantiationQueue.Count > 0;
            if (!hasLoading)
            {
                cache.DoGC();
            }
#if DEBUG_BUNDLE_CACHE
            Profiler.EndSample();
#endif
        }