一、創建Assetbundle 在unity3d開發的游戲中,無論模型,音頻,還是圖片等,我們都做成Prefab,然后打包成Assetbundle,方便我們后面的使用,來達到資源的更新。

一、創建Assetbundle

在unity3d開發的游戲中,無論模型,音頻,還是圖片等,我們都做成Prefab,然后打包成Assetbundle,方便我們后面的使用,來達到資源的更新。

? ? ? ?一個Assetbundle可以打包一個模型(這里的模型不單單指的是預制模型,可以是Project視圖下的任何東西),也可以是多個模型,但兩種打包方式占用的空間不一樣。

? ? ? 比如我打包三個一樣的模型(只不過他們的腳本不一樣創建三個空的GameObject(One,Two,Three),分別掛載腳本One,Two,Three)。如果我為每個模型單獨打包生成One,Two,Three三個Assetbundle,其所占的空間是A,B,C,但是A+B+C != D.由此可知想通的資源盡可能的打包到一起,他們共用一套資源。不相同的模型盡量分開打包。



二、分開打包(注意這個腳本必須放在Editor文件夾內,Editor文件夾沒有的話需自己創建)

[csharp]?view plaincopy
print?
  1. ///?<summary>??
  2. ///?將選中的預制分別打包??
  3. ///?</summary>??
  4. [MenuItem("AssetBundleDemo/Create?AssetBundles?By?themselves")]??
  5. static?void?CreateAssetBundleThemelves(){??
  6. ????//獲取要打包的對象(在Project視圖中)??
  7. ????Object[]?selects?=?Selection.GetFiltered?(typeof(Object),SelectionMode.DeepAssets);??
  8. ????//遍歷選中的對象??
  9. ????foreach(Object?obj?in?selects){??
  10. ????????//這里建立一個本地測試??
  11. ????????//注意本地測試中可以是任意的文件,但是到了移動平臺只能讀取路徑StreamingAssets里面的??
  12. ????????//StreamingAssets是只讀路徑,不能寫入??
  13. ????????string?targetPath?=?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/"?+?obj.name?+?".assetbundle";//文件的后綴名是assetbundle和unity都可以??
  14. ????????if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies)){??
  15. ??
  16. ????????????Debug.Log(obj.name?+?"is?packed?successfully!");??
  17. ????????}else{??
  18. ????????????Debug.Log(obj.name?+?"is?packed?failly!");??
  19. ????????}??
  20. ????}??
  21. ????//刷新編輯器(不寫的話要手動刷新,否則打包的資源不能及時在Project視圖內顯示)??
  22. ????AssetDatabase.Refresh?();??
  23. }??

SelectionMode.DeepAssets

這個選擇模式意味著如果選擇中包含多個文件,那么他將包含這個文件視圖中的所有資源。

他還有其他的各種選項(以下是官方文檔)

SelectionMode

Description

SelectionMode can be used to tweak the selection returned by Selection.GetTransforms.

The default transform selection mode is: SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable.

UnfilteredReturn the whole selection.
TopLevelOnly return the topmost selected transform. A selected child of another selected transform will be filtered out.
DeepReturn the selection and all child transforms of the selection.
ExcludePrefabExcludes any prefabs from the selection.
EditableExcludes any objects which shall not be modified.
AssetsOnly return objects that are assets in the Asset directory.
DeepAssetsIf the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.

最核心的方法:BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)

參數1:它只能放一個對象,因為我們這里是分別打包,所以通過循環將每個對象分別放在了這里。

參數2:可以放入一個數組對象。

參數3:要打包到的路徑

參數4:默認情況下打的包只能在電腦上用,如果要在手機上用就要添加一個參數。

Android上:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)

IOS上:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)

另外,電腦上和手機上打出來的Assetbundle不能混用,不同平臺只能用自己的。

三、一起打包

[csharp]?view plaincopy
print?
  1. ///?<summary>??
  2. ///?將選中的預制打包到一起??
  3. ///?</summary>??
  4. [MenuItem("AssetBundleDemo/Create?AssetBundles?Together")]??
  5. static?void?CreateAssetBundleTogether(){??
  6. ????//要打包的對象??
  7. ????Object[]?selects?=?Selection.GetFiltered?(typeof(Object),SelectionMode.DeepAssets);??
  8. ????//要打包到的路徑??
  9. ????string?targetPath?=?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/Together.assetbundle";??
  10. ????if(BuildPipeline.BuildAssetBundle(null,selects,targetPath,BuildAssetBundleOptions.CollectDependencies)){??
  11. ????????Debug.Log("Packed?successfully!");??
  12. ??
  13. ????}else{??
  14. ????????Debug.Log("Packed?failly!");??
  15. ????}??
  16. ????//刷新編輯器(不寫的話要手動刷新)??
  17. ????AssetDatabase.Refresh?();??
  18. }??


四、讀取

