Xlua实现unity捕鱼达人热更新

一、Xlua与unity项目的配置

1、将xlua项目下的 D:\UnityProject\Xlua\XluaProjects\xLua-master\Assets  里面 所有配置文件 复制到  要热更新项目 下的 D:\UnityProject\Xlua\XluaProjects\FishingJoy\Assets目录下 

 

 

2、更改热更新项目的 宏定义

 

 

 设置成功效果:

 

 

 

 

3、解决 Xlua案例的问题

 

 

 

 

 

 

3.1第一步  更改任何xlua和C#代码 必须 重新生成 一下 代码

 

 

 

3.2、将生成的代码 注入到编译器里面

 

 

 

 

3.3、将Tools工具复制到 热更新项目  根目录下

 

 

 

3.4、解决注入警告问题   1.热更新项目文件名称不能包含中文名   2.库文件 复制到 D:\UnityProject\Xlua\XluaProjects\FishingJoyTest1.0\Assets\XLua\Src\Editor       

 

 

 

 

3.5、利用lua项目 示例   lua代码中运用 C#代码

 

 

 

复制代码
using UnityEngine;
using XLua;

[Hotfix]
public class HotfixTest : MonoBehaviour
{
    LuaEnv luaenv = new LuaEnv();

    public int tick = 0; //如果是private的,在lua设置xlua.private_accessible(CS.HotfixTest)后即可访问

    // Use this for initialization
    void Start()
    {
    }

    // 本来的方法    LuaCallCSharp这个标签是lua调用 c#代码
    [LuaCallCSharp]
    void Update()
    {
        if (++tick % 50 == 0)
        {
            Debug.Log(">>>>>>>>Update in C#, tick = " + tick);
        }
    }

    

    void OnGUI()
    {
        //用xlua更改的方法
        //CS.UnityEngine相当告诉 lua用的是C#代码

        if (GUI.Button(new Rect(10, 10, 300, 80), "Hotfix"))
        {
            luaenv.DoString(@"
                xlua.hotfix(CS.HotfixTest, 'Update', function(self)
                    local a = CS.UnityEngine.GameObject.Find('Main Camera')
                    CS.UnityEngine.Debug.Log(a.name)
                end)
            ");
        }

        

        //self.tick = self.tick + 1
        //if (self.tick % 50) == 0 then
        //    print('<<<<<<<<Update in lua, tick = ' .. self.tick)
        //end




        string chHint = @"在运行该示例之前,请细致阅读xLua文档,并执行以下步骤:

1.宏定义:添加 HOTFIX_ENABLE 到 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'。
(注意:各平台需要分别设置)

2.生成代码:执行 'XLua > Generate Code' 菜单,等待Unity编译完成。

3.注入:执行 'XLua > Hotfix Inject In Editor' 菜单。注入成功会打印 'hotfix inject finish!' 或者 'had injected!' 。";
        string enHint = @"Read documents carefully before you run this example, then follow the steps below:

1. Define: Add 'HOTFIX_ENABLE' to 'Edit > Project Settings > Player > Other Settings > Scripting Define Symbols'.
(Note: Each platform needs to set this respectively)

2.Generate Code: Execute menu 'XLua > Generate Code', wait for Unity's compilation.


3.Inject: Execute menu 'XLua > Hotfix Inject In Editor'.There should be 'hotfix inject finish!' or 'had injected!' print in the Console if the Injection is successful.";
        GUIStyle style = GUI.skin.textArea;
        style.normal.textColor = Color.red;
        style.fontSize = 16;
        GUI.TextArea(new Rect(10, 100, 500, 290), chHint, style);
        GUI.TextArea(new Rect(10, 400, 500, 290), enHint, style);
    }
}
复制代码

 

3.6、一个天坑就是  生成项目之前 必须把xlua的案例删除掉,要不然会报错

 

 

 

 

二、开发过程:

首先开发业务代码->

  在所有可能出现问题的类上打上    [Hotfix]      的标签,

  在所有lua调用CSharp的方法上打上  [LuaCallCSharp]  

  在所有CSharp调用Lua的方法上打上   [CSharpCallLua]  ->

打包发布->

修改bug时只需要更新Lua文件,修改资源时(声音,模型,贴图,图片,UI)只需要更新ab包。用户只需要去下载lua文件跟ab包。

 

 

 

三、实现热更新过程

Xlua官方问题 FAQ

https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/faq.md

1、新建lua脚本

 

 

1.1、fish.lua.txt

 

复制代码
--1.1 1.点击宝箱领取的金币钻石太拥挤,分散一点。

local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.Treasour,'CreatePrize',function(self)
    for i=0,4,1 do
        local go=UnityEngine.GameObject.Instantiate(self.gold,self.transform.position+UnityEngine.Vector3(-10+i*40,0,0),self.transform.rotation)
        go.transform.SetParent(go.transform,self.cavas)
        local go1=UnityEngine.GameObject.Instantiate(self.diamands,self.transform.position+UnityEngine.Vector3(0,40,0)+UnityEngine.Vector3(-10+ i*40,0, 0),self.transform.rotation)
        go1.transform.SetParent(go1.transform,self.cavas)

    end
end)
复制代码

 

1.2、fishDispose.lua.txt  必须将其 热更新过的函数  设置为空 不然会报错

xlua.hotfix(CS.Treasour,'CreatePrize',nil)

 

这个是为了解决下图问题

 

 

 

2、新建一个热更新脚本 ,将其挂在 主相机上

复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;

public class HotFixScript : MonoBehaviour {

    private LuaEnv luaEnv;

    
    void Start () {
        //创建 lua 虚拟环境
        luaEnv = new LuaEnv();
        luaEnv.AddLoader(MyLoader);

        luaEnv.DoString("require'fish'");


    }
    
    
    void Update () {
        
    }

    /// <summary>
    /// 读取路径的 文本
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns>一个文本</returns>
    private byte[] MyLoader(ref string filePath)
    {
        string adsPath = @"D:\UnityProject\Xlua\XluaProjects\TestBin\"+filePath+".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(adsPath));
    }

    /// <summary>
    /// 修复好bug后 相当于卸载掉热更新的内容
    /// </summary>
    private void OnDisable()
    {
        luaEnv.DoString("require'fishDispose'");
    }


    /// <summary>
    /// 销毁掉 lua虚拟环境
    /// </summary>
    private void OnDestroy()
    {
        luaEnv.Dispose();
    }


}
复制代码

 

posted @   Domefy  阅读(286)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示