xlua uses

Start preparing
Download address: https://github.com/Tencent/xLua/tree/v2.1.16
After downloading, import the XLua and Plugins inside into your own project. The Tools directory is also put into your own project and add the macro definition HOTFIX_ENABLE,
for example, add the macro definition in Unity2020.3.33

Configure the generated Warp class

(1) Labeling, for example, lua needs to call a certain class of C#, then add the label [LuaCallCSharp] to this class, so that the Warp class will be automatically generated when Generate Code (2)
Static list:
Sometimes it is impossible to package the package Add tags to some classes. At this time, you can declare a static field in a static class. In addition to BlackList and AdditionalProperties, this field only needs to implement IEnumerable. Then add a label to this field
[LuaCallCSharp]
public static List my_list = new List(){ typeof(GameObject), } This field needs to be placed in a static class, and created in the Editor directory (3) The dynamic list is created through attributes Dynamically configure the List, just put the corresponding label, and also put it in the static




Label

LuaCallCSharp


If a C# adds this configuration, XLua will generate this type of adaptation code, otherwise it will try to access a type of extension method with poor performance reflection. Adding this configuration will also generate adaptation code and add the extension type On the member method,
xlua will only generate the type with this configuration, and will not automatically generate the adaptation code of its parent class. When accessing the parent class method of the subclass object, if the parent class has the LuaCallCSharp tag added, the parent class will be executed. Adapt the code, otherwise try to use reflective access
In addition to poor performance, reflective access may also be inaccessible due to code tailoring under il2cpp

ReflectionUse

A C# type is added with this tag, and XLua will generate link.xml to prevent il2cpp from clipping the code

CSharpCallLua

Adapt a lua function to a C# delegate

GCOptimize

A C# pure value type

BlackList

If you don't want to generate some member adaptation codes of a type, you can use this configuration to achieve

The following is the generation configuration, which must be placed in the Editor directory

CSObjectWrapEditor.GenPath

Configure the placement path of the generated code, the type is string. It is placed under "Assets/XLua/Gen/" by default.

CSObjectWrapEditor.GenCodeMenu

This configuration is used for the secondary development of the generation engine. A parameterless function is added with this tag, and the call of this function will be triggered when the "XLua/Generate Code" menu is executed.

simple process

Create a lua virtual machine: LuaEnv luaEnv = new LuaEnv();
Load the loader: luaEnv.AddLoader(CustomMyLoader);
The loader is used with require, you can customize a loader
private byte[] CustomMyLoader(ref string fileName)
{ string luaPath = Application.dataPath + “/LuaScripts/” + fileName + “.lua.txt”; string strLuaContent = File.ReadAllText(luaPath); byte[] result = System.Text.Encoding.UTF8.GetBytes(strLuaContent); return result; } Load Lua file: luaEnv.DoFile(); luaEnv.DoString();





hot reload

First, add the feature Hotfix to the script class A that needs to be hot-reloaded. Hotfix
needs to overload the method in class A in lua. Start
and execute
xlua.private_accessible(CS.A)
xlua.hotfix(CS.A,"start in lua ", function(self)
print("Overloaded class" ... self.name);
end)

C# calls lua

C# calls the method test in a.lua
Declaration in C#
[XLua.CSharpCallLua]
public delegate void Test(float a)
binding
var test = luaEnv.Global.GetInPath("a.test")
calls
test();

Lua calls C#

By warp class, CS.xxx.xxx

Guess you like

Origin blog.csdn.net/weixin_44806700/article/details/124386700