AssetBundle入门笔记02

AB包加载的四种方式

1.AssetBundle.LoadFromMemoryAsync(从内存中加载)
异步:

   IEnumerator LoadFromMemoryAsync(string path)
    {        
        AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return createRequest; //等待加载完成
        AssetBundle bundle = createRequest.assetBundle;
        GameObject greenCube = bundle.LoadAsset("greenCube");
        Instantiate(greenCube);
    }

同步:

    void LoadFromMemory(string path)
    {
        AssetBundle bundle = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
        GameObject greenCube = bundle.LoadAsset("greenCube");
        Instantiate(greenCube);
    }

File.ReadAllBytes(path)可以用任何的字节数组来取代。

2.AssetBundle.LoadFromFile(从文件中加载)
如果包是未压缩的或者块(LZ4)压缩的,LoadFromFile将直接从磁盘加载;如果是完全压缩(LZMA)包将先解压缩包再加载

  void LoadFromFile(string path)
    {
        AssetBundle bundle = AssetBundle.LoadFromFile(path);
        if (bundle == null)
        {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        GameObject greenCube = bundle.LoadAsset("greenCube");
        Instantiate(greenCube);
    }

3.WWW.LoadFromCacheOrDownload(已被4取代)
可以从远程服务器下载AssetBundles或加载本地AssetBundles

  IEnumerator Start()
    {
        while (!Caching.ready)
            yield return null;//返回null挂起一帧
        //输入地址+版本号,地址需要绝对路径
        WWW www = WWW.LoadFromCacheOrDownload(@"file://F:\Unity3D Little Games\AssetBundle_Learning\MY_AssetBundles\cube.ab", 1);
        // WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/MY_AssetBundles/cube.ab", 1);//服务器路径
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle bundle = www.assetBundle;
        GameObject greenCube = bundle.LoadAsset("greenCube");
        Instantiate(greenCube);
    }

crc参数是manifest文件里面的一串校验码,主要用来检测或校验数据传输或者保存后可能出现的错误

4.UnityWebRequest

  IEnumerator WebRequest()
    {
        //string uri = @"file://F:\Unity3D Little Games\AssetBundle_Learning\MY_AssetBundles\cube.ab";//本地地址
        string uri = @"http://localhost/MY_AssetBundles\cube.ab";//服务器地址
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.Send();//等待下载完成
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        //另一种方法
        //AssetBundle bundle = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

        GameObject greenCube = bundle.LoadAsset("greenCube");
        Instantiate(greenCube);
    }

使用AB包

  • 加载单个Gameobject:
GameObject gameObject = loadedAssetBundle.LoadAsset(assetName);
  • 加载所有:
Unity.Object[] objectArray = loadedAssetBundle.LoadAllAssets();
  • 异步加载:
AssetBundleRequest request = loadedAssetBundleObject.LoadAssetAsync(Name);
yield return request;
var loadedAsset = request.asset;
AssetBundleRequest request = loadedAssetBundle.LoadAllAssetsAsync();
yield return request;
var loadedAssets = request.allAssets;

加载AB包清单(manifest)

 void LoadManifest()
    {
        AssetBundle manifestAB = AssetBundle.LoadFromFile("MY_AssetBundles/MY_AssetBundles");//加载总包
        AssetBundleManifest manifest = manifestAB.LoadAsset("AssetBundleManifest");//加载总包的manifest
        string[] asset_names = manifest.GetAllAssetBundles();//获取每个包的名字       
        string[] strs = manifest.GetDirectDependencies("Cube.ab");//获取某个包所依赖的所有包的名字
        foreach (string item in strs)
        {
            AssetBundle.LoadFromFile("MY_AssetBundles/" + item);//加载每个依赖包
        }

        AssetBundle cubes = AssetBundle.LoadFromFile("MY_AssetBundles/" + asset_names[0]);//获得第一个包
        GameObject cube = cubes.LoadAsset("Cube");
        Instantiate(cube);
    }

AB包的卸载

  • 卸载的好坏
    1.减少内存使用
    2.有可能导致丢失

  • 什么时候去卸载资源
    AssetBundle.Unload(true) : 卸载所有资源,即使有资源被使用着
    (1.在关卡切换、场景切换 2.资源没被用的时候调用)
    AssetBundle.Unload(false) : 卸载所有没用被使用的资源
    个别资源卸载问题:
    1.通过 Resources.UnloadUnusedAssets.
    2.场景切换的时候


AB包浏览工具

下载地址

AssetBundle入门笔记02_第1张图片

你可能感兴趣的:(AssetBundle入门笔记02)