Unity—AssetBundle的打包及四种加载资源方式
生活随笔
收集整理的這篇文章主要介紹了
Unity—AssetBundle的打包及四种加载资源方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
AssetBundle打包:腳本放在Editor文件夾內(nèi)
具體代碼如下: using UnityEditor; using System.IO;public class CreateAssetBundles {[MenuItem("自定義菜單/資源打包")]static void BuildAllAssetBundles(){//定義文件夾名字string dir = "AssetBundles";//文件夾不存在,則創(chuàng)建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的方式 ,從內(nèi)存中加載 LoadFromMemory#region//方法一:異步加載//加載資源AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));yield return request;//加載共同依賴資源,如貼圖、材質(zhì)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);//方法二:同步加載(無需用協(xié)程)//加載資源AssetBundle ab3 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path1));//加載共同依賴資源,如貼圖、材質(zhì)AssetBundle ab4 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path2));//使用里面的資源GameObject wallPrefab2 = ab.LoadAsset("CubeWall");Instantiate(wallPrefab2);#endregion//第二種加載AB的方式 ,從本地加載 LoadFromFile(無需用協(xié)程)#regionAssetBundle ab5 = AssetBundle.LoadFromFile(path1);AssetBundle ab6 = AssetBundle.LoadFromFile(path2);GameObject wallPrefab3 = ab5.LoadAsset("CubeWall");Instantiate(wallPrefab3);#endregion//第三種加載AB的方式 ,從本地或服務(wù)器加載 WWW(將被棄用)#region//是否準(zhǔn)備好while (Caching.ready == false){yield return null;}//從本地加載//WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:AssetBundleProject\AssetBundleProject\AssetBundles", 1);//從服務(wù)器加載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方式 從服務(wù)器端下載 UnityWebRequest(新版Unity使用)#region//服務(wù)器路徑 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轉(zhuǎn)載于:https://www.cnblogs.com/wuwenbo/p/7813246.html
總結(jié)
以上是生活随笔為你收集整理的Unity—AssetBundle的打包及四种加载资源方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 套接字I/O模型-重叠I/O
- 下一篇: LeetCode 167 两数之和 II