Unity3d之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/387522.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387522.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387522.shtml

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

相關文章

Android代碼抄襲Java曝猛料 新證據出現

Oracle最初告Android代碼里侵犯了他們旗下Java知識產權的時候,大多數不明真相的圍觀群眾都是站在Google這一邊的,畢竟Oracle蠻橫不講理慣了嘛. 但是,這次我們還真是當了不明真相的圍觀群眾了,美國專利博 ... Oracle最初告Android代碼里侵犯了他們旗下Java知識產權的時候,大多數…

JS之數據類型v(** v**)v個人筆記

<body> <!-- 單詞記憶 argument&#xff1a;實參 assignment&#xff1a;賦值 instance&#xff1a;實例 1.JS中的數據類型分為以下類型 *值類型&#xff08;基本類型&#xff09;*String&#xff1a;可以為任何字符串*Number&#xff1a;可以為任何數字*boolean&…

unity3d 各個目錄的意思

1.首先&#xff0c;你得理解Unity中各個目錄的意思&#xff1f; 我這里說的是移動平臺&#xff08;安卓舉例&#xff09;&#xff0c;讀&#xff0c;寫。所謂讀&#xff0c;就是你出大版本的包之后&#xff0c;這個只讀的話&#xff0c;就一輩子就這些東西了&#xff0c;不會改…

WordPress Option API(數據庫儲存 API)

WordPress Option API 是提供給開發者的數據庫存儲機制&#xff0c;通過調用函數&#xff0c;可以快速、安全的把數據存儲到數據庫里&#xff08;都在 wp_options 表&#xff09;。 每個設置的模式是 key – value&#xff0c;利于擴展。Option API 不僅僅給主題和插件開發者用…

asp.net core根據用戶權限控制頁面元素的顯示

asp.net core根據用戶權限控制頁面元素的顯示 Intro 在 web 應用中我們經常需要根據用戶的不同允許用戶訪問不同的資源&#xff0c;顯示不同的內容&#xff0c;之前做了一個 AccessControlHelper 的項目&#xff0c;就是解決這個問題的。 asp.net core 支持 TagHelper 和 基于 …

Please let us know in case of any issues

Please let us know in case of any issues轉載于:https://www.cnblogs.com/zhangchenliang/archive/2010/05/18/1738117.html

Java面向對象(二)

source:http://blog.java1234.com/index.html?typeId1 Java類的繼承 1&#xff0c;繼承定義以及基本使用 定義&#xff1a;子類能夠繼承父類的屬性和方法&#xff1b; 注意點&#xff1a;Java中只支持單繼承&#xff1b; 私有方法不能繼承&#xff1b; 2&#xff0c;方法重寫 …

游戲通訊方式

農藥自從上線以來&#xff0c;依靠著強大的產品力以及騰訊的運營能力&#xff0c;在游戲市場上表現可謂是風生水起&#xff0c;根據第三方的調研數據顯示&#xff0c;《王者榮耀》滲透率達到22.3%&#xff0c;用戶規模達到2.01億人&#xff0c;每日的日活躍用戶&#xff08;DAU…

小小c#算法題 - 3 - 字符串語句反轉

題目&#xff1a;反轉語句。 如I love Beijing! 反轉后輸出 !Beijing love I 特點是指反轉單詞的順序&#xff0c;其他字符&#xff08;這個可以自己指定&#xff09;不反轉。且不能用內置函數&#xff0c;如Split和Substring。 分析&#xff1a;我們需要保證一個單詞的字…

unity5.4.3p2里面的AssetBundle打包流程

unity5.4.3p2里面的AssetBundle打包流程&#xff0c;相比之前unity4.x的打包簡單了許多&#xff0c;Unity4.X中打包的時候需要自己去管理依賴關系&#xff0c;各種BuildPipeline.PushAssetDependencies()和BuildPipeline.PopAssetDependencies()&#xff0c;一不小心手一抖&…

靜態查找表的實現

#ifndef SSTABLE_H #define SSTABLE_H#include <iostream> using namespace std;/************************************************************* SSTable&#xff1a;stastic search table 靜態查找表的模板類實現 順序存儲結構 ************************************…

(轉)javascript匿名函數

文章來源: http://hi.baidu.com/koen_li/blog/item/4b14e4fc0c9b140c08244d8c.html 匿名函數的寫法 顧名思義&#xff0c;就是沒有名字的函數&#xff08;⊙﹏⊙b汗&#xff09;。匿名函數通常用于javascript作用域的控制&#xff0c;可以有效的避免對全局變量的污染。常見的匿…

BZOJ3307 雨天的尾巴

題目鏈接&#xff1a;戳我 樹上鏈修改->差分 每一個節點都開一個權值線段樹&#xff0c;最后從下往上合并qwq 代碼如下&#xff1a; #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define MA…

主成分分析(PCA)原理詳解 2016/12/17 · IT技術 · 主成分分析, 數學 分享到: 21 原文出處: 中科春哥 一、PCA簡介 1. 相關背景 主成分分析(Principa

主成分分析&#xff08;PCA&#xff09;原理詳解 2016/12/17 IT技術 主成分分析, 數學 分享到&#xff1a;21原文出處&#xff1a; 中科春哥 一、PCA簡介 1. 相關背景 主成分分析&#xff08;Principal Component Analysis&#xff0c;PCA&#xff09;&#xff0c; 是一種統…

1 Hadoop簡介

1.1 什么是Hadoop 分布式計算平臺 優點&#xff1a; 高可靠性 高擴展性 高效性 在各節點之間動態地移動數據&#xff0c;保證各個節點的動態平衡 高容錯性 數據多副本&#xff1b;重新啟動失敗任務 Hadoop應用&#xff1a; Yahoo 廣告系統Web搜索研究 Facebook 數據分…

Google Xpath Helper

Google Xpath Helper 下載方法&#xff1a; 1. 訪問http://chrome-extension-downloader.com/ 2. 把https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl拷貝到文本框里面&#xff0c;然后點擊“Download Extention”按鈕。 使用方法&am…

【Tensorflow】 Object_detection之訓練PASCAL VOC數據集

參考&#xff1a;Running Locally 1、檢查數據、config文件是否配置好 可參考之前博客&#xff1a; Tensorflow Object_detection之配置Training Pipeline Tensorflow Object_detection之準備數據生成TFRecord 2、訓練模型 PIPELINE_CONFIG_PATH/data/zxx/models/research/date…

2 Hadoop的安裝與配置

需要JDK、SSH 對于偽分布式&#xff0c;Hadoop會采取與集群相同的處理方式&#xff1a;按次序啟動文件conf/slaves中記載的主機上的進程&#xff0c;只不過在偽分布式中Slave為localhost&#xff08;自身&#xff09;。 Hadoop從三個角度將主機劃分為兩種角色&#xff1a; 最…

局域網訪問控制

訪問局域網內其他機器可用如下方式&#xff1a; \\PC-name\d$\dir 或者 \\192.168.xxx.xxx\d$\dir d代表d盤 但前提是對方機器已經把本機用戶設置為管理員賬戶轉載于:https://www.cnblogs.com/jimmy-c/p/4116804.html

Unity3d 插值同步

文中大體的思路&#xff1a; A玩家 移動時&#xff0c;本機自行移動&#xff0c;并發送移動指令給服務端&#xff0c;假設移動是成功的&#xff0c;服務端同步其他客戶端 B玩家&#xff0c;B玩家 中用一個隊列 Queue 來裝服務端來的移動指令&#xff0c;然后客戶端在updata中做…