AssetBundle-关于Mainfest文件使用

AssetBundle-关于Mainfest文件使用_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
/*
 * Author:W
 * mainfenst文件
 */
public class AssetBundleManager : MonoBehaviour
{
    private AssetBundle shareAb = null;
    private AssetBundle ab = null;

    // Start is called before the first frame update
    void Start()
    {
                
        //加载mainfest的AB包。并获取到其中mainfest文件,从而知晓项目中所有AB包以及他们之间的依赖关系
        AssetBundle assetBundle = LoadAssetBundleFromFile("AssetBundles/AssetBundles");
        AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //打印所有AB名
        foreach (string abName in assetBundleManifest.GetAllAssetBundles())
        {
            Debug.Log("AB包名:"+abName);
        }

        //获取cubewall包依赖的所有AB包,并进行加载
        string[] depenABs = assetBundleManifest.GetAllDependencies("walls/cubewall.unityobj");
        foreach (string name in depenABs)
        {
            Debug.Log("walls/cubewall.unityobj包所依赖的包:"+name);
            LoadAssetBundleFromFile("AssetBundles/"+name);
        }

        //再加载cubewall包本身
        AssetBundle wallAB = LoadAssetBundleFromFile("AssetBundles/walls/cubewall.unityobj");

        //实例化
        GameObject wallPrefab = wallAB.LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab);

    }

    /// 
    /// AB加载方式2:从本地文件夹中加载【同步】
    /// 
    /// 
    private AssetBundle LoadAssetBundleFromFile(string path)
    {
        AssetBundle ab = null;

        ab = AssetBundle.LoadFromFile(path);

        return ab;
    }
    
}

你可能感兴趣的:(Unity)