Example #1
0
        private AssetBundle LoadAssetBundle(string ABName)
        {
            AssetBundleItem item = null;
            uint            crc  = Crc32.GetCrc32(ABName);

            if (mAssetBundleItemDic.TryGetValue(crc, out item) && item != null)
            {
                item.Retain();
            }
            else
            {
                AssetBundle assetBundle = null;
                string      fullPath    = ABDataHolder.Instance.GetABPrefix() + ABName;
                if (ABDataHolder.Instance.JudgeCanLoadAB(fullPath))
                {
                    assetBundle = AssetBundle.LoadFromFile(fullPath);
                }
                else
                {
                    AFLogger.e("不存在AB包路径 :" + ABName + " " + fullPath);
                }

                if (assetBundle == null)
                {
                    AFLogger.e(" Load AssetBundle Error:" + ABName + " " + fullPath);
                }

                item = SafeObjectPool <AssetBundleItem> .Instance.Allocate();

                item.assetBundle = assetBundle;
                item.Retain();
                mAssetBundleItemDic.Add(crc, item);
            }
            return(item.assetBundle);
        }
Example #2
0
        /// <summary>
        /// 获取进度
        /// </summary>
        /// <param name="list"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public float GetListProcess(List <string> list, float unit)
        {
            float process = 0;

            list.ForEach((str) =>
            {
                process += mResDictionary[Crc32.GetCrc32(str)].Process;
            });
            return(process * unit);
        }
Example #3
0
 public virtual void Init(ResLoadInfo resLoadInfo)
 {
     mResPath        = resLoadInfo.mResPath;
     mLoadRePriority = resLoadInfo.loadResPriority;
     DestroyCache    = resLoadInfo.DestroyCache;
     isSprite        = resLoadInfo.mIsSprite;
     resItem         = new ResItem();
     resItem.mCrc    = Crc32.GetCrc32(mResPath);
     mListener       = resLoadInfo.mListener;
     mResState       = ResState.Waiting;
 }
Example #4
0
        public bool ReleaseResouce(string ResPath)
        {
            ResInfo item = null;

            if (!mResDictionary.TryGetValue(Crc32.GetCrc32(ResPath), out item) || null == item)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + ResPath + "  可能释放了多次");
            }
            DestoryResouceItme(item, item.DestroyCache);
            return(true);
        }
Example #5
0
        public static ResLoadInfo Allocate(ObjLoadInfo objLoadInfo)
        {
            ResLoadInfo resLoadInfo = SafeObjectPool <ResLoadInfo> .Instance.Allocate();

            resLoadInfo.mResPath     = objLoadInfo.mResPath;
            resLoadInfo.mResFromType = objLoadInfo.mResFromType;
            resLoadInfo.mIsSprite    = false;
            resLoadInfo.mListener    = objLoadInfo.loadResCall;
            resLoadInfo.mCRC         = Crc32.GetCrc32(objLoadInfo.mResPath);
            resLoadInfo.DestroyCache = objLoadInfo.mClear;
            return(resLoadInfo);
        }
Example #6
0
        public static ObjLoadInfo Allocate(ResFromType resType, string assetPath, Transform ObjParentTrans,
                                           bool mclear = false, LoadResPriority loadResPriority = LoadResPriority.RES_NUM,
                                           Action <bool, ResInfo> loadResCall = null, Action <bool, ResObject> loadObjCall = null)
        {
            ObjLoadInfo objLoadInfo = SafeObjectPool <ObjLoadInfo> .Instance.Allocate();

            objLoadInfo.mCRC            = Crc32.GetCrc32(assetPath);
            objLoadInfo.mResFromType    = resType;
            objLoadInfo.ObjParentTrans  = ObjParentTrans;
            objLoadInfo.mClear          = mclear;
            objLoadInfo.mResPath        = assetPath;
            objLoadInfo.loadResPriority = loadResPriority;
            objLoadInfo.loadResCall     = loadResCall;
            objLoadInfo.loadObjCall     = loadObjCall;
            return(objLoadInfo);
        }
Example #7
0
        /// <summary>
        /// 这里的mListen与调用loader传的参数并不相同
        /// </summary>
        /// <param name="resType"></param>
        /// <param name="assetPath"></param>
        /// <param name="isSprite"></param>
        /// <param name="mListen"></param>
        /// <returns></returns>
        public static ResLoadInfo Allocate(ResFromType resType, string assetPath, bool isSprite,
                                           Action <bool, ResInfo> mListen  = null, Action <bool, ResInfo> mRealListener = null, bool DestroyCache = false,
                                           LoadResPriority loadResPriority = LoadResPriority.RES_NUM)
        {
            ResLoadInfo resLoadInfo = SafeObjectPool <ResLoadInfo> .Instance.Allocate();

            resLoadInfo.mResPath        = assetPath;
            resLoadInfo.mResFromType    = resType;
            resLoadInfo.mListener       = mListen;
            resLoadInfo.mIsSprite       = isSprite;
            resLoadInfo.DestroyCache    = DestroyCache;
            resLoadInfo.loadResPriority = loadResPriority;
            resLoadInfo.mCRC            = Crc32.GetCrc32(assetPath);
            resLoadInfo.mRealListener   = mRealListener;
            return(resLoadInfo);
        }
Example #8
0
        /// <summary>
        /// 不需要实例化的资源卸载,根据路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="destoryObj"></param>
        /// <returns></returns>
        public bool ReleaseResouce(string path, bool destoryObj)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            ResInfo item = null;

            if (!mResDictionary.TryGetValue(Crc32.GetCrc32(path), out item) || null == item)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + path + "  可能释放了多次");
                return(false);
            }
            DestoryResouceItme(item, destoryObj);
            return(true);
        }
Example #9
0
        private void UnLoadAssetBundle(string ABName)
        {
            AssetBundleItem item = null;
            uint            crc  = Crc32.GetCrc32(ABName);

            if (mAssetBundleItemDic.TryGetValue(crc, out item) && item != null)
            {
                item.Release();
                if (item.RefCount <= 0 && item.assetBundle != null)
                {
                    item.assetBundle.Unload(true);
                    item.Recycle2Cache();
                    SafeObjectPool <AssetBundleItem> .Instance.Recycle(item);

                    mAssetBundleItemDic.Remove(crc);
                }
            }
        }
Example #10
0
        public override IEnumerator DoLoadAsync(Action <uint> finishCallback)
        {
            if (mResPath.IsNullOrEmpty())
            {
                AFLogger.e("加载路径不能为空");
                yield break;
            }
            mResState = ResState.Loading;
            uint      crc       = Crc32.GetCrc32(mResPath);
            ABResItem ABResitem = ResManager.Instance.LoadAssetBundle(crc);

            if (ABResitem.assetBundle == null)
            {
                AFLogger.e("加载的AssetBundle为空:" + ResPath);
                yield break;
            }
            resItem.Copy(ABResitem);
            if (isSprite)
            {
                ABRequest = ABResitem.assetBundle.LoadAssetAsync <Sprite>(ResPath);
            }
            else
            {
                ABRequest = ABResitem.assetBundle.LoadAssetAsync <Object>(ResPath);
            }
            yield return(ABRequest);

            if (!ABRequest.isDone)
            {
                AFLogger.e("Failed to Load Resources:" + mResPath);
                OnResLoadFaild();
                mListener.InvokeGracefully(false, this);
                finishCallback(mCRC);
                yield break;
            }

            mAsset    = ABRequest.asset;
            mResState = ResState.Ready;
            mListener.InvokeGracefully(true, this);
            finishCallback(mCRC);
        }
