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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Unity—AssetBundle的打包及四种加载资源方式

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity—AssetBundle的打包及四种加载资源方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AssetBundle打包:腳本放在Editor文件夾內

具體代碼如下: using UnityEditor; using System.IO;public class CreateAssetBundles {[MenuItem("自定義菜單/資源打包")]static void BuildAllAssetBundles(){//定義文件夾名字string dir = "AssetBundles";//文件夾不存在,則創建if(!Directory.Exists(dir)){Directory.CreateDirectory(dir);}//資源打包 BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);}}AssetBundle的四種方式資源加載,腳本綁在場景任意物體即可 具體代碼如下:using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.Networking;public class DownLoad : MonoBehaviour {IEnumerator Start () {//資源包路徑string path1 = "AssetBundles/cubewall.unity3d";//共享依賴資源包路徑string path2 = "AssetBundles/share.unity3d";//第一種加載AB的方式 ,從內存中加載 LoadFromMemory#region//方法一:異步加載//加載資源AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));yield return request;//加載共同依賴資源,如貼圖、材質AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));yield return request2;AssetBundle ab = request.assetBundle;AssetBundle ab2 = request2.assetBundle;//使用里面的資源GameObject wallPrefab1 = ab.LoadAsset("CubeWall");Instantiate(wallPrefab1);//方法二:同步加載(無需用協程)//加載資源AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1));//加載共同依賴資源,如貼圖、材質AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));//使用里面的資源GameObject wallPrefab2 = ab.LoadAsset("CubeWall");Instantiate(wallPrefab2);#endregion//第二種加載AB的方式 ,從本地加載 LoadFromFile(無需用協程)#regionAssetBundle ab5 = AssetBundle.LoadFromFile(path1);AssetBundle ab6 = AssetBundle.LoadFromFile(path2);GameObject wallPrefab3 = ab5.LoadAsset("CubeWall");Instantiate(wallPrefab3);#endregion//第三種加載AB的方式 ,從本地或服務器加載 WWW(將被棄用)#region//是否準備好while (Caching.ready == false){yield return null;}//從本地加載//WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);//從服務器加載WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);yield return www;//是否報錯if (string.IsNullOrEmpty(www.error) == false){Debug.Log(www.error); yield break;}AssetBundle ab7 = www.assetBundle;//使用里面的資源GameObject wallPrefab4 = ab7.LoadAsset("CubeWall");Instantiate(wallPrefab4);#endregion//第四種加載AB方式 從服務器端下載 UnityWebRequest(新版Unity使用)#region//服務器路徑 localhost為IPstring uri = @"http://localhost/AssetBundles/cubewall.unity3d";UnityWebRequest request3 = UnityWebRequest.GetAssetBundle(uri);yield return request3.Send();//AssetBundle ab8 = ((DownloadHandlerAssetBundle)request3.downloadHandler).assetBundle;AssetBundle ab8 = DownloadHandlerAssetBundle.GetContent(request3);//使用里面的資源GameObject wallPrefab5 = ab8.LoadAsset("CubeWall");Instantiate(wallPrefab5);//加載cubewall.unity3d資源包所依賴的資源包AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");AssetBundleManifest manifest = manifestAB.LoadAsset("AssetBundleManifest");//foreach(string name in manifest.GetAllAssetBundles())//{// print(name);//}//cubewall.unity3d資源包所依賴的資源包的名字string[] strs = manifest.GetAllDependencies("cubewall.unity3d");foreach (string name in strs){AssetBundle.LoadFromFile("AssetBundles/" + name);}#endregion} }

?

原文地址:http://blog.sina.com.cn/s/blog_140bb6bd40102xajb.html

轉載于:https://www.cnblogs.com/wuwenbo/p/7813246.html

總結

以上是生活随笔為你收集整理的Unity—AssetBundle的打包及四种加载资源方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。