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
    评论
Unity,AssetBundle是一个用于异步加载资源(如音频、纹理、模型等)的数据包,它可以让你在游戏一次性下载多个资源,并在需要的时候按需解压和加载。如果你想要查找已经加载或未加载的AssetBundle,你可以按照以下步骤操作: 1. 使用`AssetDatabase.LoadAssetAsync<T>(string assetPath)`方法:这个方法会异步加载指定路径下的AssetBundle内容,并返回一个Task,可以通过await获取结果。例如: ```csharp using UnityEngine; using System.IO; using System.Threading.Tasks; public class AssetLoader : MonoBehaviour { async void LoadAssetBundle(string bundlePath) { string assetName = "YourAssetName"; Task<Asset> assetTask = AssetDatabase.LoadAssetAsync<Asset>(bundlePath + "/" + assetName); if (assetTask.IsCompleted) { Asset loadedAsset = await assetTask; // 处理已加载资产 } else { // 仍在加载 } } } ``` 这里的`YourAssetName`需要替换为AssetBundle内的具体资源名称。 2. 使用`AssetManager.UnloadAllAssetBundles()`:在不需要AssetBundle时,可以将其卸载,释放内存。但要注意,这会影响当前进程内的所有AssetBundle,不是精确的查找功能。 3. `AssetDatabase.Bundles`:这是一个静态列表,包含了所有已加载的AssetBundle。你可以遍历这个列表来检查哪些AssetBundle是可用的: ```csharp foreach (var bundle in AssetDatabase.Bundles) { Debug.Log($"AssetBundle: {bundle.name}"); } ``` 如果你想查找特定AssetBundle,可以在循环添加条件判断
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值