MCPcopy Index your code
hub / github.com/Tencent/xLua

github.com/Tencent/xLua @v2.1.16_ndkr21b

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.1.16_ndkr21b ↗ · + Follow
3,472 symbols 9,504 edges 143 files 502 documented · 14%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

license release PRs Welcome Build status

Lua programming solution for C

xLua adds Lua scripting capability to Unity, .Net, Mono, and other C# environments. With xLua, Lua code and C# code can easily call each other.

xLua's superior features

xLua has many breakthroughs in function, performance, and ease of use. The most significant features are:

  • You can inplace C# implementations (methods, operators, properties, events, etc...) by Lua's during runtime.
  • Outstanding GC optimization, customized struct, no C# gc alloc when passing the enumerated objects between C# and lua;
  • Lightweight development with no needs to generate code in editor mode;

Installation

Unpack the zip package and you will see an Assets directory, which corresponds to the Unity project's Assets directory. Keep the directory structure in your Unity project.

If you want to install it to another directory, please see the FAQs.

Documents

Quick Start

A complete example requires only 3 lines of code:

Install xLua, create a MonoBehaviour drag scenario, add the following code to Start:

XLua.LuaEnv luaenv = new XLua.LuaEnv();
luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')");
luaenv.Dispose();
  1. The DoString parameter is a string, and you can enter any allowable Lua code. In this example, Lua calls C#’s UnityEngine.Debug.Log to print a log.

  2. A LuaEnv instance corresponds to a Lua virtual machine. Due to overhead, it is recommended that the Lua virtual machine be globally unique.

It is simple that C# actively calls Lua. For example, the recommended method to call Lua's system function is:

  • Declare
[XLua.CSharpCallLua]
public delegate double LuaMax(double a, double b);
  • Bind
var max = luaenv.Global.GetInPath<LuaMax>("math.max");
  • Call
Debug.Log("max:" + max(32, 12));

It is recommended that you bind once and reuse it. If code is generated, no gc alloc is generated when calling max.

Hotfix

  • This has lower intrusiveness, and it can be used without any modification of the original code of the old project.
  • This has little impact on the runtime, which is almost the same as the original program which hotfix is not used.
  • If you have problems, you can also use Lua to patch. Then the Lua code logic is involved.

Here is the usage guide:

More Examples

  • 01_Helloworld: Quick Start Examples
  • 02_U3DScripting: This example shows how to use Mono to write MonoBehaviour.
  • 03_UIEvent: This example shows how to use Lua to write UI logic.
  • 04_LuaObjectOrented: This example shows the cooperation between Lua's object-oriented programming and C#.
  • 05_NoGc: This example shows how to avoid the value type GC.
  • 06_Coroutine: This example shows how Lua coroutines work with Unity coroutines.
  • 07_AsyncTest: This example shows how to use Lua coroutines to synchronize asynchronous logic.
  • 08_Hotfix: These are Hotfix examples (Please enable hotfix feature. See the Guide for details).
  • 09_GenericMethod: This is a generic function support demo.
  • 10_SignatureLoader: This example shows how to read the Lua script with a digital signature. See the Digital Signature document for details.
  • 11_RawObject: This example shows how to specify transferring a Lua number in the int after boxing when the C# parameter is an object.
  • 12_ReImplementInLua: This shows how to change complex value types to Lua implementations.

Technical support

QQ Group 1: 612705778 (may be full)

QQ Group 2: 703073338

QQ Group 3: 811246782

Check answers: If you encounter a problem, please read the FAQs first.

Extension points exported contracts — how you extend this code

IExchanger (Interface)
(no doc)
Assets/XLua/Examples/05_NoGc/NoGc.cs
ITableAccess (Interface)
(no doc)
Test/PrefTest/xLuaPerfTest/PerfMain.cs
ITestLuaClass (Interface)
(no doc)
Test/UnitTest/xLuaTest/LuaTestObj.cs
ICalc (Interface)
(no doc)
Assets/XLua/Examples/04_LuaObjectOrented/InvokeLua.cs
ITest (Interface)
(no doc)
Test/UnitTest/xLuaTest/LuaTestObj.cs
RawObject (Interface)
(no doc)
Assets/XLua/Src/RawObject.cs
IGenCodeTest (Interface)
(no doc)
Test/UnitTest/xLuaTest/LuaTestObj.cs
ItfD (Interface)
(no doc)
Assets/XLua/Tutorial/CSharpCallLua/CSCallLua.cs

Core symbols most depended-on inside this repo

push
called by 227
Assets/XLua/Src/LuaBase.cs
Add
called by 134
Assets/XLua/Src/ObjectPool.cs
isDef
called by 125
docs/public/js/vue.js
isDef
called by 125
docs/source/themes/catlib/source/js/vue.js
lua_pushvalue
called by 118
WebGLPlugins/lapi.c
luaL_error
called by 99
Assets/XLua/Src/LuaDLL.cs
luaL_error
called by 95
WebGLPlugins/lauxlib.c
lua_gettop
called by 86
Assets/XLua/Src/LuaDLL.cs

Shape

Function 2,081
Method 1,027
Class 323
Enum 25
Interface 16

Languages

C#40%
C31%
TypeScript27%
C++2%

Modules by API surface

docs/source/themes/catlib/source/js/vue.js386 symbols
docs/public/js/vue.js386 symbols
Test/UnitTest/xLuaTest/LuaTestObj.cs179 symbols
Assets/XLua/Src/LuaDLL.cs136 symbols
Test/UnitTest/xLuaTest/LuaTestObjReflect.cs112 symbols
Test/UnitTest/xLuaTest/CSharpCallLua/TCForTestCSCallLua.cs103 symbols
WebGLPlugins/xlua.c92 symbols
WebGLPlugins/lparser.c91 symbols
docs/source/themes/catlib/source/js/jquery.js86 symbols
docs/public/js/jquery.js86 symbols
WebGLPlugins/lapi.c86 symbols
Assets/XLua/Src/ObjectTranslator.cs73 symbols

For agents

$ claude mcp add xLua \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact