usingSystem;
usingLuaInterface;
usingSystem.IO;
usingSystem.Text;
namespaceLuaForCsharp_01
{
publicclassHero{
publicstringname="沙王";
publicfloatattack=250;
publicvoidAttack(){
Console.WriteLine(this.name+"发起攻击伤害值为:"+this.attack);
}
publicstaticvoidInit(){
Console.WriteLine("这是C#中的静态方法");
}
}
classMainClass
{
publicstaticvoidMain(string[]args)
{
//创建lua环境
Lua evn=newLua();
//通过lua环境创建两个变量
evn["num"]=1;
evn["name"]="张三";
//执行lua中的代码
evn.DoString("print(name)");
//我们可以将一段lua代码放进去
evn.DoString(@"
Player={}
Player['name']='猴子'
print(Player.name)
");
//c#中注册普通方法给lua调用
Hero h=newHero();
evn.RegisterFunction("HeroAttack",h,h.GetType().GetMethod("Attack"));
//C#中静态方法的注册
evn.RegisterFunction("Init",null,typeof(Hero).GetMethod("Init"));
evn.DoFile("C:\\Users\\Administrator\\Desktop\\LuaForCsharp_01\\LuaForCsharp_01\\lua02.lua");
//evn.DoString("HeroAttack()");
//FileStream.TextFile=File.Open(@"c:\tempuploads\newFile.txt",FileMode.Append);
}
}
}
txt文件
print("这是lua文件-----")
for i=1 ,10 do
print(i)
end
HeroAttack()
Init()
-- lua 中应用c#类型
—-引入命名空间
luanet.load_assembly("System”)
Int32=luanet.import_type("System.Int32")
num = Int32.Parse("123")
print(num)
luanet.load_assembly("LuaForCsharp_01")
--加载Hero类型
Hero = luanet.import_type("LuaForCsharp_01.Hero")
hero = Hero()
print(hero.name)
hero:Attack()
http://blog.csdn.net/u011244737/article/details/49998437