Lua 入门

来源:互联网 发布:mac系统qq截图快捷键 编辑:程序博客网 时间:2024/05/20 19:47

本文固定连接:http://blog.csdn.net/u013108312/article/details/52814058

LuaInterface包括两个核心库一个是LuaInterface.dll,一个是Luanet.dll,我们可以通过LuaInterface完成Lua和C#(CLR)之间的互相调用

在C#中执行访问Lua代码

Lua lua = new Lua();    //创建Lua解析器    lua["num"]=2;   //定义一个num    lua["str"]="a string";  //定义一个字符串    lua.newTable("tab");    //创建一个表  tab={}

取得lua环境中的变量

        double num = (double)lua["num"];        string str = (string)lua["str"];

在C#中执行Lua脚本文件,或者脚本字符串

    lua.DoFile("script.lua");//执行script.lua脚本    lua.DoString("num=2");    lua.DoString("str='a string'");    object[] retVals = lua.DoString("return num,str");

在热更新中,只需要写好解析lua脚本的代码,然后c#代码不需要变动,只需要修改lua脚本就好,通过lua脚本控制游戏逻辑。

lua和C#中类型的对应

    nil         null    string      System.String    number      System.Double    boolean         System.Boolean    table       LuaInterface.LuaTable    function        LuaInterface.LuaFunction

把一个C#方法注册进Lua的一个全局方法

//把一个类中的普通方法注册进去lua.RegisterFunction("NormalMethod",obj,obj.GetType().GetMethod("NormalMethod"))lua.DoString(" NormalMethod()");//把一个类的静态方法注册进去lua.RegisterFunction("StaticMethod",null,typeof(ClassName).GetMethod("StaticMethod"))lua.DoString(" StaticMethod()")

在Lua中使用c#中的类

require "luanet"--加载CLR的类型、实例化CLR对象luanet.load_assembly("System.Windows.Forms")luanet.load_assembly("System.Drawing")Form = luanet.import_type("System.Windows.Forms.Form")StartPosition = luanet.import_type("System.Windows.Forms.FormStartPosition")print(Form)print(StartPosition)在Lua中使用C#中的类创建对象的时候,会自动匹配最合适的构造方法

在Lua中访问C#中的属性和方法

Lua代码中,访问C#对象的属性的方式和访问table的键索引一样,比如obj.name 或者 obj[“name”]

Lua代码中,访问C#对象的普通函数的方式和调用table的函数一样,比如obj:method1()

在Lua中访问C#中的方法-特殊情况

当函数中有outref参数时,out参数和ref参数和函数的返回值一起返回,并且调用的时候,out参数不需要传入C#函数定义class Obj{    int OutMethod1(int parameter1,out parameter2,out parameter3)        {        parameter2=34;parameter3=213;        return parameter1;    }    int OutMethod2(int parameter1,ref parameter2)    {        parameter2=parameter2+2;        return parameter1+parameter2;    }}
Lua中的调用和返回值obj:OutMethod1(34) --out参数不需要参数,这个返回一个table,里面的值为parameter1,parameter2,parameter3(34,34,213)obj:OutMethod2(10,10)--ref参数需要传入,返回一个table有两个值(value1,value2)

在Lua中访问C#中的方法-特殊情况

当有重载函数的时候,调用函数会自动匹配第一个能匹配的函数可以使用get_method_bysig函数得到C#中指定类的指定参数的函数用法luaMethod = get_method_bysig(Obj,"CSharpMethod","System.String")                  luaMethod("siki")

在Lua中注册C#中的事件委托(event delegate)

在Lua中通过Add方法或者Remove方法把一个Lua的函数注册或者注销从C#中的事件委托中        function method()        end        obj.SomeEvent:Add(methodname(不用带引号))

TestScript

using LuaInterface;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{    class Program    {        public string name = "circle";        public void TestMethod()        {            Console.WriteLine("this is a C# TestMesthod ");        }        public void TestOut(string outname,out int count)        {            Console.WriteLine(outname);            count = outname.Length;        }        public void TestRef(string name,ref int count)        {            Console.WriteLine(name);            Console.WriteLine(count);            count = name.Length;        }        static void Main(string[] args)        {            Lua lua = new Lua();            lua["num"] = 12;            Console.WriteLine(lua["num"]);            Console.WriteLine("--------------------------");            lua.DoString("num = 2");            lua.DoString("str = 'a string'");            object[] values = lua.DoString("return num,str");            foreach (object obj in values)            {                Console.WriteLine(obj);            }            Console.WriteLine("--------------------------");            lua.DoFile("myLua.lua");            Console.WriteLine("--------------------------");            Program p = new Program();            lua.RegisterFunction("LuaMethod", p, p.GetType().GetMethod("CLRMethod"));            lua.DoString("LuaMethod()");            lua.RegisterFunction("MyStaticMethod", null, typeof(Program).GetMethod("MyStaticMethod"));            lua.DoString("MyStaticMethod()");            Console.WriteLine("--------------------------");            lua.DoFile("script.lua");            Console.ReadKey();        }        public void CLRMethod()        {            Console.WriteLine("this is a c# clrmethod");        }        public static void MyStaticMethod()        {            Console.WriteLine("this is a C# static method");        }    }}

MyLua.lua

num = 2str = "circle"print(num,str)print("Hello Dofile")

script.lua

print("this call the script.lua")require "luanet"luanet.load_assembly("System")luanet.load_assembly("ConsoleApplication1")Int32 = luanet.import_type("System.Int32")Program = luanet.import_type("ConsoleApplication1.Program")num = Int32.Parse("3435")print(Int32)print(num)print("-------------------------")program1  = Program()print(program1.name)program1:TestMethod()print("-------------------------")void,strLength = program1:TestOut("this is Test String")print(void,strLength)print("-------------------------")void,count=program1:TestRef("this is a test Ref",10)print(void,count)

本文固定连接:http://blog.csdn.net/u013108312/article/details/52814058

0 0
原创粉丝点击