1,安装Xlua
https://github.com/Tencent/xLua 下载zip包,解压,把底下Assets下的文件塞入Unity的工程里。Tools复制到Assets的上一级目录
在这里插入图片描述
2,添加脚本编译,HOTFIX_ENABLE
增加后点击Generate Code
特殊,Vscodelua乱码:
C#调用Lua文件
简介:调用lua文件,首先需要一个HotFixScript,就是一个负责调用lua的脚本代码。
核心代码:定义一个虚拟机,private LuaEnv luaEnv;并且new,实例化。
第二步,使用luaEnv.AddLoader(_CustomLoader);内容是一个byte[];二进制文件,把lua文件加载进去。
加载lua文件,加载为Text:System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
第三步,加载Lua文件,,LuaEnv.DoString(“require‘Test’”);
第四步,销毁Mono对象的时候,一定要把LuaEnv给Dispose掉。
using XLua;
using System;
using System.IO;
public class HotFixScript : MonoBehaviour
{
**private LuaEnv luaEnv;//虚拟机**
// Start is called before the first frame update
void Start()
{
luaEnv=new LuaEnv();
// public delegate byte[] CustomLoader(ref string filepath);
// public void LuaEnv.AddLoader(CustomLoader loader);这样都行,就是自定义加载器
luaEnv.AddLoader(MyLoader); //运行添加自定义加载器,反正返回一个byte[]就行了,里边改成委托都ok
luaEnv.DoString("require'Test'");//这是一个方法,许您在xLua中执行一个字符串。里边的require,是加载一个lua模块,通常是指另外一个lua文件
}
private byte[] MyLoader(ref string _FilePath){
string abspath=@"F:\UnityProgram\XLUAHOTFIXProject\Luas\"+_FilePath+".lua.txt";
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(abspath));
}
private void OnDestroy() {
luaEnv.Dispose();//记得关闭虚拟机实例
}
}
lua文件如图:
**Test.lua.txt**
print("你好!")
local m= require'Test2'
m.hello2()
**Test2.lua.txt**
print("我是luaTest2")
local M={} --表
function M.hello()
print("Hello, World!我是Hello的脚本")
end
function M.hello2()
print("Hello, World!我是Hello2的脚本")
end
return M
Lua热补丁功能,lua修改C#内的功能
第一步:引入XLua,加上HotFix特性,添加完要GenerateCode,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[Hotfix]
public class CubeController : MonoBehaviour
{
private new Rigidbody rigidbody;
// Start is called before the first frame update
void Start()
{
rigidbody=GetComponent<Rigidbody>();
}
[LuaCallCSharp]
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
rigidbody.AddForce(Vector3.up*50);
}
}
}
在这里插入代码片
HotFixScript脚本:
using UnityEngine;
using XLua;
using System;
using System.IO;
public class HotFixScript : MonoBehaviour
{
private LuaEnv luaEnv;//虚拟机
// Start is called before the first frame update
void Start()
{
luaEnv=new LuaEnv();
// public delegate byte[] CustomLoader(ref string filepath);
// public void LuaEnv.AddLoader(CustomLoader loader);这样都行
luaEnv.AddLoader(MyLoader); //运行添加自定义加载器,反正返回一个byte[]就行了,里边改成委托都ok
luaEnv.DoString("require'CubeController'");//这是一个方法,许您在xLua中执行一个字符串。里边的require,是加载一个lua模块,通常是指另外一个lua文件
}
private byte[] MyLoader(ref string _FilePath){
string abspath=@"F:\UnityProgram\XLUAHOTFIXProject\Luas\"+_FilePath+".lua.txt";
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(abspath));
}
private void OnDisable() {
luaEnv.DoString("require'LuaDispose'");//lua里引用了函数要关掉才行
}
private void OnDestroy() {
luaEnv.Dispose();//记得关闭虚拟机实例
}
}
接下来开始写lua文件
ControllerLua文件:
print("Lua文件")
local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.CubeController,'Update',function (self) --这里调用了函数,一一定要nil掉才行。
if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.S)) then
self.rigidbody:AddForce(UnityEngine.Vector3.up*500);
end
end)
OnDisable文件:
xlua.hotfix(CS.CubeController,'Update',nil) --把之前引用的函数给注释掉