您好,登錄后才能下訂單哦!
小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。