原貼:http://blog.csdn.net/sinat_20559947/article/details/48065719
luaframework中只有out的實例:TestOut.unity
直接上例子:
C#代碼:
using System; using LuaInterface; namespace myLua { class MainClass { public string name = "Ocean"; public void CSharpMethod(string name ,out int count) { Console.WriteLine ("這是C#里的一個方法"); count = name.Length; } public void TestRef(string name,ref int count) { Console.WriteLine (name); Console.WriteLine (count); count = name.Length; } public static void Main (string[] args) { // 創建一個Lua解釋器 Lua lua = new Lua(); // 在C#中使用lua的語法調用lua腳本 lua.DoFile("luaScript.lua"); } } }
Lua代碼:
myClass = MainClass() print(myClass.name)
returnValue,strLength = myClass:CSharpMethod("Ocean")
--myClass:CSharpMethod("Ocean",nil) --一般情況下最好是這么寫,不要省略那個參數
--這個地方一般是要加上第二個參數,傳遞nil,因為在通過射線取hit的方法中,獲取的out的值並不是在參數的末尾,如果不傳一個nil,好像是會報錯的
print(returnValue, strLength) --returnValue是方法的返回值,如果沒有返回值就返回nil
returnValue,count = myClass:TestRef("Ocean",5) --這里第二個參數是ref的取值,必需要傳遞一個參數