AssetBundle的加载和使用

share.unity3d、cubewall.unity3d文件的构建查看

https://blog.csdn.net/cuijiahao/article/details/105552405

 https://blog.csdn.net/cuijiahao/article/details/105482962

public class LoadFromFileExample : MonoBehaviour {

 

    void Start () {

        string path = "AssetBundles/cubewall.unity3d";

 

        AssetBundle.LoadFromFile("share.unity3d");//加载共享依赖包

 

        //单个提取资源

        AssetBundle ab = AssetBundle.LoadFromFile(path);//同步加载AB包并返回一个AssetBundle对象

        GameObject wallPrefab = ab.LoadAsset("CubeWall");//AssetBundle里取出一个资源

        Instantiate(wallPrefab);//将资源实例化到场景中

 

        //提取所有资源

        AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");

        Object[] objs = ab.LoadAllAssets();//加载AB包里所有的资源,并返回obj数组

        foreach (Object o in objs)//实例化所有的资源

        {

            Instantiate(o);

        }

}

查看相关文档

AssetBundle的加载和使用_第1张图片

AssetBundle的使用 

 AssetBundle的加载和使用_第2张图片

 1,AssetBundle.LoadFromMemoryAsync

从内存加载

using System.Collections;

using System.IO;

using UnityEngine;

 

public class LoadFromFileExample : MonoBehaviour {

 

    // 异步加载函数返回值必须是IEnumerator

    IEnumerator Start () {

        string path = "AssetBundles/cubewall.unity3d";

 

        //第一种加载AB的方式 LoadFromMemoryAsync  从内存进行加载

        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//异步加载

        yield return request;//等待加载完成

        AssetBundle ab = request.assetBundle;//获取AssetBundle对象

 

        //使用里面的资源

        GameObject wallPrefab = ab.LoadAsset("CubeWall");

        Instantiate(wallPrefab);

}

 

    // 同步加载不需要IEnumerator

    void Start () {

        string path = "AssetBundles/cubewall.unity3d";       

        //同步加载

        AssetBundle ab1 = AssetBundle.LoadFromMemory(File.ReadAllBytes(path)); //File.ReadAllBytes是将文件转换为byte[]

        AssetBundle ab = request.assetBundle;//获取AssetBundle对象

        //使用里面的资源

        GameObject wallPrefab = ab.LoadAsset("CubeWall");

        Instantiate(wallPrefab);

    }

}

 2,AssetBundle.LoadFromFile

本地加载

using System.Collections;

using System.IO;

using UnityEngine;

 

public class LoadFromFileExample : MonoBehaviour {

 

    // 异步加载函数返回值必须是IEnumerator

    IEnumerator Start () {

        string path = "AssetBundles/cubewall.unity3d";

 

        //第二种加载AB的方式 LoadFromFile 从本地进行加载

        //异步加载

        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

        yield return request;//等待返回

        AssetBundle ab = request.assetBundle;//获取AssetBundle对象

        //使用里面的资源

        GameObject wallPrefab = ab.LoadAsset("CubeWall");

        Instantiate(wallPrefab);

    }

}

3,WWW.LoadFromCacheOrDownload

网络加载

using System.Collections;

using System.IO;

using UnityEngine;

 

public class LoadFromFileExample : MonoBehaviour {

 

    // 异步加载函数返回值必须是IEnumerator

    IEnumerator Start () {

        string path = "AssetBundles/cubewall.unity3d";

 

        //第三种加载AB的方式 WWW

        while (Caching.ready == false)

        {

            yield return null;

        }

        //本地

        WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\Unity Project Workspace\AssetBundleProject\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;

    }

}

4,UnityWebRequest 

using System.Collections;

using System.IO;

using UnityEngine;

using UnityEngine.Networking;

 

public class LoadFromFileExample : MonoBehaviour {

 

    // 异步加载函数返回值必须是IEnumerator

    IEnumerator Start () {

        string path = "AssetBundles/cubewall.unity3d";      

 

        //第四种方式 使用UnityWebRequest

        string uri = @"file:///E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d";//本地

        string uri = @"http://localhost/AssetBundles/cubewall.unity3d";//服务端

        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);

        yield return request.Send();//开始从服务端下载

        //获取AssetBundle的两种方式

        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//第一种方式

        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//第二种方式

 

        //使用里面的资源

        GameObject wallPrefab = ab.LoadAsset("CubeWall");

        Instantiate(wallPrefab);

 

        //保存到本地

        File.WriteAllBytes("要保存的位置", request.downloadHandler.data);

    }

}

 保存到本地

//保存到本地

File.WriteAllBytes("要保存的位置", request.downloadHandler.data);

从AssetBundle里面加载资源 

GetAllAssetNames

获取所有的资源名字

LoadAllAssets

获取所有的资源(不包括依赖的资源)

LoadAllAssetsAsync

异步获取所有资源(不包括依赖的资源)

LoadAsset

获取资源

LoadAssetAsync

异步获取资源

LoadAssetWithSubAssets

获取所有的子资源

通过Manifest文件加载所有的包

通过Manifest文件的得到某个包的依赖 

using System.Collections;

using System.IO;

using UnityEngine;

using UnityEngine.Networking;

 

public class LoadFromFileExample : MonoBehaviour {

 

    // 异步加载函数返回值必须是IEnumerator

    void Start () {

        string path = "AssetBundles/cubewall.unity3d";

 

        //加载AssetBundles文件夹里的AssetBundles文件

        AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");

        //加载AssetBundleManifest

        AssetBundleManifest manifest = manifestAB.LoadAsset("AssetBundleManifest");

        //获取所有的AssetBundle包的名字(包括依赖包的名字)

        foreach (string name in manifest.GetAllAssetBundles())

        {

            print(name);

        }

        //获取cubewall.unity3d这个包所依赖的所有包的名字

        string[] strs = manifest.GetAllDependencies("cubewall.unity3d");

        //加载所有依赖的包

        foreach (string name in strs)

        {

            print(name);

            AssetBundle.LoadFromFile("AssetBundles/" + name);//加载依赖的包

        }

    }

}

你可能感兴趣的:(AssetBundle)