Resources/1.lua.txt
Config = {
name = '张无忌',
age = 30,
ip = '168'
}
Name = "赵明"
Age = 20
IsBoy = false
function printInfo(a)
print("C# 传递过来的参数是:" .. a)
return 100
end
print("test0.lua执行完毕")
========================= 脚本 =========================
using UnityEngine;
using XLua;
public class Config
{
public string name;
public int age;
public int ip;
}
delegate int Fun(int x);
public class LuaCallCsharp : MonoBehaviour
{
private LuaEnv _env = new LuaEnv();
private void OnGUI()
{
if (GUILayout.Button("part1 C#调用"))
{
CSharpDoLua();
}
if (GUILayout.Button("part2 C# 调用LuaFile"))
{
CSharpDoLuaFile();
}
}
void CSharpDoLuaFile()
{
_env.DoString("require '1'");
// 调用Lua中的全局变量
print(_env.Global.Get<string>("Name"));
print(_env.Global.Get<int>("Age"));
print(_env.Global.Get<bool>("IsBoy"));
// 获取Lua总的表中数据
LuaTable table = _env.Global.Get<LuaTable>("Config");
print(table.Get<string>("name"));
print(table.Get<int>("age"));
print(table.Get<int>("ip"));
print("===============映射类================");
Config c1 = _env.Global.Get<Config>("Config");
print(c1.age);
print(c1.name);
print(c1.ip);
print("===============映射Function================");
LuaFunction function = _env.Global.Get<LuaFunction>("printInfo");
object[] datas = function.Call(123);
int x = System.Convert.ToInt32(datas[0]);
print(x);
print("===============映射Delegate================");
Fun f = _env.Global.Get<Fun>("printInfo");
print(f(23));
}
void CSharpDoLua()
{
_env.DoString("print 'Hello'");
}
}