xLua README

The project's README from github.com/Tencent/xLua. As one markdown file: README.md.

license release PRs Welcome Build status

(English Documents Available)

C# 下 Lua 编程支持

xLua 为 Unity、.Net、Mono 等 C# 环境增加 Lua 脚本编程的能力,借助 xLua,这些 Lua 代码可以方便的和 C# 相互调用。


xLua 的突破

xLua 在功能、性能、易用性都有不少突破,这几方面分别最具代表性的是:

更详细的特性、平台支持介绍请参考 xLua 文档: 功能特性。


安装

xLua 可以直接简单的安装在 Unity 项目中.

  1. 从 Releases 中下载发行版, 或直接下载本仓库代码.
  2. 打开下载下来的源码压缩包, 你会看到一个 Assets 目录, 这目录就对应 Unity 工程的 Assets 目录,保持这目录结构, 将其内容置入 Unity 项目即可.

注意, Assets/Examples 目录下为示例代码, 你应该在生产环境下删去他们.

如果希望安装到其它目录,请看 FAQ 相关介绍。


文档


快速入门

一个完整的例子仅需3行代码:

安装好xLua,建一个MonoBehaviour拖到场景,在Start加入如下代码:

XLua.LuaEnv luaEnv = new XLua.LuaEnv();
luaEnv.DoString("CS.UnityEngine.Debug.Log('hello world')");
luaEnv.Dispose();
  1. DoString 参数为 string,可输入任意合法的 Lua 代码,本示例在 Lua 里调用 C# 的 UnityEngine.Debug.Log 打印了个日志。
  2. 一个 LuaEnv 实例对应 Lua 虚拟机,出于开销的考虑,建议全局唯一。

C#主动调用 Lua 也很简单,比如要调用 Lua 的系统函数,推荐方式是:

注意, 请不要重复调用 luaEnv.Global.GetInPath<LuaMax>, 这没有任何必要.


热补丁

除了使用 Lua 在 Unity 进行脚本编写, 你也可以使用 Lua 实现 "热补丁". xLua 提供了使用 Lua 逻辑替换 C# 方法逻辑的方案.

参考使用指南: xLua 文档: 热补丁


更多示例


技术支持

一群:612705778 (已满)

二群:703073338 (已满)

三群:811246782

入群的问题:有问题该先从哪找答案

回答:FAQ

平时也要谨记这答案,90%以上问题都可以在FAQ里头找到答案。这些问题就别在群刷屏了。

xLua (Tencent/xLua)

Searching Database…

10.2K ·GitHub
What is this?xLua adds Lua scripting to C# environments — Unity, .Net and Mono — so Lua code and C# can call each other easily, typically to script Unity games.
Where does the Lua actually run — does xLua bundle its own interpreter?Yes. Each LuaEnv instance you create corresponds to a Lua virtual machine, and the README recommends keeping exactly one instance, globally unique, to limit overhead.
How do I add it to my Unity project?Download a build from GitHub Releases (or grab the repo code), then drop the contents of its Assets directory into your project's Assets folder, keeping the structure. Remove Assets/Examples before shipping to production.
Do I need anything else installed alongside it?No — it installs as plain source inside your Unity project with no external steps. If you later want third-party Lua libraries, there is a dedicated guide for adding or removing them.
Is the hot-fix feature ready to use out of the box?No — it has to be enabled first; the 08Hotfix example notes you should follow the hot-fix guide in the docs to switch it on.
Will it work in mobile builds, or only desktop?Mobile is first-class: the project supports android and ios alongside windows, linux and osx.
What can I actually do with the Lua side?The bundled examples show writing MonoBehaviours and UI logic in Lua, mixing Lua OOP with C#, running Lua coroutines alongside Unity's (even turning async code synchronous), calling generic methods, loading digitally signed scripts, and re-implementing C# value types in Lua.
How much code for a hello world?Three lines: create a LuaEnv and call DoString with any valid Lua code — the quickstart logs via C#'s UnityEngine.Debug.Log from inside Lua. C# can call back into Lua too, for example grabbing a Lua function from the global table.
What is the hot-fix feature good for?It replaces C# methods, operators, properties and events with Lua implementations at runtime. Old projects need no code changes, and until a patch actually fires the program behaves as before — so problems in an already-released build can be fixed with Lua.
Is there anything special about performance?Yes — xLua highlights GC optimizations: custom structs and enums cross between Lua and C# with zero C# GC allocations. It also skips code generation while working in the editor, keeping the dev loop lightweight.
Creator
Tencent
Size
25.8 MB
Stars
10.2K
Age
9y
Commits · 90 Days