【Unity&XLua】热更新框架 | Bundle构建

该文章讲述了在游戏开发中如何使用C#调用Lua进行框架开发,包括Bundle的处理流程(构建、加载、更新),Lua脚本的加载与管理,以及C#向Lua提供接口进行资源和事件管理。同时,文中提到了资源目录的合理划分,如Lua、UI、模型等,并提供了具体的代码示例展示如何构建AssetBundles。
摘要由CSDN通过智能技术生成

框架开发

  1. Bundle处理(构建-加载-更新)
  2. C#调用Lua(Lua加载与管理、Lua绑定与执行)
  3. 向Lua提供接口
  4. 完善和优化

资源目录划分

将Assets根据是否需要打bundle分类

1:Lua,UI,模型,特效,声音,动画
0:
  xlua
  c#:
    bundle构建工具等工具
    与Lua交互:
        c#调用Lua(加载Lua脚本,执行Lua逻辑)
      Lua调用c#(c#给Lua提供接口用于资源加载、资源管理、事件管理、场景模型加载等)`

Bundle构建

xLua下载链接:xLua
解压后Assets下的文件导入Unity
在这里插入图片描述
策略:对每个单独文件打包bundle
使用Unity提供的BuildPipeline构建

//BuildPipeline(输出路径,数组列表,压缩格式,bundle目标平台)
        BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);

查找BuildResources下的资源文件

//获取所有文件策略 GetFiles(路径,搜索的匹配字符串,搜索选项:全部文件夹)
        string[] files = Directory.GetFiles(PathUtil.BuildResourcesPath, "*", SearchOption.AllDirectories);

生成标签:

[MenuItem("Tools/Build Windows Bundle")]
    static void BundleWindowsBuild()
    {
        Build(BuildTarget.StandaloneWindows);
    }

在这里插入图片描述
放入UI
在这里插入图片描述
运行Build Windows Bundle
结果:路径斜杠混乱

在这里插入图片描述
对路径规范处理:

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

在这里插入图片描述

Code

PathUtil.cs

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

public class PathUtil
{
    //根目录
    public static readonly string AseetsPath = Application.dataPath;
    //需要打bundle的目录
    public static readonly string BuildResourcesPath = Application.dataPath + "/BuildResources";
    //bundle输出目录
    public static readonly string BundleOutPath = Application.streamingAssetsPath;
    
    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("\\", "/");//替换
    }
}

BuildTool.cs

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

public class BuildTool : Editor
{
    [MenuItem("Tools/Build Windows Bundle")]
    static void BundleWindowsBuild()
    {
        Build(BuildTarget.StandaloneWindows);
    }
    [MenuItem("Tools/Build Android Bundle")]
    static void BundleAndroidBuild()
    {
        Build(BuildTarget.Android);
    }
    [MenuItem("Tools/Build iPhone Bundle")]
    static void BundleiPhoneBuild()
    {
        Build(BuildTarget.iOS);
    }
    static void Build(BuildTarget target)
    {
        List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
        
        //获取所有文件策略 GetFiles(路径,搜索的匹配字符串,搜索选项:全部文件夹)
        string[] files = Directory.GetFiles(PathUtil.BuildResourcesPath, "*", SearchOption.AllDirectories);
        //排除.meta文件
        for(int i = 0; i < files.Length; i++)
        {
            if (files[i].EndsWith(".meta"))
                continue;
            
            AssetBundleBuild assetBundle = new AssetBundleBuild();
            
            string fileName = PathUtil.GetStandardPath(files[i]);
            Debug.Log("file:" + fileName);

            string assetName = PathUtil.GetUnityPath(fileName);
            assetBundle.assetNames=new string[]{ assetName};
            string bundleName = fileName.Replace(PathUtil.BuildResourcesPath, "").ToLower();
            assetBundle.assetBundleName = bundleName + ".ab";
            assetBundleBuilds.Add(assetBundle);
        }

        //判断输出目录是否存在,若里面由文件则删除再创建
        if (Directory.Exists(PathUtil.BundleOutPath))
            Directory.Delete(PathUtil.BundleOutPath, true);
        Directory.CreateDirectory(PathUtil.BundleOutPath);
        
        //BuildPipeline(输出路径,数组列表,压缩格式,bundle目标平台)
        BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值