AssetBundle资源打包与加载

AssetBundle资源打包

 1.AssetLabels资源标签

  文件名:资源打包成AssetBundle后的文件名,类似于压缩包的名字
  后缀:自定义
  文件名和后缀名都是小写格式(大写会自动转为小写)
2. BuildPipeline.BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform) 打包所有设置了AssetLabel的资源
  outputPath:路径,打包出来的AssetBunlde文件存放的位置
  BuildAssetBundleOptions:选项,设置AssetBundle打包过程中压缩方式

    BuildAssetBundleOptions枚举选项:

      None使用LZMA压缩算法进行压缩,打包后资源体积最小
      UncompressedAssetBundle不压缩,打包后的AssetBundle体积最大,但是加载速度最快
      ChunkBasedCompression使用LZ4压缩算法进行压缩,打包后的AssetBundle体积和加载速度介于上面二者之间
  BuildTarget:平台,AssetBundle是平台之间不兼容的,IOS,Android是两套资源

  
在AssetLabels区域填写AssetBundle名称的时候,名称是可以分目录嵌套的:文件夹名/文件名

AssetBundle打包后的资源包,分两部分组成:

  1.资源打包出来的AssetBundle文件
  2.AssetBundle文件配套的manifest文本文件

manifest文件

manifest文件用于专门存储打包后的AssetBundle文件的基本信息,主要包含:
  CRC校验码:类似于MD5,用于计算出该资源的一个特殊信息标示
  ClassTypes列表:当前资源关联使用到了Unity中的哪些类,这些类是以编号索引的形式存在的,每个编号都对应一个类文件
  Assets:AssetsBundle里包含了哪些资源文件
  Dependencies:依赖

 在打包出来的AssetBundle文件中,有一个特殊的manifest文件,和AssetBundle存放的文件夹同名,且只在根文件夹下有唯一的一个

这个manifest文件可以称作"AssetBundle目录文件",它存储了打包出来的所有AssetBundle的文件的索引信息
通过这个目录文件,可以找到所有的AssetBundle文件

AssetBundle资源加载

将项目资源打包成AssetBundle后,一般有两种操作

一.将这些AssetBundle留着项目工程中,当成普通资源使用

  1.加载AssetBundle资源到内存

    AssetBundle ab =  AssetBundle.LoadFromFile("路径");加载AssetBundle资源到内存,返回一个AssetBundle对象

        2.从AssetBundle中获取资源

    T res = ab.LoadAsset<T>("资源名");

   3.实例化

打包

[MenuItem("xx/xxx")] 
public static void Build_WINDOWS64()
{
  string outputPath = Path.Combine(Application.streamingAssetsPath, "Data");
  if (!Directory.Exists(outputPath))
    Directory.CreateDirectory(outputPath);

  BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);//自己选择平台
  AssetDatabase.Refresh();
}

加载

using System.Collections;
using System.Collections.Generic;

public class AssetBundleManager
{
    private readonly string _path = UnityEngine.Application.streamingAssetsPath + "/Data/";
    private static AssetBundleManager _instance;
    private bool mIsLoadingEnd = false;
    public bool IsLoadingEnd { get { return mIsLoadingEnd; } }
    private List<string> mLoadingNames;
    private Dictionary<string, UnityEngine.Object> mAssets;
    private Dictionary<string, UnityEngine.Texture2D> mTexture2Ds;

    public static AssetBundleManager GetInstance()
    {
        if (_instance == null)
        {
            _instance = new AssetBundleManager();
        }
        return _instance;
    }
    
    private AssetBundleManager()
    {
        mLoadingNames = new List<string>(){
            //todo:要加载的资源名
        };

        mAssets = new Dictionary<string, UnityEngine.Object>();
        mTexture2Ds = new Dictionary<string, UnityEngine.Texture2D>();
    }
    
