日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一、创建Assetbundle 在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。

發(fā)布時間:2023/11/30 编程问答 25 豆豆

一、創(chuàng)建Assetbundle

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

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

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



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

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

  • 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)

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

    參數(shù)2:可以放入一個數(shù)組對象。

    參數(shù)3:要打包到的路徑

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

    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?
  • ///?<summary>??
  • ///?將選中的預制打包到一起??
  • ///?</summary>??
  • [MenuItem("AssetBundleDemo/Create?AssetBundles?Together")]??
  • static?void?CreateAssetBundleTogether(){??
  • ????//要打包的對象??
  • ????Object[]?selects?=?Selection.GetFiltered?(typeof(Object),SelectionMode.DeepAssets);??
  • ????//要打包到的路徑??
  • ????string?targetPath?=?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/Together.assetbundle";??
  • ????if(BuildPipeline.BuildAssetBundle(null,selects,targetPath,BuildAssetBundleOptions.CollectDependencies)){??
  • ????????Debug.Log("Packed?successfully!");??
  • ??
  • ????}else{??
  • ????????Debug.Log("Packed?failly!");??
  • ????}??
  • ????//刷新編輯器(不寫的話要手動刷新)??
  • ????AssetDatabase.Refresh?();??
  • }??


  • 四、讀取

    [csharp]?view plaincopy print?
  • using?UnityEngine;??
  • using?System.Collections;??
  • ??
  • public?class?ReanAssetbundle?:?MonoBehaviour?{??
  • ??
  • ????//不同平臺下StreamingAssets的路徑是不同的,這里需要注意一下。??
  • ????public?static?readonly?string?m_PathURL?=??
  • ????????#if?UNITY_ANDROID??
  • ????????"jar:file://"?+?Application.dataPath?+?"!/assets/";??
  • ????????#elif?UNITY_IPHONE??
  • ????????Application.dataPath?+?"/Raw/";??
  • ????????#elif?UNITY_STANDALONE_WIN?||?UNITY_EDITOR??
  • ????????"file://"?+?Application.dataPath?+?"/AssetBundleLearn/StreamingAssets/";??
  • ????????#else??
  • ????????string.Empty;??
  • ????????#endif??
  • ??
  • ????void?OnGUI(){??
  • ????????if(GUILayout.Button("加載分開打包的Assetbundle")){??
  • ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"One.assetbundle"));??
  • ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+??"Two.assetbundle"));??
  • ????????????StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL?+?"Three.assetbundle"));??
  • ??
  • ????????}??
  • ??????????
  • ????????if(GUILayout.Button("加載打包在一起的Assetbundle")){??
  • ????????????StartCoroutine(LoadGameObjectPackedTogether(m_PathURL?+?"Together.assetbundle"));??
  • ????????}??
  • ??????????
  • ????}??
  • ????//單獨讀取資源??
  • ????private?IEnumerator?LoadGameObjectPackedByThemselves(string?path){??
  • ????????WWW?bundle?=?new?WWW?(path);??
  • ????????yield?return?bundle;??
  • ??
  • ????????//加載??
  • ????????yield?return?Instantiate?(bundle.assetBundle.mainAsset);??
  • ????????bundle.assetBundle.Unload?(false);??
  • ????}??
  • ??
  • ????IEnumerator??LoadGameObjectPackedTogether?(string?path)??
  • ????{??
  • ????????WWW?bundle?=?new?WWW?(path);??
  • ????????yield?return?bundle;??
  • ??
  • ????????Object?one?=?bundle.assetBundle.Load?("One");??
  • ????????Object?two?=?bundle.assetBundle.Load?("Two");??
  • ????????Object?three?=?bundle.assetBundle.Load?("Three");??
  • ??
  • ????????//加載??
  • ????????yield?return?Instantiate?(one);??
  • ????????yield?return?Instantiate?(two);??
  • ????????yield?return?Instantiate?(three);??
  • ????????bundle.assetBundle.Unload?(false);??
  • ????}??
  • }??
  • 總結

    以上是生活随笔為你收集整理的一、创建Assetbundle 在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。