はじめに
「tolua」を Unity プロジェクトに導入することで
Unity で Lua が使用できるようになります
ライブラリ
- tolua_runtime
https://github.com/topameng/tolua_runtime - Debugger
https://github.com/topameng/Debugger - CString
https://github.com/topameng/CString - protoc-gen-lua
https://github.com/topameng/protoc-gen-lua
フレームワークとデモ
- LuaFrameWork
https://github.com/jarjin/LuaFramework_NGUI
https://github.com/jarjin/LuaFramework_UGUI - XlsxToLua
https://github.com/zhangqi-ulua/XlsxToLua - UnityHello
https://github.com/woshihuo12/UnityHello - UWA-ToLua
http://uwa-download.oss-cn-beijing.aliyuncs.com/plugins%2FiOS%2FUWA-iOS-ToLua.zip
デバッガー
- EmmyLua
https://github.com/tangzx/IntelliJ-EmmyLua - unity_tolua-zerobrane_api
https://github.com/LabOfHoward/unity_tolua-zerobrane_api
クイックスタート
例1
var lua = new LuaState(); lua.Start(); lua.DoString("print('hello world')"); lua.Dispose();
例2
private LuaState luaState; private void Awake() { luaState = LuaClient.GetMainState(); try { luaState.Call("UIShop.Awake", false); } catch (Exception e) { luaState.ThrowLuaException(e); } } private void Start() { luaState.Call("UIShop.Start", false); }
例3
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 連携 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) 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