Skip to main content

Design overview

:::tip Who should read this Evaluators, new adopters, and readers who need to understand “why it’s designed this way.” For day-to-day APIs see Guides; implementation details are in the Spec. :::

ZLua treats Lua as another form of Native: like P/Invoke, declarative APIs unify bidirectional interop; on Il2Cpp it generates C++ stubs (ZLua/Generate/All), not xLua-style C# Wrap.

P/Invoke vs ZLua

C# interopRoleZLua counterpart
P/InvokeC# calls native functionsGetFunction<T> — C# calls Lua
MonoPInvokeCallbacknative callbacks into C#[MonoLuaCallback] — only int (IntPtr L) native callbacks
MarshalAsOverride default Marshal[LuaMarshalAs] — C# ↔ Lua Marshal override

Core principles

PrincipleNotes
Unified bidirectional callsC#→Lua: GetFunction<T>; Lua→C#: lazy CSharp registration, syntax close to C#
Auto-gen (Lua→C#)Editor Emit / Il2Cpp Generate C++ stubs; C#→Lua has no per-call codegen
Deep integrationOne LuaAppDomain.Initialize sets up CLR + lua_State + zlua lib; clear after hot reload via Reset
C++ direct bridgePlayer reads field offsets directly, methods via methodPointer — no huge C# Wrap
No wrapper bloatSame signatures share bridge functions instead of one Wrap per member

Auto-gen pipeline (Lua→C#)

StageMono (Editor)Il2Cpp (Player)
C#→LuaGetFunction + Delegate bridgeSame (native path)
Lua→C# membersFirst access EnsureBinding + EmitEnsureBinding + C++ stubs (Generate)
Developer experienceNo C# WrapNo C# Wrap; must Generate stubs

Path differences vs xLua (summary)

DimensionTypical xLua pathZLua
Type exposureGenerate C# Wrap / CodeEmitCSharp root table + three metatable tables
C#→LuaLuaEnv.DoString / DelegateBridgeGetFunction<T> + Invoke
Player performanceWrap + many LuaDLL callsC++ direct bridge + signature reuse (see PERFORMANCE)

See Comparison, Il2Cpp impl.

Which doc to read when

Your questionRead
How does C# call Lua?C# calling Lua Guide
How does Lua access C# types?Lua calling C#
How are parameters passed?Marshal model overview
Editor vs Player differences?Dual runtime
Full design semantics?Design Spec