Unity中加载AB包(本地加载)
下载AssetBundles插件下载地址https://gitee.com/icejacool/AssetBundles-Browser下载成功之后解压文件。解压之后的文件拖入Unity的Asset文件夹下面。应该会有报错删除AssetBundles-Browser-master文件夹下面的Tests文件夹配置资源(场景)1.选中想要打成AB包的场景在右下角AssetBundle的位置新建标签以test为例2.点击Window-AssetBundleBrowser打开打包AB包的界面刷新界面显示所有的AB包确保显示的是你想要打包的场景3.点击Build页签设置打包相关信息选择目标平台我选的是Android勾选ClearFolders,每次打包的时候删除之前打包的AB包无缓存重新打包选择Compression(压缩方式)NoCompression:无压缩加载速度最快包体最大LZMA:压缩最狠加载速度最慢解压需要占的内存也最多包体最小LZ4:微压缩加载速度较快包体占三种里的中等(我是本地加载包体压缩不是最重要所以选了这个)4.打包完成之后在Assets的同级文件夹会出现AssetBundles文件夹里面是对应平台的AB包加载AB包获取加载地址//获取加载AB包的地址 private string GetBundlePath(string sceneName) { string path; #if UNITY_EDITOR path Application.dataPath.Replace(/Assets, ) /AssetBundles/Android/; #else path Application.persistentDataPath /; #endif switch (sceneName) { //根据场景名字加载不同的AB包 case env_04: path path test; break; } return path; }异步加载AB包先定义一个AssetBudle,再进行加载赋值private AssetBundle assetBundle; private IEnumerator LoadAssetBundleAsync(string sceneName, ActionAssetBundle callback,int index1) { Debug.LogError(开始加载ab包.... sceneName); string bundlePath GetBundlePath(sceneName); AssetBundleCreateRequest request AssetBundle.LoadFromFileAsync(bundlePath); while (!request.isDone) { //Debug.LogError(AB加载中进度为 request.progress); //这里可以放进度条之类的提示 yield return null; } assetBundle request.assetBundle; if (assetBundle ! null) { // 缓存AB包 callback?.Invoke(assetBundle); } else { Debug.LogError($无法加载AB包: {bundlePath}); callback?.Invoke(null); } }加载附加场景我的项目中设定的是Loading场景一直存在其他场景叠加显示所以流程是先卸载当前场景再加载AB包最后再加载场景private IEnumerator LoadNextSceneCoroutine(string _sceneName, Action _loadCompleteEvent null, Action _completeEveent null) { //这块是显示UI进度条 yield return StartCoroutine(ShowLoadingUI()); #region 卸载场景 Scene currentScene SceneManager.GetActiveScene(); AsyncOperation unloadOp SceneManager.UnloadSceneAsync(currentScene); Debug.LogError(开始卸载); while (!unloadOp.isDone) { Debug.LogError(卸载中......); yield return null; } #endregion // 执行GC AssetBundle.UnloadAllAssetBundles(true); yield return Resources.UnloadUnusedAssets(); GC.Collect(); Debug.LogError(卸载成功); LoadScene(_sceneName, _loadCompleteEvent, _completeEveent); } /// summary /// 加载场景 /// /summary /// param name_sceneName场景名字/param /// param name_loadCompleteEvent加载完成后的事件/param /// param name_completeEveent/param public void LoadScene(string _sceneName,Action _loadCompleteEventnull,Action _completeEveentnull) { StartCoroutine(LoadSceneCoroutine(_sceneName, _loadCompleteEvent, _completeEveent)); } private IEnumerator LoadSceneCoroutine(string _sceneName, Action _loadCompleteEvent null, Action _completeEveent null) { Debug.LogError(开始加载.... _sceneName); if (assetBundle ! null) { assetBundle.Unload(true); assetBundle null; GC.Collect(); } string scene _sceneName; yield return StartCoroutine(LoadAssetBundleAsync(_sceneName, (loadedBundle) { assetBundle loadedBundle; })); if (assetBundle ! null) { scene assetBundle.GetAllScenePaths()[0]; } //加载场景 AsyncOperation op SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive); while (!op.isDone) { // 实际加载进度0-0.9 actualProgress op.progress; yield return null; } Scene newScene SceneManager.GetSceneByName(_sceneName); if (newScene.IsValid()) { //Debug.LogError(设置新的激活场景); SceneManager.SetActiveScene(newScene); } yield return Resources.UnloadUnusedAssets(); GC.Collect(); if (_loadCompleteEvent ! null) { _loadCompleteEvent(); } yield return new WaitForSecondsRealtime(1); if (_completeEveent ! null) { _completeEveent(); } yield break; }