先在场景中挂载脚本,加载自定义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)