博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity C# 打包AssetBundle与场景
阅读量:7257 次
发布时间:2019-06-29

本文共 5377 字,大约阅读时间需要 17 分钟。

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

我们只需要关心两个API:
1.BuildPipline.BuildAssetBundles() 打包AssetBundle
2.BuildPipline.BuildPlayer() 打包场景

1.打包AssetBundle

先在资源的Inspector面板最下方 填写资源所属的AssetBundle名称和后缀(后缀可以不填)
再利用BuildPipeline.BuildAssetBundles()进行打包
填写资源包名

2.打包Scene

(1)可以利用BuildPipline.BuildAssetBundles()进行打包。
注意:场景包内只能给.unity场景文件填写资源包名,不能给其他资源命名,否则打包会失败
一个场景ab包内可以包含多个场景文件,场景需要的资源会自动关联
有些资源是Editor下专用的。不会被打包进ab里 比如LightingData.asset
使用方式是www加载场景ab包后 先调用www.assetBundle 再调用SceneManager.LoadScene("name")

(2)利用BuildPipeline.BuildPlayer()进行打包

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

3.脚本批量打包

开始打包

4.打包完毕

打包完毕

5.加载测试

加载测试

6.打包和测试脚本

AssetBundleBuilder.cs

using UnityEngine;using UnityEditor;using System.IO;/// /// 资源包打包工具/// 
打包AssetBundle和场景(Unity 2018.2.20)
///
ZhangYu 2019-02-26
///
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("所有场景打包完毕"); } } /// 获取文件夹和子文件夹下所有指定类型文件 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.get(path, OnLoadABComplete); } // 加载场景 private void LoadScene() { // 资源包路径 string path = Application.streamingAssetsPath + "/Abs/Test.scene"; // WWW下载 Http http = gameObject.AddComponent
(); 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"); }}

转载地址:http://hfvdm.baihongyu.com/

你可能感兴趣的文章
关于ajax请求数据后,数据本身的js失效的一些想法
查看>>
06-Flutter移动电商实战-dio基础_Get_Post请求和动态组件协作
查看>>
基础认识
查看>>
【转】iOS设备的UDID是什么?苹果为什么拒绝获取iOS设备UDID的应用?如何替代UDID?...
查看>>
GetWindowRect和GetClientRect比较学习
查看>>
thinkphp 多表事务处理
查看>>
11-散列1 电话聊天狂人
查看>>
学SEO你其实只需要半个钟
查看>>
3G网卡PPP拨号
查看>>
AS3.0中的反射概念
查看>>
UIViewController中各方法调用顺序及功能详解
查看>>
C++ do{...}while(0)的好处
查看>>
【Todo】Python字符编码学习
查看>>
python实现朴素贝叶斯
查看>>
Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类
查看>>
Java操作MongoDB:连接&增&删&改&查
查看>>
Maven使用
查看>>
用python脚本把windows的文件上传到linux中
查看>>
m_Orchestrate learning system---三十四、使用重定义了$的插件的时候最容易出现的问题是什么...
查看>>
打开居中显示的窗口
查看>>