欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity C#打包AssetBundle与场景详解

程序员文章站 2023-08-17 19:53:02
unity2018已经把打包过程简化很多了 我们只需要关心两个api: 1.buildpipline.buildassetbundles() 打包assetbu...

unity2018已经把打包过程简化很多了

我们只需要关心两个api:

1.buildpipline.buildassetbundles() 打包assetbundle

2.buildpipline.buildplayer() 打包场景

1.打包assetbundle

先在资源的inspector面板最下方 填写资源所属的assetbundle名称和后缀(后缀可以不填)

再利用buildpipeline.buildassetbundles()进行打包

Unity C#打包AssetBundle与场景详解

2.打包scene

利用buildpipeline.buildplayer()进行打包

为方便使用 先把要打包的场景放入指定的文件夹 通过脚本批量打包

Unity C#打包AssetBundle与场景详解

3.脚本批量打包

Unity C#打包AssetBundle与场景详解

4.打包完毕

Unity C#打包AssetBundle与场景详解

5.加载测试

Unity C#打包AssetBundle与场景详解

6.打包和测试脚本

assetbundlebuilder.cs

using unityengine;
using unityeditor;
using system.io;

/// <summary>
/// 资源包打包工具
/// <para>打包assetbundle和场景(unity 2018.2.20)</para>
/// <para>zhangyu 2019-02-26</para>
/// </summary>
public class assetbundlebuilder
{
 [menuitem("打包/windows/资源包和场景")]
 public static void buildabsandsceneswindows() {
  buildabsandscenes(buildtarget.standalonewindows);
 }

 [menuitem("打包/android/资源包和场景")]
 public static void buildabsandscenesandroid() {
  buildabsandscenes(buildtarget.android);
 }

 [menuitem("打包/ios/资源包和场景")]
 public static void buildabsandscenesios() {
  buildabsandscenes(buildtarget.ios);
 }

 [menuitem("打包/windows/资源包")]
 public static void buildabswindows() {
  buildassetbundles(buildtarget.standalonewindows);
 }

 [menuitem("打包/android/资源包")]
 public static void buildabsandroid() {
  buildassetbundles(buildtarget.android);
 }

 [menuitem("打包/ios/资源包")]
 public static void buildabsios() {
  buildassetbundles(buildtarget.ios);
 }

 [menuitem("打包/windows/场景")]
 public static void buildsceneswindows() {
  buildscenes(buildtarget.standalonewindows);
 }

 [menuitem("打包/android/场景")]
 public static void buildscenesandroid() {
  buildscenes(buildtarget.android);
 }

 [menuitem("打包/ios/场景")]
 public static void buildscenesios() {
  buildscenes(buildtarget.ios);
 }

 // 打包assetbundle和scenes
 public static void buildabsandscenes(buildtarget platform) {
  buildassetbundles(platform);
  buildscenes(platform);
 }

 // 打包assetbundles
 private static void buildassetbundles(buildtarget platform) {
  // 输出路径
  string outpath = application.streamingassetspath + "/abs";
  if (!directory.exists(outpath)) directory.createdirectory(outpath);
  editorutility.displayprogressbar("信息", "打包资源包", 0f);
  buildpipeline.buildassetbundles(outpath, buildassetbundleoptions.deterministicassetbundle, platform);
  assetdatabase.refresh();
  debug.log("所有资源包打包完毕");
 }

 // 打包scenes
 private static void buildscenes(buildtarget platform) {
  // 指定场景文件夹和输出路径
  string scenepath = application.datapath + "/abresources/scenes";
  string outpath = application.streamingassetspath + "/abs/";

  if (directory.exists(scenepath)) {
   // 创建输出文件夹
   if (!directory.exists(outpath)) directory.createdirectory(outpath);

   // 查找指定目录下的场景文件
   string[] scenes = getallfiles(scenepath, "*.unity");
   for (int i = 0; i < scenes.length; i++) {
    string url = scenes[i].replace("\\", "/");
    int index = url.lastindexof("/");
    string scene = url.substring(index + 1, url.length - index - 1);
    string msg = string.format("打包场景{0}", scene);
    editorutility.displayprogressbar("信息", msg, 0f);
    scene = scene.replace(".unity", ".scene");
    debug.log(string.format("打包场景{0}到{1}", url, outpath + scene));
    buildpipeline.buildplayer(scenes, outpath + scene, buildtarget.standalonewindows, buildoptions.buildadditionalstreamedscenes);
    assetdatabase.refresh();
   }
   editorutility.clearprogressbar();
   debug.log("所有场景打包完毕");
  }
 }

 /// <summary> 获取文件夹和子文件夹下所有指定类型文件 </summary>
 private static string[] getallfiles(string directory, params string[] types) {
  if (!directory.exists(directory)) return new string[0];
  string searchtypes = (types == null || types.length == 0) ? "*.*" : string.join("|", types);
  string[] names = directory.getfiles(directory, searchtypes, searchoption.alldirectories);
  return names;
 }

}

loadtest.cs

using unityengine;
using unityengine.scenemanagement;

public class loadtest : monobehaviour {

 private void start () {
  loadab();
  loadscene();
 }

 // 加载资源包
 private void loadab() {
  // 资源包路径
  string path = application.streamingassetspath + "/abs/test.ab";

  // www下载
  http http = gameobject.addcomponent<http>();
  http.get(path, onloadabcomplete);
 }

 // 加载场景
 private void loadscene() {
  // 资源包路径
  string path = application.streamingassetspath + "/abs/test.scene";

  // www下载
  http http = gameobject.addcomponent<http>();
  http.get(path, onloadscenecomplete);
 }

 // 加载assetbundle完毕
 private void onloadabcomplete(www www) {
  // 实例化预制
  assetbundle ab = www.assetbundle;
  object prefab = ab.loadasset("test");
  gameobject instance = (gameobject)instantiate(prefab);
  dontdestroyonload(instance);
 }

 // 加载场景完毕
 private void onloadscenecomplete(www www) {
  // 必须写www.assetbundle这句 这样场景才能被读取到
  assetbundle ab = www.assetbundle;
  scenemanager.loadscene("test");
 }

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。