unity3d:xlua hotfix 热补丁修改c#脚本bug

2023-08-24 14:20:31 浏览数 (1)

先在场景中挂载脚本,加载自定义Loader

代码语言:javascript复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
using UnityEngine.Networking;

public class HotFixScript : MonoBehaviour {

    private LuaEnv luaEnv;

    private void Awake()
    {
        luaEnv = new LuaEnv();
        luaEnv.AddLoader(MyLoader);
        luaEnv.DoString("require 'xxx'");
    }

    // Use this for initialization
    void Start () {
     
	}
	
	// Update is called once per frame
	void Update () {
		
	}


    private byte[] MyLoader(ref string filePath)
    {
        string absPath = @"xxx"   filePath ".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
    }

    private void OnDisable()
    {
        luaEnv.DoString("require 'xxxDispose'");
    }

    private void OnDestroy()
    {
        luaEnv.Dispose();
    }

要修复的C#脚本类上打上 [Hotfix] 这个类要修复的函数打上 [LuaCallCSharp]

例如

代码语言:javascript复制
[Hotfix]
public class Test1: MonoBehaviour
{

[LuaCallCSharp]
    private void OnTest()
    {
    Debug.Log("C#");
    }
}

lua代码如下:

代码语言:javascript复制
local UnityEngine = CS.UnityEngine
xlua.hotfix(CS.Test1,'OnTest',function(self)
	-- body
		UnityEngine.Debug.Log("Lua")
	end
end)

0 人点赞