一直以來對Lua熱更新技術很感興趣,在上周開始了對Lua的學習,主要學的是uLua。
直接上干貨
准備工作:
LuaInterface包括兩個核心庫一個是LuaInterface.dll,一個是Luanet.dll,我們可以通過LuaInterface完成Lua和C#(CLR)之間的互相調用
需要在vs的工程中導入兩個dll分別是LuaInterface.dll和luanet.dll,前者是vs調用lua時需要引用,並在vs的工程中引入命名空間using LuaInterface;引用以后就可以創建lua的解釋器,來執行lua代碼。
lua代碼的腳本需要放在vs工程的輸出環境下即E:\vsworkspace\luainterface\luainterface\bin\Debug文件下。並且編碼格式一定要是ANSI。UTF-8在編譯時會出錯。
luanet.dll是在lua腳本中調用vs的方法或變量時需要引入,require “luanet” 並且需要將該dll放在vs工程的輸出環境下,即E:\vsworkspace\luainterface\luainterface\bin\Debug文件下。
一、在C#中調用lua
1、在C#中執行lua代碼。
1 Lua lua = new Lua(); //創建lua解釋器 2 lua["num"] = 33; //定義一個num 3 lua["string"] = "Hello Lua "; //定義一個字符串 4 Console.WriteLine(lua["num"]); 5 Console.WriteLine(lua["string"]); 6 Console.ReadKey();
2、訪問lua環境中的變量
double num = (double)lua["num"];
string str = (string)lua["str"];
說一下lua變量類型和c#中變量類型的對應
nil null
string System.String
number System.Double
boolean System.Boolean
table LuaInterface.LuaTable
Function LuaInterface.LuaFunction
3、在C#中執行Lua腳本文件,或者腳本字符串
1 Lua lua = new Lua(); //創建lua解釋器 2 //lua.DoString("xxxxxxxxxx"); 里邊寫的字符串就相當於lua腳本中的代碼 3 lua.DoString("num=2"); 4 lua.DoString("str = 'a string'"); 5 6 object[] strs = lua.DoString("return num,str"); 7 8 foreach (object item in strs) 9 { 10 Console.WriteLine(item); 11 }
1 Lua lua = new Lua(); //創建lua解釋器 2 lua.DoFile("myLua.lua");//這個lua腳本需要放在vs的bin/debug文件夾下
4、把一個C#方法注冊進Lua的一個全局方法
1 //把一個類中的普通方法注冊進去 2 Program p = new Program(); 3 lua.RegisterFunction("LuaMethod", p, p.GetType().GetMethod("CLRMethod")); 4 lua.DoString("LuaMethod()");//執行這個方法 5 6 7 //把一個類的靜態方法注冊進去 8 lua.RegisterFunction("MyStaticMethod",null,typeof(Program).GetMethod("MyStaticMethod")) 9 lua.DoString(" MyStaticMethod()") //執行這個方法 10 11 12 public void CLRMethod() 13 { 14 Console.WriteLine("哈哈"); 15 } 16 17 public static void MyStaticMethod() 18 { 19 Console.WriteLine("這是靜態方法"); 20 }
二、在Lua中使用c#中的類 這是lua中的代碼
1 require "luanet" --請求連接 2 3 luanet.load_assembly("System") --load_assembly 加載程序集 4 luanet.load_assembly("testluainterface") 5 6 7 Int32 = luanet.import_type("System.Int32") --import_type 導入類型 8 Program = luanet.import_type("testluainterface.Program") 9 10 num = Int32.Parse("123456") 11 12 --print(Int32) 13 14 --print(num) 15 16 program1 = Program() --使用類型 17 18 --print(program1.name) --輸出變量 19 --program1:TestMethod() --調用方法 20 21 --void, strlength = program1:TestOut("hahaha") --調用帶有Out的方法 22 --print(void,strlength) --輸出方法的返回值 和 out的返回值 C#的方法沒有返回值這里也需要接收 會返回nil 23 void ,strlength = program1:TestRef("sahdjkhaskd",10) --調用帶有Ref的方法 需要給ref傳值 24 print(void,strlength) --輸出方法的返回值 和 ref的返回值
lua腳本寫好之后在C#中使用lua.DoFile("腳本的名字.lua")進行調用就會執行lua這個腳本即可。
在C#中對應的在Program類中的一些方法如下
1 public string name = "zhangli"; 2 3 public void TestMethod() 4 { 5 Console.WriteLine("TestMethod"); 6 } 7 8 public void TestOut(string name,out int count) 9 { 10 Console.WriteLine(name); 11 count = name.Length; 12 } 13 14 public void TestRef(string name,ref int count) 15 { 16 Console.WriteLine(name ); 17 Console.WriteLine(count); 18 count = name.Length; 19 }
三、在Lua中通過Add方法或者Remove方法把一個Lua的函數注冊或者注銷從C#中的事件委托中
3 function method() 4 5 end 6 7 obj.SomeEvent:Add(methodname(不用帶引號))
根據學習進度今天分享這些基礎內容。后續會繼續學習。歡迎批評指正,共同學習。
歡迎廣大博友加群學習165628892(進群備注:博客) 隨時提出問題解決問題!
樹欲靜而風不止,子欲養而親不待
2016年12月15日17:30:16