Home

Awesome

tolua#

tolua# is a Unity lua static binder solution. the first solution that analyzes code by reflection and generates wrapper classes.<br> It is a Unity plugin that greatly simplifies the integration of C# code with Lua. which can automatically generate the binding code to access Unity from Lua and map c# constants, variables, functions, properties, classes, and enums to Lua.<br> tolua# grows up from cstolua. it's goal is to be a powerful development environment for Unity.<br> Support unity4.6.x and Unity5.x all(copy /Unity5.x/Assets to /Assets) <br> If you want to test examples(example 1 is excluded)in mobile, first click menu Lua/Copy lua files to Resources. then build it <br> 如果你想在手机上测试例子(例子1除外),首先点击菜单Lua/Copy lua files to Resources, 之后再build. <br>

欢迎大家点星支持,谢谢^_^<br> 有bug 可以到QQ群286510803反馈。 可以加讨论群: <br> ulua&tolua技术交流群① 341746602(已满) <br> ulua&tolua技术讨论群② 469941220(已满) <br> tolua#技术讨论群④ 543826216(已满)<br> tolua#技术群 286510803<br>

Library

tolua_runtime <br> https://github.com/topameng/tolua_runtime <br> Debugger <br> https://github.com/topameng/Debugger <br> CString <br> https://github.com/topameng/CString <br> protoc-gen-lua <br> https://github.com/topameng/protoc-gen-lua <br>

FrameWork and Demo

LuaFrameWork<br> https://github.com/jarjin/LuaFramework_NGUI <br> https://github.com/jarjin/LuaFramework_UGUI <br> XlsxToLua<br> https://github.com/zhangqi-ulua/XlsxToLua<br> UnityHello<br> https://github.com/woshihuo12/UnityHello<br> UWA-ToLua<br> http://uwa-download.oss-cn-beijing.aliyuncs.com/plugins%2FiOS%2FUWA-iOS-ToLua.zip<br>

Debugger

EmmyLua<br> https://github.com/tangzx/IntelliJ-EmmyLua<br> unity_tolua-_zerobrane_api<br> https://github.com/LabOfHoward/unity_tolua-_zerobrane_api<br>

Packages

 Basics        Math      Data Structures<br>  luabitop       Quaternion       list<br>   struct         Vector3        event<br>   int64          Vector4        slot<br>   Time          Vector2<br> Networking        Ray<br>  luasocket         Color<br>  Parsing        Bounds<br>  lpeg             Mathf<br>  Protol           Touch<br>  pblua          RaycastHit<br>

特性

快速入门

在CustomSetting.cs中添加需要导出的类或者委托,类加入到customTypeList列表,委托加入到customDelegateList列表 <br> 通过设置saveDir变量更改导出目录,默认生成在Assets/Source/Generate/下,点击菜单Lua->Generate All,生成绑定文件 <br> 在LuaConst.cs中配置开发lua文件目录luaDir以及tolua lua文件目录toluaDir <br>

//例子1
LuaState lua = new LuaState();
lua.Start();
lua.DoString("print('hello world')");
lua.Dispose();

//例子2
LuaState luaState = null;

void Awake()
{
    luaState = LuaClient.GetMainState();

    try
    {            
        luaState.Call("UIShop.Awake", false);
    }
    catch (Exception e)
    {
        //Awake中必须这样特殊处理异常
        luaState.ThrowLuaException(e);
    }
}

void Start()
{
    luaState.Call("UIShop.Start", false);
}
local go = GameObject('go')
go:AddComponent(typeof(UnityEngine.ParticleSystem))
go.transform.position = Vector3.zero
go.transform:Rotate(Vector3(0,90,0), UnityEngine.Space.World)
go.transform:Rotate(Vector3(0, 1, 0), 0)

--DoTween 需要在CustomSetting导出前定义USING_DOTWEENING宏,或者取消相关注释
go.transform:DORotate(Vector3(0,0,360), 2, DG.Tweening.RotateMode.FastBeyond360)

Shop = {}

function Shop:Awake()
    self.OnUpdate = UpdateBeat:CreateListener(Shop.Update, self)
    UpdateBeat:AddListener(self.OnUpdate)
end

function Shop:OnDestroy()
    UpdateBeat:RemoveListener(self.OnUpdate)
end

function Shop:OnClick()
    print("OnClick")
end

function Shop:OnToggle()
    print("OnToggle")
end

function Shop:Update()
end

--委托
local listener = UIEventListener.Get(go)
listener.onClick = function() print("OnClick") end
listener.onClick = nil
listener.onClick = UIEventListener.VoidDelegate(Shop.OnClick, Shop)
listener.onClick = listener.onClick + UIEventListener.VoidDelegate(Shop.OnClick, Shop)
listener.onClick = listener.onClick - UIEventListener.VoidDelegate(Shop.OnClick, Shop)

local toggle = go:GetComponent(typeof(UIToggle))
EventDelegate.Add(toggle.onChange, EventDelegate.Callback(Shop.OnToggle, Shop))
EventDelegate.Remove(toggle.onChange, EventDelegate.Callback(Shop.OnToggle, Shop))

--事件
local Client = {}

function Client:Log(str)
end

Application.logMessageReceived = Application.logMessageReceived + Application.LogCallback(Clent.Log, Client)
Application.logMessageReceived = Application.logMessageReceived - Application.LogCallback(Clent.Log, Client)

--out参数
local _layer = 2 ^ LayerMask.NameToLayer('Default')
local flag, hit = UnityEngine.Physics.Raycast(ray, nil, 5000, _layer)

if flag then
    print('pick from lua, point: '..tostring(hit.point))
end

这里是更多的示例。

关于反射

tolua# 不支持动态反射。动态反射对于重载函数有参数匹配问题,函数排序问题,ref,out 参数问题等等。<br> tolua#提供的替换方法是:<br>

  1. preloading, 把你未来可能需要的类型添加到导出列表customTypeList,同时也添加到dynamicList列表中,这样导出后该类型并不会随binder注册到lua中,你可以通过 require "namespace.classname" 动态注册到lua中,对于非枚举类型tolua#系统也可以在第一次push该类型时动态载入,当然也可在过场动画、资源下载、登录、场景加载或者某个的函数中require这个类型。<br>
  2. 静态反射,参考例子22。通过静态反射支持精确的函数参数匹配和类型检查。不会存在重载函数参数混乱匹配错误问题, 注意iOS必须配置好link.xml<br>

Performance

平台属性读写重载函数Vector3构造GameObject构造Vector3归一化Slerp
PC0.0465:0.150.076:0.120.02:0.0010.1:0.140.014:0.0010.10:0.11
Android0.16:1.10.28:0.760.17:0.000350.43:0.50.21:0.020.3:0.06
iOS0.04:0.1450.055:0.110.017:0.050.074:0.080.035:0.110.078:0.5

测试结果为C#:Lua. 环境不同会略有差异。可用数字倍率做参考<br> PC: Intel(R) Core(TM) i5-4590 CPU@3.3GHz + 8GB + 64 位win7 + Unity5.4.5p4<br> Android: 中兴nubia z9 max(NX512J) + Adnroid5.0.2<br> iOS(il2cpp): IPhone6 Plus<br> 按照1.0.7.355版本更新了测试数据, u5相对u4, 安卓上c#有了不小的提升<br>

Examples

参考包内1-24例子

About Lua

win, android ios using luajit2.1-beta3. macos using luac5.1.5(for u5.x). 注意iOS未编译模拟器库,请用真机测试