コード例 #1
0
        ResPackInfo GetResPackInfo(ResObj res)
        {
            if (null == _packConfig)
            {
                return(null);
            }

            return(_packConfig.GetPackInfoForResource(res.Path));
        }
コード例 #2
0
        /// <summary>
        /// 回收
        /// </summary>
        /// <param name="r"></param>
        public static void Recycle(ResObj r)
        {
            if (r != null)
            {
                r.Reset(null);

                if (!_pool.Contains(r))
                {
                    _pool.Add(r);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// 同步加载资源
        /// </summary>
        /// <param name="ResObj">资源实例</param>
        private void LoadResource(ResObj resource)
        {
            //找配置
            ResPackInfo packInfo = GetResPackInfo(resource);

            if (packInfo != null)
            {
                if (packInfo.GetPackType() == (byte)ResPackType.ResPackTypeBundle)
                {
                    BundlePackInfo pi = packInfo as BundlePackInfo;
                    //跟随Resource
                    if (!pi.Outside && pi.IsNoBundle())
                    {
                        // load from Resources
                        resource.Load();
                    }
                    else
                    {
                        // load from bundle
                        // load bundle
                        BundleService.GetInstance().LoadSync(pi);
                        // load asset from bundle
                        AssetBundle bundle = BundleService.GetInstance().GetBundle(packInfo.Path);
                        if (bundle != null)
                        {
                            if (string.IsNullOrEmpty(resource.Ext))
                            {
                                resource.Ext = pi.GetResExt(resource.Path);
                            }
                            resource.Load(bundle);
                        }
                        else
                        {
                            JW.Common.Log.LogE("Loading bundle failed for path:{0}, bundle path:{1}", resource.Path, packInfo.Path);
                        }
                    }
                }
                else
                {
                    //二进制类型 直接根据文件路径 获取
                    resource.Load(packInfo.Path, packInfo.IsOutside(resource.Path));
                }
            }
            else
            {
                //不从属于任何资源包, 从Resources目录下读取 Editor模式 或者保护为未打包的
                resource.Load();
            }
        }
コード例 #4
0
        /// <summary>
        /// 释放资源实例 一般二进制文件获取后 就需要卸载
        /// </summary>
        /// <param name="resource">资源实例</param>
        /// <param name="immediately">是否立即释放</param>
        private void _UnloadResource(ResObj resource, bool immediately)
        {
            if (resource == null)
            {
                return;
            }

            string path = resource.Path;

            if (_resCache.Remove(resource, immediately))
            {
                // 未使用cache资源,说明bundle已经被卸载了
                if (!resource.UsingCache)
                {
                    UnloadBundle(path);
                }
            }
        }
コード例 #5
0
        // 取实例
        public static ResObj Get(string path)
        {
            ResObj r = null;

            if (_pool.Count > 0)
            {
                r = _pool[_pool.Count - 1];
                r.Reset(path);

                _pool.RemoveAt(_pool.Count - 1);
            }
            else
            {
                r = new ResObj(path);
            }

            return(r);
        }
コード例 #6
0
        /// <summary>
        /// 添加资源到缓存
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <returns>创建的资源对象</returns>
        public ResObj Add(string path)
        {
            ResObj resource;

            if (_resources.TryGetValue(path, out resource))
            {
                if (resource.RefCnt == 0)
                {
                    resource.UsingCache = true;
                }

                resource.RefCnt++;
            }
            else
            {
                resource = ResObj.Get(path);
                _resources.Add(path, resource);
            }

            return(resource);
        }
コード例 #7
0
        /// <summary>
        /// 同步方式获取资源,如果是打包资源自动加载对应bundle
        /// </summary>
        /// <param name="fullPathInResources">资源在Resources目录下的相对路径</param>
        /// <returns></returns>
        private ResObj _GetResource(string fullPathInResources)
        {
            if (string.IsNullOrEmpty(fullPathInResources))
            {
                return(null);
            }

            ResObj resource = _resCache.Add(fullPathInResources);

            if (null == resource.Content)
            {
                LoadResource(resource);
                if (resource.Content == null)
                {
                    JW.Common.Log.LogE("Load resource failed, path:{0}", fullPathInResources);
                    _resCache.Remove(resource, true);
                    return(null);
                }
            }

            return(resource);
        }
コード例 #8
0
        /// <summary>
        /// 移除资源对象
        /// </summary>
        /// <param name="resource">资源对象</param>
        /// <param name="immediately">是否立即删除</param>
        /// <returns>true:表示所有同路径资源都已移除完</returns>
        public bool Remove(ResObj resource, bool immediately)
        {
            if (resource == null)
            {
                return(true);
            }

            if (_resources.ContainsKey(resource.OriginPath))
            {
                if (immediately)
                {
                    resource.Unload();
                    _resources.Remove(resource.OriginPath);
                    ResObj.Recycle(resource);

                    return(true);
                }
                else
                {
                    if (resource.RefCnt > 0)
                    {
                        resource.RefCnt--;
                    }
                    else
                    {
                        JW.Common.Log.LogE("Unloading resource [{0}] while its reference count is zero.", resource.Path);
                    }

                    return(resource.RefCnt <= 0);
                }
            }
            else
            {
                resource.Unload();
                ResObj.Recycle(resource);
                return(true);
            }
        }
コード例 #9
0
        /// <summary>
        /// 卸载资源
        /// </summary>
        public void UnloadUnusedResources(JWObjList <string> willUseAssets = null)
        {
            JWObjList <string> unloaded = null;

            var itor = _resources.GetEnumerator();

            while (itor.MoveNext())
            {
                if (itor.Current.Value.RefCnt <= 0)
                {
                    if (willUseAssets != null && willUseAssets.Contains(itor.Current.Key))
                    {
                        willUseAssets.Remove(itor.Current.Key);
                        continue;
                    }

                    if (unloaded == null)
                    {
                        unloaded = new JWObjList <string>();
                    }

                    unloaded.Add(itor.Current.Key);

                    // unload
                    itor.Current.Value.Unload();

                    // recycle ResObj instance 回池
                    ResObj.Recycle(itor.Current.Value);
                }
            }

            for (int i = 0; unloaded != null && i < unloaded.Count; i++)
            {
                _resources.Remove(unloaded[i]);
            }
        }
コード例 #10
0
 /// <summary>
 /// UnloadResource的静态封装
 /// </summary>
 /// <param name="ResObj"></param>
 /// <param name="immediately">是否立即释放</param>
 public static void UnloadResource(ResObj ResObj, bool immediately = false)
 {
     GetInstance()._UnloadResource(ResObj, immediately);
 }
コード例 #11
0
        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <returns></returns>
        public ResObjRequest GetResourceAsync(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            // request
            ResObjRequest rr = new ResObjRequest();
            // complete loading
            Action <ResObj> complete = (ResObj r) =>
            {
                // try repaire
                //if (r.Content is GameObject)
                //{
                //    _repaireMachine.Repaire(r.Content as GameObject);
                //}
                rr.isDone   = true;
                rr.resource = r;
            };

            // loading progress
            Action <float> progress = (float prog) =>
            {
                rr.progress = prog;
            };

            // cache
            ResObj resource = _resCache.Add(path);

            if (resource.Content != null)
            {
                //cache包含直接返回
                complete(resource);
                return(rr);
            }

            // real loading
            ResPackInfo packInfo = GetResPackInfo(resource);

            if (packInfo != null)
            {
                //打包的资源AssetBundle
                if (packInfo.GetPackType() == (byte)ResPackType.ResPackTypeBundle)
                {
                    //AssetBundle 信息
                    BundlePackInfo pi = packInfo as BundlePackInfo;
                    //补充扩展名
                    if (string.IsNullOrEmpty(resource.Ext))
                    {
                        resource.Ext = pi.GetResExt(resource.Path);
                    }

                    // AssetBundle 缓存
                    AssetBundle bundle = BundleService.GetInstance().GetBundle(packInfo.Path);
                    if (bundle == null)
                    {
                        //不在外且跟随Resource出档了
                        if (!pi.Outside && pi.IsNoBundle())
                        {
                            // in Resources
                            StartCoroutine(resource.LoadAsync(complete, progress));
                        }
                        else
                        {
                            // load bundle first
                            StartCoroutine(BundleService.GetInstance().LoadAsync(pi, delegate(BundleRef bundleRef)
                            {
                                if (bundleRef != null && bundleRef.Bundle != null)
                                {
                                    bundle = bundleRef.Bundle;
                                    //从asset bundle 加载
                                    StartCoroutine(resource.LoadAsync(bundle, complete, progress));
                                }
                                else
                                {
                                    JW.Common.Log.LogE("Async loading bundle failed, resource:{0}, bundle:{1}", path, packInfo.Path);
                                    complete(null);
                                    _resCache.Remove(resource, true);
                                }
                            }, delegate(BundlePackInfo failedPackInfo, string error)
                            {
                                JW.Common.Log.LogE("Load bundle failed, error:{0}", error);
                                complete(null);
                                _resCache.Remove(resource, true);
                            }));
                        }
                    }
                    else
                    {
                        // bundle is exists
                        // 增加引用计数
                        BundleService.GetInstance().LoadSync(pi);
                        // 加载
                        StartCoroutine(resource.LoadAsync(bundle, complete, progress));
                    }
                }
                else
                {
                    //二进制文件包异步获取
                    StartCoroutine(resource.LoadAsync(packInfo.Path, packInfo.IsOutside(resource.Path), complete, progress));
                }
            }
            else
            {
                // 非IFS 打包资源,从Resources目录异步读取
                //JW.Common.Log.LogD("Load From Resource Async:"+ path);
                StartCoroutine(resource.LoadAsync(complete, progress));
            }
            return(rr);
        }