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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AssetBundle

發(fā)布時(shí)間:2025/3/14 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AssetBundle 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

AssetBundle

UnityAssetBundle

設(shè)置資源AB

基本上unity所包含的資源文件,除了腳本文件都可以設(shè)置成ab資源。

生成AB資產(chǎn)

生成AB有兩種方式

  • 自定義打包
  • AssetBundles-Browser-master插件
  • 預(yù)覽AB
  • 這里使用插件打包,對于目前所有ab資源一目了然,如有ab設(shè)置錯(cuò)誤也會有錯(cuò)誤提示,我們已經(jīng)設(shè)置好ab資源用它打開如圖

    正是因?yàn)閡nity支持‘/’來分層,所以我們用文件層次來設(shè)置ab名稱
    才使得對應(yīng)ab包層級目錄結(jié)構(gòu)清晰明確
    2. 打包設(shè)置


    enter description here

    根據(jù)當(dāng)前ab資源選擇不同壓縮模式比較

    壓縮模式UnCompressionLZ4LZMA
    Andriod平臺大小325kb160kb119kb
    • LZ4 解壓讀取速度快(推薦)
    • LZMA 壓縮率高,文件占用空間小,解壓比較慢

    獲取AB加載

    大致流程圖示

    1559045934508.drawio.html 42.38 KB

    下面示例從服務(wù)器中獲取 tom ab資源一系列流程

    要測試在服務(wù)中的ab資源


    enter description here

    測試代碼

    1.連接服務(wù)器,獲取主依賴

    調(diào)用入口

    StartCoroutine(GetDepends(host, "AB_Andriod_LZ4"));

    具體實(shí)現(xiàn)

    /// <summary>/// 服務(wù)器地址/// </summary>string host = @"https://ab-test-1258795305.cos.ap-shanghai.myqcloud.com/";/// <summary>/// ab-依賴/// </summary>public Dictionary<string/*ab path*/, AB_DATA> dic_AB = new Dictionary<string, AB_DATA>();/// <summary>/// 獲取主依賴,本地存儲依賴關(guān)系,下載全部ab/// </summary>/// <param name="host_path">服務(wù)器根地址</param>/// <param name="dir_name">服務(wù)器下ab根文件名,以及主ab文件名稱</param>/// <returns></returns>IEnumerator GetDepends(string host_path, string dir_name){dic_AB.Clear();//目錄跟路徑string root_path = host_path + "/" + dir_name;//獲取主ab,拿到manifest獲取依賴關(guān)系AssetBundleManifest assetBundleManifestServer = null; //服務(wù)器 總的依賴關(guān)系 UnityWebRequest ServerManifestWWW = UnityWebRequestAssetBundle.GetAssetBundle(root_path + "/" + dir_name);Debug.Log("HTTP : " + ServerManifestWWW.url.ToString());yield return ServerManifestWWW.SendWebRequest();assetBundleManifestServer = (AssetBundleManifest)DownloadHandlerAssetBundle.GetContent(ServerManifestWWW).LoadAsset("AssetBundleManifest");//存儲各依賴關(guān)系string[] depends_name = assetBundleManifestServer.GetAllAssetBundles();for (int i = 0; i < depends_name.Length; i++){AB_DATA ab_data = new AB_DATA();ab_data.name = depends_name[i];UnityWebRequest target_uwq = UnityWebRequestAssetBundle.GetAssetBundle(Path.Combine(root_path, ab_data.name));yield return target_uwq.SendWebRequest();if (target_uwq.isDone){//下載ab到本地StartCoroutine(DownLoadAssetBundelAbdSave(Path.Combine(root_path, ab_data.name)));//dependsvar depends = assetBundleManifestServer.GetAllDependencies(ab_data.name);ab_data.depends_name = new string[depends.Length];for (int j = 0; j < depends.Length; j++){ab_data.depends_name[j] = depends[j];}dic_AB.Add(depends_name[i], ab_data);}}}

    2.緩存已下載的ab資源

    /// <summary>/// 下載AB文件并保存到本地/// </summary>/// <returns></returns>IEnumerator DownLoadAssetBundelAbdSave(string url){WWW www = new WWW(url);yield return www;if (www.isDone){//表示資源下載完畢使用IO技術(shù)把www對象存儲到本地SaveAssetBundle(Path.GetFileName(url), www.bytes, www.bytes.Length);}}/// <summary>/// 存儲AB文件到本地/// </summary>private void SaveAssetBundle(string fileName, byte[] bytes, int count){string dir_path = Application.persistentDataPath + "/" + fileName;Debug.Log("存儲路徑 : " + dir_path);Stream sw = null;FileInfo fileInfo = new FileInfo(dir_path);sw = fileInfo.Create();sw.Write(bytes, 0, count);sw.Flush();sw.Close();sw.Dispose();Debug.Log(fileName + "下載并存儲完成");}

    3.從本地磁盤讀取ab并實(shí)例化

    調(diào)用入口

    private void LoadStream_OnClick(){//設(shè)置要加載的目標(biāo)物體名string ab_name = "tom.prefab";string dir_path = Application.persistentDataPath + "/" + ab_name;//從已緩存的字節(jié)文件中加載tomStartCoroutine(LoadABFormStream(dir_path));//加載tom所需的依賴string[] ad_names = dic_AB[ab_name].depends_name;for (int i = 0; i < ad_names.Length; i++){string dir_path_depends = Path.Combine(Application.persistentDataPath, ad_names[i]);//加載依賴時(shí),直接加載出其ab包即可,不需要分解其依賴類型再去實(shí)例,unity自會處理AssetBundle.LoadFromFile(dir_path_depends);//所以下面部分就不需要了// StartCoroutine(LoadABFormStream(dir_path_depends));}}

    相關(guān)方式實(shí)現(xiàn)

    /// <summary>/// 從字節(jié)文件讀取ab,并且實(shí)例化/// </summary>/// <param name="dir_path"></param>/// <returns></returns>IEnumerator LoadABFormStream(string dir_path){FileStream fileStream = new FileStream(dir_path, FileMode.Open, FileAccess.Read);AssetBundleCreateRequest request = AssetBundle.LoadFromStreamAsync(fileStream);yield return request;AssetBundle myLoadedAssetBundle = request.assetBundle;if (myLoadedAssetBundle == null){Debug.Log("Failed to load AssetBundle!");yield break;}//實(shí)例化abInstanceAB_FormAssetBundle(myLoadedAssetBundle, myLoadedAssetBundle.name);fileStream.Close();}private void InstanceAB_FormAssetBundle(AssetBundle assetBundle, string obj_name){Object gameObject = null;if (obj_name.Contains(".mat")){gameObject = assetBundle.LoadAsset<Material>(obj_name);Instantiate<Material>((Material)gameObject);}else if (obj_name.Contains(".prefab")){gameObject = assetBundle.LoadAsset<GameObject>(obj_name);Instantiate<GameObject>((GameObject)gameObject);}}

    轉(zhuǎn)載于:https://www.cnblogs.com/Jean90/p/10940299.html

    總結(jié)

    以上是生活随笔為你收集整理的AssetBundle的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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