AssetBundle(2)AssetBundle的加载和使用

四种加载方式

1,AssetBundle.LoadFromMemoryAsync           第一种从内存异步加载 IEnumerator
2,AssetBundle.LoadFromFileAsync                  第二种从本地异步加载 IEnumerator
3,WWW.LoadFromCacheOrDownload             第三种使用www来加载
4,UnityWebRequest                                          第四种使用UnityWebRequest

一、AssetBundle.LoadFromMemoryAsync


using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundles : MonoBehaviour {

    IEnumerator Start ()
    {
        string rootPath = Application.streamingAssetsPath;

        string path = rootPath+ "/AssetBundles/cubewall.unity3d";

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


        //第一种从内存异步加载 IEnumerator
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return request;
        AssetBundle ab = request.assetBundle;

        AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
        AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}

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

        foreach (string str in dependencies)
        {
            print(str);
            AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
        }

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


        //AssetBundle.UnloadAllAssetBundles(true);

    }

}

二、AssetBundle.LoadFromFileAsync

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundles : MonoBehaviour {

    IEnumerator Start ()
    {
        string rootPath = Application.streamingAssetsPath;

        string path = rootPath+ "/AssetBundles/cubewall.unity3d";

        AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/scenes/share.unity3d");
        
        //第二种从本地异步加载 IEnumerator
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        yield return request;
        AssetBundle ab = request.assetBundle;

        //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scenes/cubewall.unity3d");

        AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
        AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}

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

        foreach (string str in dependencies)
        {
            print(str);
            AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
        }

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


        //AssetBundle.UnloadAllAssetBundles(true);

    }

}

三、WWW.LoadFromCacheOrDownload

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundles : MonoBehaviour {

    IEnumerator Start ()
    {
        string rootPath = Application.streamingAssetsPath;

        string path = rootPath+ "/AssetBundles/cubewall.unity3d";

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

        //第三种使用www来加载
        while (!Caching.ready)
        {
            yield return null;
        }
        WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\UnityProjects\Unity5.4\AssetBundleProjects\Assets\StreamingAssets\AssetBundles\AssetBundles\cubewall.unity3d", 1);
        //WWW www = WWW.LoadFromCacheOrDownload(@"http://192.168.1.102/AssetBundles\cubewall.unity3d", 1);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }

        AssetBundle ab = www.assetBundle;

        AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
        AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}

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

        foreach (string str in dependencies)
        {
            print(str);
            AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
        }

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


        //AssetBundle.UnloadAllAssetBundles(true);

    }

}

四、UnityWebRequest

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundles : MonoBehaviour {

    IEnumerator Start ()
    {
        string rootPath = Application.streamingAssetsPath;

        string path = rootPath+ "/AssetBundles/cubewall.unity3d";

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

        //第四种使用UnityWebRequest
        string url = @"file://E:\UnityProjects\Unity5.4\AssetBundleProjects\Assets\StreamingAssets\AssetBundles\cubewall.unity3d";
        //string url = @"http://192.168.1.102/AssetBundles\cubewall.unity3d";

        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.Send();

        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;

        AssetBundle assetBundleManifest = AssetBundle.LoadFromFile(rootPath+"/AssetBundles/AssetBundles");
        AssetBundleManifest manifest = assetBundleManifest.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}

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

        foreach (string str in dependencies)
        {
            print(str);
            AssetBundle.LoadFromFile(rootPath+"/AssetBundles/" + str);
        }

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


        //AssetBundle.UnloadAllAssetBundles(true);

    }

}

加载Manifests

        AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);
        AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] dependencies = manifest.GetAllDependencies("assetBundle"); //Pass the name of the bundle you want the dependencies for.
        foreach (string name in manifest.GetAllAssetBundles())
        {
            Debug.Log(name);
        }
        foreach (string dependency in dependencies)
        {
            AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
        }

AssetBundle的卸载

卸载有两个方面
1,减少内存使用
2,有可能导致丢失

什么时候去卸载资源

AssetBundle.Unload(true)卸载所有资源,即使有资源被使用着
1,在关卡切换、场景切换
2,资源没被用的时候 调用
AssetBundle.Unload(false)卸载所有没用被使用的资源

个别资源怎么卸载

1,通过 Resources.UnloadUnusedAssets.
2,场景切换的时候

关于文件校验

CRC MD5 SHA1

相同点:
CRC、MD5、SHA1都是通过对数据进行计算,来生成一个校验值,该校验值用来校验数据的完整性。
不同点:
1. 算法不同。CRC采用多项式除法,MD5和SHA1使用的是替换、轮转等方法;
2. 校验值的长度不同。CRC校验位的长度跟其多项式有关系,一般为16位或32位;MD5是16个字节(128位);SHA1是20个字节(160位);
3. 校验值的称呼不同。CRC一般叫做CRC值;MD5和SHA1一般叫做哈希值(Hash)或散列值;
4. 安全性不同。这里的安全性是指检错的能力,即数据的错误能通过校验位检测出来。CRC的安全性跟多项式有很大关系,相对于MD5和SHA1要弱很多;MD5的安全性很高,不过大概在04年的时候被山东大学的王小云破解了;SHA1的安全性最高。
5. 效率不同,CRC的计算效率很高;MD5和SHA1比较慢。
6. 用途不同。CRC一般用作通信数据的校验;MD5和SHA1用于安全(Security)领域,比如文件校验、数字签名等。

结尾

1,依赖包重复问题
a,把需要共享的资源打包到一起
b,分割包,这些包不是在同一时间使用的
c,把共享部分打包成一个单独的包
2,图集重复问题
3,Android贴图问题
4,iOS文件处理重复fixed in Unity 5.3.2p2.

Unity Asset Bundle Browser tool

工具链接:http://pan.baidu.com/s/1slPrVaP 密码:zz80

直接放在Editor目录下即可,在unity编辑器导航栏Windows->AssetBundle Browser




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值