[csharp]?view plaincopy
print?
  1. using?UnityEngine;??
  2. using?System.Collections;??
  3. ??
  4. public?class?ReanAssetbundle?:?MonoBehaviour?{??
  5. ??
  6. ????//不同平臺下StreamingAssets的路徑是不同的,這里需要注意一下。??
  7. ????public?static?readonly?string?m_PathURL?=??
  8. ????????#if?UNITY_ANDROID??
  9. ????????"jar:file://"?+?Application.dataPath?+?"!/assets/";??
  10. ????????#elif?UNITY_IPHONE??
  11. ????????Application.dataPath?+?"/Raw/";??
  12. ????????#elif?UNITY_STANDALONE_WIN?||?UNITY_EDITOR??
  13. ????????"file://"?+?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/";??
  14. ????????#else??
  15. ????????string.Empty;??
  16. ????????#endif??
  17. ??
  18. ????void?OnGUI(){??
  19. ????????if(GUILayout.Button("加載分開打包的Assetbundle")){??
  20. ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"One.assetbundle"));??
  21. ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+??"Two.assetbundle"));??
  22. ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"Three.assetbundle"));??
  23. ??
  24. ????????}??
  25. ??????????
  26. ????????if(GUILayout.Button("加載打包在一起的Assetbundle")){??
  27. ????????????StartCoroutine(LoadGameObjectPackedTogether(m_PathURL?+?"Together.assetbundle"));??
  28. ????????}??
  29. ??????????
  30. ????}??
  31. ????//單獨讀取資源??
  32. ????private?IEnumerator?LoadGameObjectPackedByThemselves(string?path){??
  33. ????????WWW?bundle?=?new?WWW?(path);??
  34. ????????yield?return?bundle;??
  35. ??
  36. ????????//加載??
  37. ????????yield?return?Instantiate?(bundle.assetBundle.mainAsset);??
  38. ????????bundle.assetBundle.Unload?(false);??
  39. ????}??
  40. ??
  41. ????IEnumerator??LoadGameObjectPackedTogether?(string?path)??
  42. ????{??
  43. ????????WWW?bundle?=?new?WWW?(path);??
  44. ????????yield?return?bundle;??
  45. ??
  46. ????????Object?one?=?bundle.assetBundle.Load?("One");??
  47. ????????Object?two?=?bundle.assetBundle.Load?("Two");??
  48. ????????Object?three?=?bundle.assetBundle.Load?("Three");??
  49. ??
  50. ????????//加載??
  51. ????????yield?return?Instantiate?(one);??
  52. ????????yield?return?Instantiate?(two);??
  53. ????????yield?return?Instantiate?(three);??
  54. ????????bundle.assetBundle.Unload?(false);??
  55. ????}??
  56. }??

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387480.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387480.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387480.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【JS】我的JavaScript學習之路(2)

3.從JavaScript頁面解析過程看執行順序 代碼(test.html)&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/x…

Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

<題目鏈接> 題目大意&#xff1a; 有一個長度為n(n<1000)的01串&#xff0c;該串中至少有一個0和一個1&#xff0c;現在由你構造出一些01串&#xff0c;進行詢問&#xff0c;然后系統會給出你構造的串與原串的 Hamming distance &#xff0c;現在要求你按照步驟進行…

王者榮耀提取攻略

1. 王者榮耀安裝后&#xff0c;就將模型等資源解壓到SD卡目錄里&#xff0c;我們需要找到這個目錄。模型資源存儲在SD卡中&#xff0c;路徑為&#xff1a;【/SDCard/Android/data/com.tencent.tmgp.sgame/files/Resources/AssetBundle/】 2. 2 所有英雄的資源包都在這個目…

2.4 multiset

#include<set> multiset與set的唯一不同&#xff1a;允許插入重復的元素。 在插入元素、刪除元素、查找元素上與set 有區別。 multiset元素的插入&#xff1a; multiset<int> ms; ms.insert(11); ms.insert(11); //插入兩個11&#xff0c;遍歷時同樣有兩個11。…

Exchange ActiveSyn身份驗證類型

http://www.exchangecn.com/html/exchange2010/20110125_316.html 配置 Exchange ActiveSync 身份驗證 時間:2011-01-25 11:01來源:Exchange中文站 作者:Exchange中文站 點擊:3045次ActiveSync 身份驗證是客戶端和服務器驗證其身份以進行數據傳輸的過程&#xff0c;本文以示例的…

HotSpot 虛擬機垃圾回收算法實現

作為使用范圍最廣的虛擬機之一HotSpot&#xff0c;必須對垃圾回收算法的執行效率有嚴格的考量&#xff0c;只有這樣才能保證虛擬機高效運行 枚舉根節點 從可達性分析中從 GC Roots 節點找引用鏈這個操作為例&#xff0c;可以作為 GC Roots 的節點主要在全局性的引用&#xff08…

Java NIO編寫Socket服務器的一個例子

最近一直在忙著JAVA NIO的知識&#xff0c;花了一下午的時間&#xff0c;總算寫出了一個可以運行的程序&#xff0c;廢話少說&#xff0c;上代碼&#xff01; Java代碼&#xff1a; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerS…

