当前位置 : 主页 > 网络编程 > net编程 >

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

来源:互联网 收集:自由互联 发布时间:2023-09-06
先在场景中挂载脚本,加载自定义Loader using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; using System.IO; using UnityEngine.Networking; public class HotFixScript : MonoBehaviour { privat


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

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]

例如

[Hotfix]
public class Test1: MonoBehaviour
{

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

lua代码如下:

local UnityEngine = CS.UnityEngine
xlua.hotfix(CS.Test1,'OnTest',function(self)
-- body
UnityEngine.Debug.Log("Lua")
end
end)


上一篇:如何通过C#将 Excel 转为PDF
下一篇:没有了
网友评论