Lua Interface基礎使用

Lua是一種可愛的腳本語言,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所組成並於1993年開發。 其設計目的是爲了嵌入應用程序中(多數指熱更新),從而爲應用程序提供靈活的擴展和定製功能.html

LuaInterface Lua語言之間是一個庫集成和微軟。網絡平臺的公共語言運行時(CLR)。Lua腳本可使用它來實例化CLR對象,訪問屬性、調用方法,甚至與Lua函數處理事件。(咱們使用C#調用Lua, 固然你能夠用C++)windows

你須要安裝如下組件網絡

LuaForWindows下載地址:http://luaforge.net/projects/luaforwindows/   直接安裝便可函數

LuaInterface下載地址:http://luaforge.net/projects/luainterface/    下載以後把Lua51.dll, LuaInterface.dll 加入C#項目中ui

image

 

1、練習C#訪問Lua定義變量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            //Lua解釋器,每個解釋器都是獨立的
            Lua lua = new Lua();

           lua["num"] = 2;             //建立數字變量
             lua["name"] = "盤子臉";     //建立一個盤子臉名稱
             lua.NewTable("classList");  //建立一個班級列表

              lua.DoString(@"print('hello Interface')");                         //輸出hello Interface
            lua.DoString(@"print(num)");                                       //輸出2
            lua.DoString(@"print(name)");                                       //輸出盤子臉
              lua.DoString(@"classList[1] = '123' print(classList[1])");        //輸出123

            //lua.DoFile();         這個方法是執行一個Lua文件

              //從Lua中獲取定義的變量值
              object[] values = lua.DoString("return num,name");
            double num = (double)lua["num"];
            string name = (string)lua["name"];
            LuaTable table = (LuaTable)lua["classList"];


            Console.WriteLine("num值: " + num);          //輸出 2
            Console.WriteLine("name值: " + name);        //輸出 盤子臉
              Console.WriteLine("table值: " + table[1]);   //輸出 123



            Console.ReadLine();
        }
    }
}

若是你敲完代碼發現程序沒法運行,混合模式程序集是針對「v2.0.50727」版的運行時生成的,在沒有配置其餘信息的狀況下,沒法在 4.0 運行時中加載該程序集。解決方案:http://www.cnblogs.com/plateFace/p/4775257.htmlthis

 

2、方法註冊到Lua,並使用Lua調用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Lua lua = new Lua();
            Program obj = new Program();

            //註冊到Lua方法名能夠不跟C#中的同樣, 註冊普通方法
              lua.RegisterFunction("WriterMyName", obj, obj.GetType().GetMethod("NormalMethod"));
            lua.DoString("WriterMyName()");

            //註冊靜態方法,obj的位置填寫null,
            lua.RegisterFunction("StaticWriterMyName", null, typeof(Program).GetMethod("StaticMethod"));
            lua.DoString("StaticWriterMyName()");

            Console.ReadLine();
        }

        public void NormalMethod() 
        {
            Console.WriteLine("我是盤子臉");
        }

        public static void StaticMethod()
        {
            Console.WriteLine("我是靜態的盤子臉");
        }

    }
}

 

3、Lua中使用C#的類和結構

下面在Lua代碼中使用一下函數就能夠實現Lua調用C#啦lua

  • luanet.load_assembly函數:加載CLR程序集;
  • luanet.import_type函數:加載程序集中的類型;
  • luanet.get_constructor_bysig函數:顯示獲取某個特定的構造函數;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Lua lua = new Lua();
            lua.DoFile("C:\\Lua\\MyLua.lua");   //調用Lua文件執行,Lua裏面在調用C#的類和方法

            Console.ReadLine();
        }
    }

    //定義一個本身的的類
    public class MyLife
    {
        public string myDog;
        public MyLife() { }
        public MyLife(string myDog)
        {
            this.myDog = myDog;
        }
        public void MyFace()
        {
            Console.WriteLine("my face is red");
        }
        public void MyDogName()
        {
            Console.WriteLine("Dog name is " + myDog);
        }
    }
}

Lua中的代碼:spa

print("開始運行interface啦")
luanet.load_assembly("System")                --加載當前dll
Int32 = luanet.import_type("System.Int32")     --引入類型,準備使用C#的System.Int32類
num = Int32.Parse("123")                    --使用類型
print(Int32)                                --輸出類型
print(num)                                    --輸出變量的值

luanet.load_assembly("ConsoleApplication4")
MyLife = luanet.import_type("ConsoleApplication4.MyLife")
life = MyLife()            --建立類
life:MyFace()

life2 = MyLife("哈皮狗")            --使用有參構造建立類
life2:MyDogName()

 

 

//今天就先寫到這裏O(∩_∩)O~.net

相關文章
相關標籤/搜索