Beispiel #1
0
        void UnloadAssetBundleInternal(string abName, bool isThorough)
        {
            AssetBundleInfo bundle = GetLoadedAssetBundle(abName);

            if (bundle == null)
            {
                return;
            }

            if (--bundle.m_ReferencedCount <= 0)
            {
                if (m_LoadRequests.ContainsKey(abName))
                {
                    return;     //如果当前AB处于Async Loading过程中,卸载会崩溃,只减去引用计数即可
                }
                bundle.m_AssetBundle.Unload(isThorough);
                m_LoadedAssetBundles.Remove(abName);
                Debug.Log(abName + " has been unloaded successfully");
            }
        }
Beispiel #2
0
        IEnumerator OnLoadAssetBundle(string abName, Type type)
        {
            string url = m_BaseDownloadingURL + abName;

            WWW download = null;

            if (type == typeof(AssetBundleManifest))
            {
                download = new WWW(url);
            }
            else
            {
                string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName);
                if (dependencies.Length > 0)
                {
                    m_Dependencies.Add(abName, dependencies);
                    for (int i = 0; i < dependencies.Length; i++)
                    {
                        string          depName    = dependencies[i];
                        AssetBundleInfo bundleInfo = null;
                        if (m_LoadedAssetBundles.TryGetValue(depName, out bundleInfo))
                        {
                            bundleInfo.m_ReferencedCount++;
                        }
                        else if (!m_LoadRequests.ContainsKey(depName))
                        {
                            yield return(StartCoroutine(OnLoadAssetBundle(depName, type)));
                        }
                    }
                }
                download = WWW.LoadFromCacheOrDownload(url, m_AssetBundleManifest.GetAssetBundleHash(abName), 0);
            }
            yield return(download);

            AssetBundle assetObj = download.assetBundle;

            if (assetObj != null)
            {
                m_LoadedAssetBundles.Add(abName, new AssetBundleInfo(assetObj));
            }
        }
Beispiel #3
0
        /// <summary>
        /// AB同步方式加载资源
        /// </summary>
        /// <param name="abName">ab包全名,如Assets/_Project/Prefab/prefab.u3d</param>
        /// 场景ab包后缀是.u3d,其余的都是.ab
        /// <returns></returns>
        private AssetBundle LoadAssetsFromAB(string abName)
        {
            abName = abName.ToLower();

            //查找AB包本体
            AssetBundleInfo assetBundleInfo = null;

            if (m_LoadAssetBundle.TryGetValue(abName, out assetBundleInfo)) //检查是否已加载
            {
                assetBundleInfo.m_ReferencedCount++;                        //引用计数+1
            }
            else
            {
                var loadPath = PathUtil.DataPath + PathUtil.ABRootPath + abName;
                var reAB     = AssetBundle.LoadFromFile(loadPath);
                if (reAB == null)
                {
                    Debug.LogError("[ResManager] AB包:" + abName + ",不存在:" + loadPath);
                }
                else
                {
                    assetBundleInfo = new AssetBundleInfo(reAB);
                    m_LoadAssetBundle.Add(abName, assetBundleInfo);         //加入已加载列表
                }
            }

            //加载依赖包
            if (m_AssetBundleManifest != null)
            {
                string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName);
                foreach (var dep in dependencies)
                {
                    LoadAssetsFromAB(dep);
                }
            }

            return(assetBundleInfo.m_AssetBundle);
        }
Beispiel #4
0
        IEnumerator OnLoadAssetsFromABAsync(string abName, Action <AssetBundle> action)
        {
            abName = abName.ToLower();

            //加载AB包本体
            AssetBundleInfo assetBundleInfo = null;

            if (m_LoadAssetBundle.TryGetValue(abName, out assetBundleInfo)) //检查是否已加载
            {
                assetBundleInfo.m_ReferencedCount++;                        //引用计数+1

                if (action != null)
                {
                    action.Invoke(assetBundleInfo.m_AssetBundle);
                }

                //依赖计数+1
                if (m_AssetBundleManifest != null)
                {
                    string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName);
                    foreach (var dep in dependencies)
                    {
                        yield return(StartCoroutine(OnLoadAssetsFromABAsync(dep, null)));
                    }
                }

                yield break;
            }
            else
            {
                if (m_nowLodingList.ContainsKey(abName))     //该资源正在被加载
                {
                    //加入多播委托列表
                    if (action != null)
                    {
                        m_nowLodingList[abName].m_CallBack += action;
                    }

                    //以委托的形式加入回调方法,在资源完成加载后增加引用计数
                    m_nowLodingList[abName].m_CallBack += delegate
                    {
                        //增加引用计数
                        m_LoadAssetBundle[abName].m_ReferencedCount++;
                        //依赖计数+1
                        string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName);
                        foreach (var dep in dependencies)
                        {
                            m_LoadAssetBundle[dep].m_ReferencedCount++;
                        }
                    };

                    Debug.Log("[ResManager] AB包:" + abName + ",正在被加载!");
                    yield break;
                }
                else
                {
                    //加入加载列表
                    if (action != null)
                    {
                        m_nowLodingList.Add(abName, new LoadingInfo(action));
                    }

                    //异步加载资源
                    var loadPath  = PathUtil.DataPath + PathUtil.ABRootPath + abName;
                    var abRequest = AssetBundle.LoadFromFileAsync(loadPath);
                    yield return(abRequest);

                    var reAB = abRequest.assetBundle;
                    if (reAB == null)
                    {
                        Debug.LogError("[ResManager] AB包:" + abName + ",不存在:" + loadPath);
                    }
                    else
                    {
                        assetBundleInfo = new AssetBundleInfo(reAB);
                        m_LoadAssetBundle.Add(abName, assetBundleInfo);         //加入已加载列表
                    }

                    //加载依赖包
                    if (m_AssetBundleManifest != null)
                    {
                        string[] dependencies = m_AssetBundleManifest.GetAllDependencies(abName);
                        foreach (var dep in dependencies)
                        {
                            yield return(StartCoroutine(OnLoadAssetsFromABAsync(dep, null)));
                        }
                    }

                    if (m_nowLodingList.ContainsKey(abName) && m_nowLodingList[abName].m_UseCallBack)
                    {
                        var actionList = m_nowLodingList[abName].m_CallBack.GetInvocationList(); //获取委托列表
                        m_nowLodingList.Remove(abName);                                          //记得一定要从队列中移除
                        //最后完成委托调用!
                        foreach (Action <AssetBundle> a in actionList)
                        {
                            if (a != null)
                            {
                                a.Invoke(assetBundleInfo.m_AssetBundle);
                            }
                        }
                    }
                }
            }
        }