保存AssetBundle资源 using UnityEditor; using System.IO; public class CreateAssetBundles { [MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundle() { string dir = "AssetBundles"; // string dir = "D:\AssetBundles"; if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } BuildPipeline.BuildAssetBundles(dir,BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64); } } 加载本地AB资源的几种方式 using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class LoadFromFileExample : MonoBehaviour { IEnumerator Start() { string path = ("AssetBundles/CubeWall.unity3d"); AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d"); // AssetBundle ab= AssetBundle.LoadFromFile("AssetBundles/CubeWall.unity3d"); // GameObject wallPrefab= ab.LoadAsset<GameObject>("CubeWall"); // Instantiate(wallPrefab); // //Object[] objs= ab.LoadAllAssets(); // // foreach (Object o in objs) // // { // // Instantiate(o); // // } //第一种加载AB的方式 loadfromMemoryAsync //AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path)); //yield return request; //AssetBundle ab = request.assetBundle; // AssetBundle ab= AssetBundle.LoadFromMemory(File.ReadAllBytes(path)); // //第二种加载AB的方式 loadFromFile; // AssetBundleCreateRequest request= AssetBundle.LoadFromFileAsync(path); //yield return request; //AssetBundle ab = request.assetBundle; //第三种加载AB的方式 www while (Caching.ready == false) { yield return null; } WWW www= WWW.LoadFromCacheOrDownload(@"file:///E:\项目\Asset Bundle Project\AssetBundles\CubeWall.unity3d", 1); yield return www; if (www.error != null) { Debug.Log(www.error); yield break; } AssetBundle ab = www.assetBundle; //使用 GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall"); Instantiate(wallPrefab); } //private void Update() //{ // if (Input.GetMouseButtonDown(0)) // { // AssetBundle.LoadFromFile("AssetBundles/share.unity3d"); // } //} } 从服务器下载资源 WWW www= WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundle Project/AssetBundles/Cubewall.unity3d", 1); //第四种方式 UnityWebRqeuest // string uri = (@"file:///E:\项目\Asset Bundle Project\AssetBundles\\CubeWall.unity3d"); string uri = @"http://localhost/AssetBundles/Cubewall.unity3d"; UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri); yield return request.Send(); //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle; 通过mainfest加载当前资源的所有依赖 AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles"); AssetBundleManifest mainfest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBUndleManifest"); //foreach (var name in mainfest.GetAllAssetBundles()) //{ // print(name); //} string[] strs = mainfest.GetAllDependencies("cubewall.unity3d"); foreach (var name in strs) { print(name); AssetBundle.LoadFromFile("AssetBundles/" + name); }
AssetBundle 资源加载的几种方式
最新推荐文章于 2024-06-02 22:03:23 发布