AB package dependencies

1. What is dependency?

Sometimes the things required by a model may be in different packages. For example, the model and material of the blue cube are in different packages (mode and head). In this case, two packages need to be loaded to allow the ball to display normally.

2. How to obtain dependencies and load them

//加载AB包
AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "model");

        //AssetBundle ab2 = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "head");
        //获取依赖关系
        //加载主包
        AssetBundle abMain = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "PC");
        //加载主包中的固定文件(固定写法)
        AssetBundleManifest abMainfest = abMain.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //得到依赖信息(例如这个model)
        string[] strs = abMainfest.GetAllDependencies("model");
        //用得到的字符串去加载相应的包
        for (int i = 0; i < strs.Length; i++)
        {
            print(strs[i]);

            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + strs[i]);
        }

        //ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "head");
        //加载AB包资源
        //用这个重载,因为Lua不支持泛型
        GameObject obj = ab.LoadAsset("Cube",typeof(GameObject)) as GameObject;
        Instantiate(obj);

        GameObject obj2 = ab.LoadAsset("Sphere", typeof(GameObject)) as GameObject;
        Instantiate(obj2,new Vector3(1,1,1),Quaternion.identity);

operation result 

Guess you like

Origin blog.csdn.net/holens01/article/details/133201016