Example #11
0
        public override bool LoadSync()
        {
            if (mResPath.IsNullOrEmpty())
            {
                AFLogger.e("加载路径不能为空");
                return(false);
            }
            mResState = ResState.Loading;
            uint      crc       = Crc32.GetCrc32(mResPath);
            ABResItem ABResitem = ResManager.Instance.LoadAssetBundle(crc);

            if (ABResitem.assetBundle == null)
            {
                AFLogger.e("加载的AssetBundle为空:" + ResPath);
                return(false);
            }
            resItem.Copy(ABResitem);
            if (isSprite)
            {
                mAsset = ABResitem.assetBundle.LoadAsset <Sprite>(ResPath);
            }
            else
            {
                mAsset = ABResitem.assetBundle.LoadAsset <Object>(ResPath);
            }
            if (mAsset.IsNull())
            {
                AFLogger.e("加载AB包资源为null,请检查路径:" + mResPath);
                mResState = ResState.Waiting;
            }
            else
            {
                mResState = ResState.Ready;
            }
            return(mAsset != null);
        }
Example #12
0
        //写入数据
        //resPathDic : 路径对应的AB包
        static void WriteData(Dictionary <string, string> resPathDic, ConfigWritingMode configWritingMode, PackageABType packageABType)
        {
            AssetBundleConfig config = new AssetBundleConfig();

            config.ABList = new List <ABBase>();
            foreach (string path in resPathDic.Keys)
            {
                //进行赋值
                ABBase abBase = new ABBase();
                abBase.Path       = path;
                abBase.Crc        = Crc32.GetCrc32(path);
                abBase.ABName     = resPathDic[path];
                abBase.AssetName  = path.Remove(0, path.LastIndexOf("/", System.StringComparison.Ordinal) + 1);
                abBase.ABDependce = new List <string>();
                string[] resDependce = AssetDatabase.GetDependencies(path);
                for (int i = 0; i < resDependce.Length; i++)
                {
                    string tempPath = resDependce[i];
                    //排除自身与脚本文件
                    if (tempPath == path || path.EndsWith(".cs", System.StringComparison.Ordinal))
                    {
                        continue;
                    }

                    string abName = "";
                    //所依赖的资源是不是在其他AB包里面
                    if (resPathDic.TryGetValue(tempPath, out abName))
                    {
                        if (abName == resPathDic[path])
                        {
                            continue;
                        }
                        //判断是否已经添加到aBBase包里面
                        if (!abBase.ABDependce.Contains(abName))
                        {
                            abBase.ABDependce.Add(abName);
                        }
                    }
                }
                config.ABList.Add(abBase);
            }
            switch (configWritingMode)
            {
            case ConfigWritingMode.TXT:
                string txtPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/AssetbundleConfig.txt";
                if (File.Exists(txtPath))
                {
                    File.Delete(txtPath);
                }
                FileStream   fileStream1 = new FileStream(txtPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamWriter sw1         = new StreamWriter(fileStream1, System.Text.Encoding.UTF8);
                string       configJson  = SerializeHelper.ToJson(config);
                sw1.Write(configJson);
                sw1.Close();
                fileStream1.Close();
                break;

            case ConfigWritingMode.XML:
                CreateXML(config, packageABType);
                break;

            case ConfigWritingMode.Binary:
                foreach (ABBase abBase in config.ABList)
                {
                    abBase.Path = "";
                }
                string     BinaryPath = "Assets/AssetbundleConfig.bytes";
                FileStream fs         = new FileStream(PathTool.ProjectPath + BinaryPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                fs.Seek(0, SeekOrigin.Begin);
                fs.SetLength(0);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, config);
                fs.Close();
                CreateXML(config, packageABType);
                AssetDatabase.Refresh();
                SetABName("assetbundleconfig", BinaryPath);
                break;
            }
        }
Example #13
0
 /// <summary>
 /// 缓存加载好的资源
 /// </summary>
 /// <param name="ResPath"></param>
 /// <param name="resInfo"></param>
 public void CacheResource(string ResPath, ResInfo resInfo)
 {
     //缓存太多,清除最早没有使用的资源
     WashOut();
     mResDictionary.AddValue <uint, ResInfo>(Crc32.GetCrc32(ResPath), resInfo);
 }
Example #14
0
 /// <summary>
 /// 取消异步加载资源
 /// </summary>
 /// <returns></returns>
 public bool CancleLoadRes(string resPath)
 {
     return(ResManager.Instance.CancelResLoad(Crc32.GetCrc32(resPath)));
 }