unity xlua入门

xlua脚本 myxlua.lua.txt

print("my xlua file");
num1=10
str1='this is lua string'
tt={
name='zyx',
ago=20,
"red",
"blue",
"yellow",
123,
mm=function()
print('this mm lua string')
end
}

function tt.fun1(self)
print(self.name)
end

function tt:fun2(num1,num2)
print(num1+num2)
end

c# 脚本调用语言
using UnityEngine;
using XLua;

public class xcluademo1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//最简单的lua
globexlua.luaenv.DoString("print('hellow lua')");

    //调用lua文件 myxlua.lua.txt
    globexlua.luaenv.DoString("require 'myxlua'");

    //利用interface 接口对接lua,table中的数据以及方法 需要标记 [CSharpCallLua]
    person p1 = globexlua.luaenv.Global.Get("tt");
    p1.fun1();
    p1.fun2(12,13);
    p1.name = "xlua"; //可更改虚拟机中的lua程序定义的值
    globexlua.luaenv.DoString("print(tt.name)");

    //利用类映射lua中定义的属性
    student s = globexlua.luaenv.Global.Get("tt");
    print(s.name);
    print(s.ago);
    s.ago = 10; //不可更改虚拟机中的lua程序定义的值
    globexlua.luaenv.DoString("print(tt.ago)");
}

}

[CSharpCallLua]
public interface person
{
string name { set; get; }
string age { set; get; }
void mm();
void fun1();
void fun2(int x,int y);
}

class student
{
public string name;
public int ago;
}

你可能感兴趣的:(unity xlua入门)