franciscochonge 发表于 2021-3-31 18:30

Unity AB加载笔记

using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;using UnityEngine.Networking;public class LoadFromFileExample : MonoBehaviour{    // Start is called before the first frame update    void Start()    {      //1.第二种加载AB的方式LoadFromFile同步加载(本地加载)      //AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");      //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/cubewall.unity3d");      //GameObject wallPrefab = ab.LoadAsset<GameObject>("cubeWall");      //Instantiate(wallPrefab);      ////第一种加载AB的方式LoadFromMemory同步加载(当AB包使用UDP或者TCP协议的时候可以运用此方法)      //string path = "AssetBundles/cubewall.unity3d";      //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));      //GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      //Instantiate(wallPrefab);      StartCoroutine(LoadUnityWebRequest());    }    IEnumerator StartFromMemoryAsync()    {      //第一种加载AB的方式LoadFromMemoryAsync异步加载(当AB包使用UDP或者TCP协议的时候可以运用此方法)      string path = "AssetBundles/cubewall.unity3d";      AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));      yield return request;      AssetBundle ab = request.assetBundle;      //使用里面的资源      GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      Instantiate(wallPrefab);    }    IEnumerator StartFromFileAsync()    {      //1.第二种加载的方式LoadFromFileAsync异步加载(本地加载)      string path = "AssetBundles/cubewall.unity3d";      AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);      yield return request;      AssetBundle ab = request.assetBundle;      //使用资源      GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      Instantiate(wallPrefab);    }    IEnumerator WWWLoadFrom()    {      //此方法已弃用      string path = "AssetBundles/cubewall.unity3d";      //第三种加载AB的方式WWW      while (Caching.ready == false)      {            yield return null;      }      //fiel://    fiel:///      //WWW www = WWW.LoadFromCacheOrDownload(@"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d", 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 ab = www.assetBundle;      //使用资源      GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      Instantiate(wallPrefab);    }    //第四种方式 使用UnityWebRequest    IEnumerator LoadUnityWebRequest()    {      //string uri = @"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d";      string uri = @"http://localhost/AssetBundles/cubewall.unity3d";      UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);      yield return request.SendWebRequest();      AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);      //使用资源      GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      Instantiate(wallPrefab);    }}加载Manifest文件 检查加载依赖包

IEnumerator LoadUnityWebRequest()    {      //string uri = @"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d";      string uri = @"http://localhost/AssetBundles/cubewall.unity3d";      UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);      yield return request.SendWebRequest();      AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);      //使用资源      GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");      Instantiate(wallPrefab);      //第一种方式本地加载Manifest文件 检查加载依赖包      //AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");      //AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");      //string[] strs = manifest.GetDirectDependencies("cubewall.unity3d");      //foreach (string name in strs)      //{      //    print(name);      //    AssetBundle.LoadFromFile("AssetBundles/"+name);      //}      //第二种方式加载服务器Manifest文件 检查加载依赖包      UnityWebRequest request_1 = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/AssetBundles");      yield return request_1.SendWebRequest();      AssetBundle ab_1 = DownloadHandlerAssetBundle.GetContent(request_1);      AssetBundleManifest manifest = ab_1.LoadAsset<AssetBundleManifest>("AssetBundleManifest");      string[] strs = manifest.GetDirectDependencies("cubewall.unity3d");      foreach (string name in strs)      {            print(name);            UnityWebRequest request_2 = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/"+name);            yield return request_2.SendWebRequest();            AssetBundle ab_2 = DownloadHandlerAssetBundle.GetContent(request_2);               }    }
页: [1]
查看完整版本: Unity AB加载笔记