Unity C#打包AssetBundle与场景详解
作者:冰封百度
这篇文章主要给大家介绍了关于Unity C#打包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"); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。