创建AB包
- 在PackageManager中下载AssetBundleBrowser
- 选择使用资源,在Inspector下方找到AssetBundle
- 下拉框中选择new 创建包名
AssetBundleBrowser
打开Windows->AssetBundleBrowser
- configure中查看准备打包的资源
- Inspect 查看已经打包后的资源
- build 创建ab包
- Build Target 打包平台
- Output Path 输出路径
- Clear Folders 清除已存在的文件夹
- 将打包后的文件复制一份到StreamingAssets中
使用AB包
void Start()
{
//1.加载ab包 Application.streamingAssetsPath在 pc 可读可写 aniorid ios只读
//注意同一个ab包不能重新加载 model 包名
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "model");
//2.同步加载ab包中的资源
//使用名字加载但不指定类型 出现同名文件时不能准确查找到想要使用的文件
GameObject cube = ab.LoadAsset<GameObject>("Cube");
GameObject cube2 = ab.LoadAsset("Cube", typeof(GameObject)) as GameObject;
Instantiate(cube);
Instantiate(cube2);
GameObject sphere = ab.LoadAsset("Sphere", typeof(GameObject)) as GameObject;
Instantiate(sphere);
//3.卸载所有ab包 参数为true时 卸载场景中已经加载的ab包资源
//false 只卸载ab包
//AssetBundle.UnloadAllAssetBundles(false);
//4.卸载单个ab包 参数代表的意思和上述类似
ab.Unload(false);
//异步加载b包中的资源
StartCoroutine(LoadAB("model", "Cube"));
}
IEnumerator LoadAB(string abName, string resName)
{
//加载ab包
AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/" + abName);
yield return abcr;//ab包加载完成
//加载包中资源
AssetBundleRequest abr = abcr.assetBundle.LoadAssetAsync(resName, typeof(GameObject));
yield return abr;//ab包中使用的资源加载完成
Instantiate(abr.asset as GameObject);
}