欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

Unity C#打包AssetBundle与场景详解

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

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

我们只需要关心两个API:

1.BuildPipline.BuildAssetBundles() 打包AssetBundle

2.BuildPipline.BuildPlayer() 打包场景

1.打包AssetBundle

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

再利用BuildPipeline.BuildAssetBundles()进行打包

2.打包Scene

利用BuildPipeline.BuildPlayer()进行打包

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

3.脚本批量打包

4.打包完毕

5.加载测试

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");
 }

}

总结

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

上一篇:Unity3D Shader实现贴图切换效果

栏    目:C#教程

下一篇:c#滚动字幕动画窗体制作步骤

本文标题:Unity C#打包AssetBundle与场景详解

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4851.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有