脚本代码
lua.DoFile("F:CSharpToLuaCSharpToLuaScriptsFromFile.lua");//执行lua脚本文件,这里我直接用了绝对定位
double age = (double)lua["age"];
Console.WriteLine("age = {0}", age);
Console.WriteLine("width = {0}", lua["width"]);
Console.ReadKey();
}
}
}
可以跟着敲一遍,提高与代码的亲密度 ~.~ And then 点击启动
咦、这时会出现一个报错、但是不要紧、
#2317c1e29524eb42a3a3efcd94f77a8c#
解决方案:

再次运行、就可以看到结果啦、惊不惊喜、意不意外

简单的介绍C#如何调用Lua代码、当然、还有很多丰富的API小伙伴们可以自行查阅哟。
(二)Lua调用C#
直接上代码 ~.~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;
namespace CSharpToLua
{
class Program
{
static void Main(string[] args)
{
// 新建一个Lua解释器,每一个Lua实例都相互独立, 一个global全局域
Lua lua = new Lua();
//---------------------------------------------------lua调用c#函数
TestClass obj = new TestClass();
// 注册CLR对象方法到Lua,供Lua调用 typeof(TestClass).GetMethod("TestPrint")
lua.RegisterFunction("TestPrint", obj, obj.GetType().GetMethod("TestPrint"));
// 注册CLR静态方法到Lua,供Lua调用
lua.RegisterFunction("TestStaticPrint", null, typeof(TestClass).GetMethod("TestStaticPrint"));
lua.DoString("TestPrint(10)");
lua.DoString("TestStaticPrint()");
Console.ReadKey();
}
}
class TestClass
{
private int value = 0;
public void TestPrint(int num)
{
value = num;
Console.WriteLine("CSharp"+value);
}
public static void TestStaticPrint()
{
Console.WriteLine("TestStaticPrint");
}
}
}
点击运行.................

perfect~.~ 当然、这里只是简单的介绍C#与Lua是如何相互调用的、小伙伴可自行Google大量丰富的API方便开发哟
重要的事要说三遍:LuaInterface是主要接口、LuaInterface是主要接口、LuaInterface是主要接口










