打包代码:

[MenuItem("AssetBundle/PC")]
public static void AssetBundleByPc ()
{
string path = Application.dataPath + "/AssetBundle/PC";
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
BuildPipeline.BuildAssetBundles(path,BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
}

====================================================================================

BuildPipeline.BuildAssetBundles(路径,压缩格式,目标平台);

====================================================================================

依赖打包可以先加载被依赖资源,在加载需要的资源,被加载出来的资源会自动寻找被依赖的资源。

=====================================================================================

加载代码

//同步加载

//通过本地文件加载

void LoadAssetBundleFromFile(){
string path =Application.dataPath+ "/AssetBundle/PC/gameobject/cube.ab";//路径
string materialsPath = Application.dataPath + "/AssetBundle/PC/material/red.ab";//材质路径
AssetBundle materialsab = AssetBundle.LoadFromFile(materialsPath);//通过本地文件加载
AssetBundle ab = AssetBundle.LoadFromFile(path);

GameObject cube_go = ab.LoadAsset<GameObject>("Cube");//在ab里加载出“Cube”文件
Instantiate(cube_go);//实例化
}

//通过内存加载
void LoadAssetBundleFromMemory()
{
string path = Application.dataPath + "/AssetBundle/PC/gameobject/cube.ab";
string materialsPath = Application.dataPath + "/AssetBundle/PC/material/red.ab";
AssetBundle materab = AssetBundle.LoadFromMemory(File.ReadAllBytes(materialsPath));//通过文件路径读取所有的字节;Assetbundle.LoadFromMenory(byte[]);
AssetBundle ab= AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
GameObject cube_go = ab.LoadAsset<GameObject>("Cube");
Instantiate(cube_go);

}

//

posted on 2020-12-02 00:06  LuChuangwu  阅读(47)  评论(0编辑  收藏  举报