    /// <summary>
    /// 加载assetsbundle
   /// 记得先加载到内存StartCoroutine(AssetBundleManager.GetInstance().Loading());
/// </summary> public IEnumerator Loading() { foreach (var nameAndType in mLoadingNames) { string filename = _path + nameAndType.ToLower(); UnityEngine.AssetBundleCreateRequest request = UnityEngine.AssetBundle.LoadFromFileAsync(filename); yield return request; LoadAsset(request.assetBundle); } mIsLoadingEnd = true; } private UnityEngine.AssetBundle LoadAssetBundle(string fileName) { UnityEngine.AssetBundle AB = UnityEngine.AssetBundle.LoadFromFile(_path + fileName); return AB; } private void LoadAsset(UnityEngine.AssetBundle ab) { UnityEngine.Object[] assets = ab.LoadAllAssets(); for (int i = 0; i < assets.Length; i++) { //一张图片的AssetsBundle包含自身Texture2D和它里面全部Sprite,有可能重名 if (!typeof(UnityEngine.Texture2D).Equals(assets[i].GetType())) mAssets.Add(assets[i].name, assets[i]); else mTexture2Ds.Add(assets[i].name, assets[i] as UnityEngine.Texture2D); } } /// <summary> /// 从assetbundle获取texture2d /// </summary> public UnityEngine.Texture2D GetTexture2D(string name) { UnityEngine.Texture2D obj; mTexture2Ds.TryGetValue(name, out obj); return obj; } /// <summary> /// 从assetbundle获取Object /// </summary> public UnityEngine.Object GetAsset(string name) { UnityEngine.Object obj; mAssets.TryGetValue(name, out obj); return obj; } }

二.将这些AssetBundle上传服务器,客户端第一次运行时从服务器下载AssetBundle缓存到本地再使用(实现客户端的安装包与资源分离,降低客户端安装包的体积)

 1.服务器端下载主文件

    从服务器端下载AssetBundle需要web路径地址,每一个地址对应一个AssetBundle文件.我们不可能在代码中写几十上百个文件的web地址,所以我们需要先下载"目录AssetBundle文件",然后通过它来间接获取其他AssetBundle文件的下载路径地址.

需要引入命名空间using UnityEngine.Networking;

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

public class TestAssetBundle : MonoBehaviour
{
    private string mainAssetBundleURL = @"http://www.xxx.com.xxx/AssetBundleFile";
    private string allAssetBundleURL =  @"http://www.xxx.com.xxx/";

    void Start()
    {
        StartCoroutine(DownloadMainAssetBundle());
    }

    /// <summary>
    /// 下载目录AssetBundle文件
    /// </summary>
    IEnumerator DownloadMainAssetBundle()
    {
        //创建一个获取AssetBundle文件的web请求
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mainAssetBundleURL);

        //发送web请求
        yield return request.SendWebRequest();

        //从请求中获取内容,返回AssetBundle类型的数据
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

        //从"目录AssetBundle"中获取manifest数据
        AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //获取manifest文件中所有的AssetBundle的名称信息
        string[] names = manifest.GetAllAssetBundles();

        foreach (var name in names)
        {
            StartCoroutine(DownloadAssetBundleAndSave(allAssetBundleURL + name));
        }
    }
    
    /// <summary>
    /// 下载AssetBundle并保存到本地
    /// </summary>
    IEnumerator DownloadAssetBundleAndSave(string url)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        yield return request.SendWebRequest();
        // 截取路径地址中的文件名(需要引入System.IO)
        string fileName = Path.GetFileName(url);
        SaveAssetBundle(fileName, request.downloadHandler.data);
    }

    /// <summary>
    /// 存储AssetBundle为本地文件
    /// </summary>
    private void SaveAssetBundle(string fileName, byte[] bytes)
    {
        //创建一个文件信息对象(System.IO)
        FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + "//" + fileName);

        //创建一个文件流对象
        FileStream fs = fileInfo.Create();

        //通过文件流对象,往文件内写入信息
        fs.Write(bytes, 0, bytes.Length);

        //文件写入存储到硬盘
        fs.Flush();

        //关闭文件流对象
        fs.Close();

        //销毁文件对象
        fs.Dispose();
    }
}

猜你喜欢

转载自www.cnblogs.com/xianguoguo/p/11563893.html

相关文章