轉載自技術情殤的博客.
原文地址:http://blog.sina.com.cn/s/blog_140bb6bd40102xajb.html
AssetBundle打包:腳本放在Editor文件夾內
具體代碼如下:
C# Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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的四種方式資源加載,腳本綁在場景任意物體即可
具體代碼如下:
C# Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 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(無需用協程) #region AssetBundle 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爲IP string 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 } } |