2.5 map

#include<map> key/value對 采用紅黑樹實現&#xff0c;鍵值不允許重復 用法與set類似 創建map&#xff1a; map<string, float> m; m["haha"] 11.1; m["hehe"] 22.2; for (map<string, float>::iterator it m.begin(); it ! m.en…

在js傳遞參數中含加號(+)的處理方式

一般情況下&#xff0c;url中的參數應使用 url 編碼規則&#xff0c;即把參數字符串中除了 “ - "、" _ " 、" . "之外的所有非字母數字字符都將被替換成百分號&#xff08;%&#xff09;后跟兩位十六進制數&#xff0c;空格則編碼為加號&#xff08;…

二 SVN代碼沖突的解決

問題&#xff1a; A和B都是最新的代碼&#xff0c;A修改了代碼提交了&#xff0c;B也修改了代碼&#xff0c;但是B提交的時候出現沖突的問題。 解決方案&#xff1a;編輯沖突 解決沖突&#xff1a; 方法一&#xff1a;將文件里面沖突的描述去掉&#xff0c;重新提交 方法二&…

Android7.0反射類找不到的問題

Java中使用反射的地方較多&#xff0c;尤其是各種框架中。最近在Android7.0的項目中遇到個問題很奇怪&#xff0c;反射使用的類找不到了&#xff0c;但是編譯的時候沒問題啊。然后在代碼中使用非反射的方式調用代碼也是沒有問題的&#xff0c;這時奇怪的現象出現了&#xff0c;…

2.6 multimap

#include<map> multimap的元素插入、刪除、查找與map不同 multimap元素的插入&#xff1a;&#xff08;未提供mm[key]value插入方式&#xff09; multimap<string, double> mm; mm.insert(pair<string, double>("haha", 11.1)); mm.insert(pai…

Mybatis學習筆記18 - 緩存

兩級緩存&#xff1a; 一級緩存&#xff1a;&#xff08;本地緩存&#xff09;&#xff1a;sqlSession級別的緩存。一級緩存是一直開啟的&#xff1b;SqlSession級別的一個Map 數據庫同一次會話期間查詢到的數據會放在本地緩存中。以后如果需要獲取相同的數據&#xff0c;直接從…

2.7 deque

#include<deque> 雙端隊列容器 注意&#xff1a;頭入隊時伴隨的是尾出隊&#xff1b;提供中間元素的更新和刪除操作。 與vector一樣&#xff0c;采用線性表順序存儲結構 deque采用分塊的線性存儲結構來存儲數據&#xff0c;每塊大小一般為512字節 所有deque塊由一個…

APK 加殼方法

下載工具http://download.csdn.net/download/sys025/8958363一款免費的為apk加固的工具。 特別說明&#xff1a;加固后需要重新簽名apk才能安裝。加固的apk包會比未加固的大一些。 jarsigner -verbose -keystore dms.keystore -storepass pactera -keypass pactera -sigfile CE…

Java DSL簡介(收集整理)

一、領域特定語言&#xff08;DSL&#xff09; 領域特定語言&#xff08;DSL&#xff09;通常被定義為一種特別針對某類特殊問題的計算機語言&#xff0c;它不打算解決其領域外的問題。對于DSL的正式研究已經持續很多年&#xff0c;直 到最近&#xff0c;在程序員試圖采用最易讀…

[轉]JSon數據解析的四種方式

轉至http://blog.csdn.net/enuola/article/details/7903632 作為一種輕量級的數據交換格式&#xff0c;json正在逐步取代xml&#xff0c;成為網絡數據的通用格式。 有的json代碼格式比較混亂&#xff0c;可以使用此“http://www.bejson.com/”網站來進行JSON格式化校驗&#xf…

2.8 list

#include<list> 雙向循環鏈表 list結點的三個域&#xff1a;數據域、前驅元素指針域、后繼元素指針域 對于list的迭代器&#xff0c;只有或--的操作&#xff0c;無n或-n的操作 創建list對象&#xff1a; list<int> l; list<int> l(10); 插入和遍歷&…

Spring AOP兩種實現機制是什么?

Spring AOP兩種實現機制是什么&#xff1f; 1.如果是有接口聲明的類進行AOP 時&#xff0c;spring調用的是java.lang.reflection.Proxy 類來做處理 2.如果是沒有接口聲明的類時&#xff0c; spring通過cglib包和內部類來實現 在AOP&#xff0c;權限控制&#xff0c;事務管理等…

iOS開發UI篇—Quartz2D使用(繪圖路徑)

1 //1.獲取圖形上下文 2 CGContextRef ctxUIGraphicsGetCurrentContext(); 3 //2.繪圖&#xff08;畫線&#xff09; 4 //設置起點 5 CGContextMoveToPoint(ctx, 20, 20); 6 //設置終點 7 CGContextAddLineToPoint(ctx, 200, 300); 8 //渲染 9…