参考链接:
https://www.jianshu.com/p/7e7b34b24296
1.manifest
打包后,会生成一个与AssetBundle对应的.manifest文件,以及与输出目录对应的.manifest文件
2.测试
a.新建一个Cube和Sphere,依赖同一个材质,然后打包(不打材质)。如下,因为不打材质,导致Cube和Sphere的AB包内都有一份相同的材质,.manifest文件也没啥变化
b.如下,打了材质后,Cube和Sphere的AB包会依赖材质的AB包,自身的包体积也会减少,.manifest文件会显示出依赖关系
3.加载
加载原则为,先加载AB包的依赖包,再加载AB包。AB包的依赖包可以通过.manifest文件获取
如果不加载依赖包会出现这种情况:
代码如下:
1 using UnityEngine; 2 3 public class TestAB : MonoBehaviour 4 { 5 void Start() 6 { 7 AssetBundle ab = AssetBundle.LoadFromFile("Assets/AB/AB"); 8 AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); 9 10 string[] dependencies = manifest.GetAllDependencies("cube"); 11 for (int i = 0; i < dependencies.Length; i++) 12 { 13 AssetBundle.LoadFromFile("Assets/AB/" + dependencies[i]); 14 } 15 16 AssetBundle cubeAB = AssetBundle.LoadFromFile("Assets/AB/cube"); 17 if (cubeAB != null) 18 { 19 GameObject go = Instantiate(cubeAB.LoadAsset<GameObject>("cube")); 20 go.transform.localScale = Vector3.one * 2; 21 } 22 } 23 }
效果: