Unity中的AssetBundle(简单了解)

1.加载AB包的流程

  1. 指定资源的Asset Bundle属性
  2. 构建Asset Bundle包
  3. 上传AB包
  4. 加载AB包和包里的资源

2.指定资源的AssetBundle属性

在这里插入图片描述


3.构建Asset Bundle包
在这里插入图片描述


4. 加载AB包和包里的资源(加载本地的AB包)
在这里插入图片描述


5.AB包分组策略
在这里插入图片描述


6.解压方式
在这里插入图片描述


7.依赖包

在这里插入图片描述


8.加载AB包的几种方式

  1. 从内存中异步加载AB包(LoadFromMemoryAsync)
 ///异步从内存中加载AB
    IEnumerator LoadFromMemoryAsync()
    {
        //从内存中异步加载Ab
       AssetBundleCreateRequest request= AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundle/wall.unity3d"));
        yield return request;//等待加载完毕
        //获取AB包
        AssetBundle ab = request.assetBundle;
        //加载AB包中的资源
        GameObject obj= ab.LoadAsset<GameObject>("Cube");
        //实例化AB包中的资源
        Instantiate(obj);
    }

2.从本地异步加载AB包(LoadFromFileAsync)

/// <summary>
    /// 异步从本地加载AB
    /// </summary>
    /// <returns></returns>
    private IEnumerator LoadFromFileAsync()
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync("AssetBundle/wall.unity3d");
        yield return request;//等待加载完毕
        //获取AB包
        AssetBundle ab = request.assetBundle;
        //加载AB包中的资源
        GameObject obj = ab.LoadAsset<GameObject>("Cube");
        //实例化AB包中的资源
        Instantiate(obj);
    }

3.使用WWW类加载(LoadFromCacheOrDownload)

 /// <summary>
    /// www类加载AB包(被弃用)
    /// </summary>
    /// <returns></returns>
    private IEnumerator LoadFromCacheOrDownload()
    {
        while (Caching.ready==false)//判断是否可以缓存
        {
            yield return null;
        }
       //开始本地加载
        // WWW www = WWW.LoadFromCacheOrDownload(@"file:///G:\UnityProjects\Unity2018.2.0Projects\AssetBundle\AssetBundle\wall.unity3d",1);
        //开始从服务器上加载
        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundle/AssetBundle/wall.unity3d",1);
        yield return www;//等待加载完毕
        if (string.IsNullOrEmpty(www.error)==false)//判断是否有错误
        {
            Debug.Log(www.error);
            yield break;
        }
        ///使用资源
        AssetBundle ab = www.assetBundle;
       Instantiate( ab.LoadAsset<GameObject>("Cube"));
    }

4.使用UnityWebRequest加载

/// <summary>
    /// 使用UnityWebRequest加载
    /// </summary>
    /// <returns></returns>
    private IEnumerator UnityWebRequestAB()
    {
        //从本地加载AB包
        //  UnityWebRequest request= UnityWebRequestAssetBundle.GetAssetBundle(@"file:///G:\UnityProjects\Unity2018.2.0Projects\AssetBundle\AssetBundle\wall.unity3d");
        //从服务器端加载AB包
        UnityWebRequest request= UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundle/wall.unity3d");

        yield return request.SendWebRequest();//发送Web请求
        //第一种得到AssetBundle对象
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

        //第二种得到AssetBundle对象
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        //实例化
        Instantiate(ab.LoadAsset<GameObject>("Cube"));
    }

9.通过Manifest加载某个包的依赖包

  /// <summary>
    /// 加载依赖包
    /// </summary>
    private void LoadDependencies()
    {
        AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundle/AssetBundle");//加载AssetBundle的AB包
        //加载AssetBundle对应的Manifest
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //得到所有的AB包
        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    print(name);
        //}
        //得到所有名字为wall.unity3d的依赖包
        string[] str = manifest.GetAllDependencies("wall.unity3d");
       
        foreach (string name in str)
        {           
            print(name);
            //加载依赖包
            AssetBundle.LoadFromFile("AssetBundle/"+name);
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值