跟我從零基礎學習Unity3D開發--資源打包篇(AssetBundle)


好久沒更新了,一直在加班敢項目進度。這里和關注我的博客的童鞋表示一下歉意!這里有我錄的Unity3D從零開始的視頻教程大家可以關注一下:http://www.imooc.com/view/555  視頻會陸續的更新,需要講什么也可以留言給我。

之前在項目中一直用的是別人以前的寫的打包代碼,后來項目需要需要修改以前的代碼把打包的代碼過了一遍,今天把自己遇到的問題和打包思路在這路寫下來。

在untiy支援加載方面分為兩個種類:

第一類:Resources方式加載-------我們不用打包資源,放在項目中的Resources目錄下就能在代碼中直接加載出來。

第二類:assetBundle方式加載-----需要我們打包資源,把每個資源打包成一個個Bundle,然后加載。

兩類資源加載方式在游戲項目中都會用,Resources加載的資源一般是要求非常及時的,assetBundle加載相對要慢一些因為需要把assetBundle資源解壓出來。assetBundle也是方便項目中做熱更新(在游戲對外發布的過程中,我們需要替換一些資源,通過熱更新的方式把資源更新出去供當前的客戶端下載替換)。Assetbundle是官方推薦使用的打包方式,AssetbundleUnity(Pro)提供的資源打包策略。

打包思路:

*高能

  • a、打包的資源一定要放在StreamingAssets目錄下因為在移動平台下只能訪問這個目錄
  • b、AssetBundle的保存后綴名可以是assetbundle或者unity3d
  • c、BuildAssetBundle要根據不同的平台單獨打包,BuildTarget參數指定平台,如果不指定,默認的webplayer

 

1、准備好我們需要打包的資源

2、加載需要打包的資源

3、獲取當前對象列表的所有依賴項

4、設置打包編譯選項

5、開始打包

 

重要函數解釋:

Selection.objects   ---------------------------------------------------  這個方法是用於判斷在Asset目錄中返回選中了哪些資源文件
AssetDatabase.GetAssetpath()-------------------------------------- 獲取的是工程文件相對於Asset的路徑
AssetDatabase.LoadMainAssetAtPath()----------------------------   加載指定路徑下的資源
EditorUtility.CollectDependencies()--------------------------------- 獲取當前對象列表的所有依賴項
BuildAssetBundleOptions ------------------------------------------- 打包編譯選項
BuildPipeline.BuildAssetBundle() ---------------------------------- 打包函數
Path.GetFileNameWithoutExtension() ----------------------------- 獲取路徑下的不要擴展名的文件名
Path.ChangeExtension() --------------------------------------------   改變路徑的文件后綴名
EditorUserBuildSettings.activeBuildTarget -----------------------   判斷當前編譯平台
 
 
下面列出打包實例代碼!
實例:
 
 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEditor;
 4 using System.IO;
 5 using System.Collections.Generic;
 6 public class AssetBoundExport :EditorWindow {
 7 
 8     [MenuItem("Window/資源打包")]
 9     static void Init()
10     {
11         EditorWindow.GetWindow(typeof(AssetBoundExport));
12     }
13 
14     void OnGUI()
15     {
16         /*
17         *ui1.prefab  和ui2.prefab 是兩個預制件,里面都包含loading.png這張圖
18         *這個方法是講依賴打包的
19         *通一個資源用在多個預制件中的時候我們就采用這種依賴打包更小
20         */
21         if (GUILayout.Button("打包UI對象", GUILayout.Width(100)))
22         {
23 
24             BuildPipeline.PushAssetDependencies();
25 
26             Object baseTexture = AssetDatabase.LoadMainAssetAtPath("Assets/Texture/loading.png");
27             ExportBundle<Texture2D>(AssetDatabase.GetAssetPath(baseTexture));
28 
29             BuildPipeline.PushAssetDependencies();
30             Object p1 = AssetDatabase.LoadMainAssetAtPath("Assets/Prefab/ui1.prefab");
31             ExportBundle<GameObject>(AssetDatabase.GetAssetPath(p1));
32             BuildPipeline.PopAssetDependencies();
33 
34             BuildPipeline.PushAssetDependencies();
35             Object p2 = AssetDatabase.LoadMainAssetAtPath("Assets/Prefab/ui2.prefab");
36             ExportBundle<GameObject>(AssetDatabase.GetAssetPath(p2));
37             BuildPipeline.PopAssetDependencies();
38 
39             BuildPipeline.PopAssetDependencies();
40             AssetDatabase.Refresh();
41         }
42     
43     }
44     /// <summary>
45     /// 這個方法是用於單獨給某類資源打包(這個方法實例是打圖片的)
46     /// 應用場景:在實際項目中我們每次不一定是要全部打包,更多的時候是只需要打某一個資源
47     /// </summary>
48     [MenuItem("Assets/打包/Textrue")]
49     public static void PackageTextureAssetBundle()
50     {
51         Object[] _Object = Selection.objects;
52         for (int i = 0; i < _Object.Length; i++)
53         {
54             ExportBundle<Texture2D>(AssetDatabase.GetAssetPath(_Object[i]));
55         }   
56     }
57     
58     /// <summary>
59     /// 打包通用類
60     /// </summary>
61     /// <typeparam name="T"></typeparam>
62     /// <param name="path"></param>
63     private static void ExportBundle<T>(string path) where T:Object
64     {
65         T assetObject = AssetDatabase.LoadMainAssetAtPath(path) as T;
66         if (assetObject == null)
67         {
68             Debug.Log("can't load "+path);
69             return;
70         }
71 
72         string _path = Application.dataPath + "/StreamingAssets/PC/" + Path.GetFileNameWithoutExtension(path);
73         _path = Path.ChangeExtension(_path, ".unity3d");
74 
75         List<Object> obejectList = new List<Object>();
76         Object[] tempobject = new Object[1];
77         tempobject[0] = assetObject;
78         Object[] kDepens = EditorUtility.CollectDependencies(tempobject);
79         for (int i = 0; i < kDepens.Length; i++)
80         {
81             obejectList.Add(kDepens[i]);   
82         }
83   
84         BuildAssetBundleOptions babo = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
85 | BuildAssetBundleOptions.DeterministicAssetBundle;
86       bool istru=   BuildPipeline.BuildAssetBundle(assetObject, obejectList.ToArray(),_path, babo, EditorUserBuildSettings.activeBuildTarget);
87       if (istru == true)
88           Debug.Log("打包成功");
89       else
90           Debug.Log("打包失敗");
91     }
92 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM