Giter Club home page Giter Club logo

xlua's Introduction

license release PRs Welcome Build status

(English Documents Available)

C# 下 Lua 编程支持

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


xLua 的突破

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

  • 可以运行时把 C# 实现(方法,操作符,属性,事件等等)替换成 Lua 实现;
  • 出色的 GC 优化,自定义 struct,枚举在 Lua 和 C# 间传递无 C# GC Alloc;
  • 编辑器下无需生成代码,开发更轻量;

更详细的特性、平台支持介绍请参考 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 的系统函数,推荐方式是:

  • 声明

    [XLua.CSharpCallLua]
    public delegate double LuaMax(double a, double b);
  • 绑定

    var max = luaEnv.Global.GetInPath<LuaMax>("math.max");
  • 调用

    Debug.Log("max:" + max(32, 12));

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


热补丁

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

  • 侵入性小,老项目原有代码不做任何调整就可使用。
  • 运行时影响小,不打补丁基本和原有程序一样。
  • 出问题了可以用 Lua 来打补丁,这时才会走到 Lua 代码逻辑;

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


更多示例


技术支持

一群:612705778 (已满)

二群:703073338 (已满)

三群:811246782

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

回答:FAQ

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

xlua's People

Contributors

chexiongsheng avatar jayatubi avatar forsakenyang avatar ps5mh avatar xuyanghuang-tencent avatar magicqy avatar zxsean avatar dotlive avatar dreamcirno avatar ezhex1991 avatar xpol avatar hsl4125 avatar zhaoboqiang avatar slimenull avatar chaosddp avatar vkensou avatar seanwengccp avatar rebelmx4 avatar pureivan avatar pkujhd avatar bigoyayubi avatar ootroyoo avatar liuyi13-cpu avatar lindaluo1113 avatar waizui avatar clainele avatar ineversleeeeep avatar wangyongxina avatar 3wz avatar xiangyuan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.