# Slua Manual > 基本Slua使用流程 ### 啟動 ``` LuaSvr luaMain = new LuaSvr(); luaMain.init(null, () => { luaMain.start("testLua"); //啟動的Lua檔名,並從main開始執行 }); ``` --- ### Call Lua function **Lua:** ``` local class={} function class:testCallback() local test = "Call Back !!"; print(test); end ``` **C#** ``` var testSvr = new LuaSvr(); testSvr.init(null, () => { //初始化 self = (LuaTable)svr.start("xxxx"); }); var luaFunction = (LuaFunction)self["testCallback"]; luaFunction.call(self); ``` **C#呼叫已初始化的lua function** 假設 lua 有設定 module ``` module("ABC", package.seeall) function Test() print("ABC.Test") end function GetValue() return 99 end ``` C# 呼叫 lua module ABC 的 Test() ``` LuaFunction func = LuaState.main.getFunction("ABC.Test"); func.call(); ``` C# 呼叫 lua module ABC 的 GetValue() 並取得回傳結果 ``` LuaFunction func2 = LuaState.main.getFunction("ABC.GetValue"); object output = func2.call(); Debug.Log(Convert.ToInt32(output)); ``` --- **如何將c# script轉換成LuaObject** 1. c# class加上attribute **[CustomLuaClass]** * 如果不加attribute, 需要在 **Slua/Editor/CustomExport.cs** 手動增加對應的class type 2. editor menu **"SLua/Custom/"** 按下 **Make** 3. 如果因為改過公開屬性或方法而造成error, 可以先按 **Clear** 後再按 **Make** **C# class有namespace的情況** * 假設有c# class **ABC**, namespace **Lobby.UI** * 轉換成LuaObject的檔名會是 **Lua_Lobby_UI_ABC.cs** * lua端的呼叫方式和c#相同 **Lobby.UI.ABC** --- ###### tags: `Lua` `Slua`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up