Exemplo n.º 1
0
        /// <summary>
        /// 强制异步为同步加载
        /// </summary>
        /// <param name="obj"></param>
        private void ForceLoadAsyncToSync(AssetBundleObject obj)
        {
            if (obj.Request != null)
            {
                obj.AssetBundle = obj.Request.assetBundle;

                obj.Request = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 同步加载
        /// </summary>
        /// <param name="abName"></param>
        /// <returns></returns>
        private AssetBundleObject LoadAssetBundleSync(string abName)
        {
            AssetBundleObject obj = null;

            var status = GetobjStatus(abName);

            if (status == objStatus.None) //  未加载
            {
                obj = Createobj(abName, false);

                DoLoad(obj, false);

                PutobjInQueue(objStatus.Loaded, obj);

                return(obj);
            }

            obj = GetobjFromQueue(abName);

            if (obj == null)
            {
                Debug.Log("obj no found!!! -> " + abName);

                return(null);
            }

            obj.RefCount++;

            foreach (var o in obj.Depends) // 加载依赖项
            {
                LoadAssetBundleSync(o.HashName);
            }

            PutobjInQueue(objStatus.Loaded, obj);

            if (status == objStatus.Loaded)
            {
                return(obj);                            // 已加载
            }
            if (status == objStatus.Ready)
            {
                DoLoad(obj, false);
            }
            else if (status == objStatus.Loading) // 强制异步为同步加载
            {
                ForceLoadAsyncToSync(obj);
            }

            DoLoadedCallFun(obj, false);

            return(obj);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isAsync">是否异步</param>
        private void DoLoad(AssetBundleObject obj, bool isAsync = true)
        {
            string assetName = obj.HashName.ToLower() + ".ab";
            string dir       = Utility.GetAssetBundlePath(assetName);

            if (isAsync)
            {
                obj.Request = AssetBundle.LoadFromFileAsync(dir);
            }
            else
            {
                obj.AssetBundle = AssetBundle.LoadFromFile(dir);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 计算某个资源包的引用计数
        /// </summary>
        /// <param name="obj"></param>
        private void DoDependsRef(AssetBundleObject obj)
        {
            obj.RefCount++;

            if (obj.Depends.Count == 0)
            {
                return;
            }

            foreach (var dpObj in obj.Depends)
            {
                DoDependsRef(dpObj); //递归依赖项,加载完
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 卸载资源
        /// </summary>
        /// <param name="obj"></param>
        private void DoUnLoad(AssetBundleObject obj)
        {
            // 这里用true,卸载Asset内存,实现指定卸载
            if (obj.AssetBundle == null)
            {
                string errormsg = string.Format("LoadAssetbundle DoUnload error ! assetName:{0}", obj.HashName);

                Debug.LogError(errormsg);

                return;
            }

            obj.AssetBundle.Unload(true);

            obj.AssetBundle = null;
        }
Exemplo n.º 6
0
        /// <summary>
        /// 将资源包加入某个队列
        /// </summary>
        /// <param name="status"></param>
        /// <param name="obj"></param>
        private void PutobjInQueue(objStatus status, AssetBundleObject obj)
        {
            var hashName = obj.HashName;

            var curStatus = GetobjStatus(hashName);

            // 清除上一个状态
            if (curStatus != objStatus.None)
            {
                GetQueue(curStatus).Remove(hashName);
            }

            if (status != objStatus.None)
            {
                GetQueue(status).Add(obj.HashName, obj);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        // 异步加载
        /// </summary>
        /// <param name="abName"></param>
        /// <param name="callFun"></param>
        private AssetBundleObject LoadAssetBundleAsync(string abName, AssetBundleLoadCallBack callFun)
        {
            AssetBundleObject obj = null;

            var status = GetobjStatus(abName);

            //  未加载过
            if (status == objStatus.None)
            {
                obj = Createobj(abName);

                obj.CallFunList.Add(callFun);

                // 移入准备队列
                PutobjInQueue(objStatus.Ready, obj);

                return(obj);
            }

            obj = GetobjFromQueue(abName);

            if (obj == null)
            {
                Debug.Log("obj no found!!! -> " + abName);

                return(null);
            }

            DoDependsRef(obj);

            if (status == objStatus.Loaded) // 已加载
            {
                callFun?.Invoke(obj.AssetBundle);
            }
            else
            {
                obj.CallFunList.Add(callFun);
            }

            return(obj);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 加载资源后的回调,(暂未提供服务器下载资源)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isAsync"></param>
        private void DoLoadedCallFun(AssetBundleObject obj, bool isAsync = true)
        {
            if (obj.AssetBundle == null) // 如果还没加载到ab包, 网上下载,先同步下载
            {
            }

            if (obj.AssetBundle == null) // 未同步下载到,直接移除
            {
                PutobjInQueue(objStatus.None, obj);
            }

            if (obj.AssetBundle == null && isAsync) // 异步下载
            {
            }

            foreach (var callback in obj.CallFunList)
            {
                callback?.Invoke(obj.AssetBundle);
            }

            Debug.Log(obj.HashName);

            obj.CallFunList.Clear();
        }
Exemplo n.º 9
0
        /// <summary>
        /// 创建新的异步资源包
        /// </summary>
        /// <param name="abName"></param>
        /// <returns></returns>
        private AssetBundleObject Createobj(string abName, bool isAsync = true)
        {
            var obj = new AssetBundleObject()
            {
                RefCount = 1,

                HashName = abName,

                DependLoadingCount = 0,
            };

            if (dependsDataDic.ContainsKey(abName))
            {
                var dps = dependsDataDic[abName];

                if (dps != null && dps.Length > 0)
                {
                    // 异步加载,需要加载完所有依赖项
                    if (isAsync)
                    {
                        obj.DependLoadingCount = dps.Length;
                    }

                    AssetBundleObject dpObj = null;

                    foreach (var d in dps)
                    {
                        if (!isAsync) // 同步
                        {
                            dpObj = LoadAssetBundleSync(d);
                        }
                        else
                        {
                            dpObj = LoadAssetBundleAsync(d, (ab) =>
                            {
                                if (obj.DependLoadingCount <= 0)
                                {
                                    return;
                                }

                                obj.DependLoadingCount--;

                                if (obj.DependLoadingCount == 0 && obj.Request != null && obj.Request.isDone) // 立即执行
                                {
                                    DoLoadedCallFun(obj);
                                }
                            });
                        }

                        if (dpObj == null)
                        {
                            Debug.Log("curobj is null!!!");
                        }
                        else
                        {
                            obj.Depends.Add(dpObj);
                        }
                    }
                }
            }

            return(obj);
        }