内容简介:Unity2018已经把打包过程简化很多了我们只需要关心两个API:1.BuildPipline.BuildAssetBundles() 打包AssetBundle
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"); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 【前端打包部署】谈一谈我在SPA项目打包=>部署的处理
- Maven多模块项目打包前的一些注意事项(打包失败)
- tar打包如何不打包某一个文件夹(排除某些文件夹)
- iOS新手用swift写一个macos打包工具 一键打包到指定位置
- HBase场景 | 对比MySQL,一文看透HBase的能力及使用场景
- Android应用签名打包
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML 编码/解码
HTML 编码/解码
RGB HSV 转换
RGB HSV 互转工具