AssetBundle加载和卸载学习笔记

string path = "AssetBundles/cubewall.unity3d";

        //本地同步加载
        AssetBundle ab = AssetBundle.LoadFromFile ("AssetBundles/share.unity3d"); //要先加载所依赖的资源
        AssetBundle ab2 = AssetBundle.LoadFromFile (path);
        //本地异步加载,需要协程
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync (path);
        yield return request;
        AssetBundle ab = request.assetBundle;

        //内存异步加载,需要协程
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync (File.ReadAllBytes (path));
        yield return request;
        AssetBundle ab = request.assetBundle;
        //内存同步加载
        AssetBundle ab = AssetBundle.LoadFromMemory (File.ReadAllBytes (path));

        //通过WWW加载,已被Unity弃用
        while (!Caching.ready)
            yield return null;
        WWW www = WWW.LoadFromCacheOrDownload (@"file:///G:\unity\Project\AssetBundleLearn\AssetBundles\CubeWall.unity3d",1);
        WWW www = WWW.LoadFromCacheOrDownload (@"http://localhost/AssetBundles/CubeWall.unity3d",1);
        yield return www;
        if (!string.IsNullOrEmpty (www.error))
        {
            Debug.Log (www.error);
            yield break;
        }
        AssetBundle ab = www.assetBundle;

        //使用UnityWbeRequest加载
        string uri = @"file:///G:\unity\Project\AssetBundleLearn\AssetBundles\CubeWall.unity3d";
        //服务器端地址
//        string uri = @"http://localhost/AssetBundles/CubeWall.unity3d";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle (uri);
        yield return request.SendWebRequest ();
        //两种方法
//        AssetBundle ab = DownloadHandlerAssetBundle.GetContent (request);
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

        //实例化资源
        GameObject wallPrefab = ab.LoadAsset<GameObject> ("CubeWall");
        Instantiate (wallPrefab);

                //通过Manifest加载依赖资源
        AssetBundle manifestAB = AssetBundle.LoadFromFile ("AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest> ("AssetBundleManifest");

        string[] strs = manifest.GetAllDependencies ("cubewall.unity3d");
        foreach (string name in strs)
        {
            print (name);
            AssetBundle.LoadFromFile ("AssetBundles/" + name);
        }
        foreach (string name in manifest.GetAllAssetBundles())
            print (name);

--------------------- 
作者:tubugs 
来源:CSDN 
原文:https://blog.csdn.net/tubugs/article/details/85054321 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值