您好,登录后才能下订单哦!
小编给大家分享一下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"); } }
以上是“Unity C#如何打包AssetBundle”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。