Xlua之Bundle构建工具创建

  1. Bundle构建工具
    1. Budle Build策略
      1. 按文件夹打包
        1)优势:budle数量少,小包模式,首次下载快
        2)劣势:后期更新的时候,更新补丁大,即使一个budle中只有一个资源发生了改变,也要全部更新整个budle包
      2. 按文件打包(推荐)
        1)优势:更新补丁小
        2)劣势:小包模式:首次下载稍慢
    2. 由于打bundle 的功能是编辑器下面的功能,因此要放在Editor文件夹下面进
  2. Bundle 工具构建的流程
    1. 首先要明确的是,打包的数量为1-*个, 因此先创建一个存储bundle的数组或者列表List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
    2. 将你要打包的所有文件进行搜索并保存起来
      string[] files = Directory.GetFiles(PathUtil.BundleResourcesPath,"*", SearchOption.AllDirectories); 需要注意的是,使用这个方法去搜索的时候,会将一些meta文件也搜索出来,因此需要在下一步进行一下过滤
    3. 文件过滤
for(int i = 0; i < files.Length;i++)
        {

            //将meta文件进行过滤
            if(files[i].EndsWith(".meta"))
            {
                continue;
            }
   。。。
        }
4.  现在把所有需要打bundle包的文件都搜索出来了,下一步则是需要设置一下(AssetBundleBuild的assetName、assetBundleName两个属性)
//如果不是meta文件的话
           

            //由于添加的时候需要设置AssetBundleBuild的属性,如assetNames(unity中的相对目录)
            AssetBundleBuild assetBundleBuild = new AssetBundleBuild();


            //获取文件的相对目录
            string fileName = PathUtil.getStandardPath(files[i]);
            Debug.Log("file :" + fileName);
            string assetName = PathUtil.GetunityPath(fileName);
            assetBundleBuild.assetNames = new string[] { assetName};//绝对路径目的是找到要打包的文件
            
            string bundleName = files[i].Replace(PathUtil.BundleResourcesPath, "").ToLower();
            assetBundleBuild.assetBundleName = bundleName + ".ab";//相对路径,给打包出来的文件名命
            Debug.Log("assetbunldName: " + bundleName);

            assetBundleBuilds.Add(assetBundleBuild);
5. 判断输出路径是否为空,如果不为空就,删除,然后重建一个文件
 if (Directory.Exists(PathUtil.BundleOutPath))
            Directory.Delete(PathUtil.BundleOutPath, true);
        Directory.CreateDirectory(PathUtil.BundleOutPath);
6. 按照之前的命名设置,以及下面这条语句的输出路径设置,要打包的Assbundlebuild文件,压缩格式的设置,运行平台的设置进行打包
BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);
        //按照之前的命名设置,这条语句的输出路径,要打包的文件,压缩格式,运行的平台,进行bundle的打包
  1. 实用类PathUtil
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathUtil  {

    //这个类是一个实用类,用来管理各种路径的


    //根目录
    public static readonly string AssetsPath = Application.dataPath;


    //需要打Bundle的目录
    public static readonly string BundleResourcesPath = AssetsPath + "/BuildResources/";//获取的是所有要打包的文件


    //Bundle 的输出目录
    public static readonly string BundleOutPath = Application.streamingAssetsPath;

   /// <summary>
   /// 获取Unity的相对路径
   /// </summary>
   /// <param name="path"></param>
   /// <returns></returns>
   public static string GetunityPath(string path)
    {
        if (string.IsNullOrEmpty(path))
            return string.Empty;
        return path.Substring(path.IndexOf("Assets"));
    }

    public static string getStandardPath(string path)
    {
        if (string.IsNullOrEmpty(path))
            return string.Empty;
        return path.Trim().Replace("\\", "/");
    }

}

实现效果
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值