xlua-C#代码访问lua的变量和函数
C#代码如何访问lua代码中数据呢?主要基本数据类型、table以及function函数等。lua代码脚本如下
--Lua全局变量
--[[基本数据类型]]--
Num = 100;
Name = 'Hello';
IsOk = true;
--[]--
example_table={
id = 'W',
age = 10,
sex = 0,
1,--多余变量1
2,--多余变量2
setSex = function(self,sexValue)
sex = sexValue;
end,
getSex = function()
return sex;
end
}
example_table2={
id = 102,
num = 25,
vip = 3
}
example_table3 = {
1,
2,
3
}
--[[函数function]]--
function Method1()
print('=====lua method1=====');
end
function Method2(a,b)
print('=====lua method2=====');
c = a+b;
a=a+2;
b=3;
return c,a,b;
end
function Method3()
print('=====lua method3=====');
return Method1;
end
C#代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
/*
* Author:W
*C#访问Lua中的数据变量,方法的方式
*/
public class CSCallLuaTest : MonoBehaviour {
private LuaEnv luaEnv = null;
/// <summary>
/// Lua表映射到Class类
/// 【注意1】值拷贝-》修改不会同步 ,并且针对复杂类型,性能代价大
/// 【注意2】不能映射Lua函数
/// </summary>
public class ExampleTable
{
public string id;
public int age;
public int sex;
}
private ExampleTable exampleTable;
/// <summary>
/// Lua表映射到struct
/// 注意点与class相同
/// </summary>
public struct ExampleStruct
{
public string id;
public int age;
public int sex;
}
private ExampleStruct exampleStruct;
/// <summary>
/// Lua表映射到Interface
/// 【注意1】可以映射Lua函数
/// 【注意2】需要加上标签,并且点击“Xlua-》Generate Code”菜单命令生成Bride结尾的C#脚本代码,
/// 在Assets/Xlua/Gen文件夹中可查看
/// 【注意3】属于引用型映射,同步修改
/// </summary>
public interface ExampleInterface
{
string id { get; set; }
int age { get; set; }
int sex { get; set; }
void setSex(int sexV);
int getSex();
}
private ExampleInterface exampleInterface;
/// <summary>
/// Lua表映射到字典Dictionary
/// 【注意1】前提保证:键key与值Value的类型是一致的
/// 【注意2】值拷贝
/// </summary>
public Dictionary<string, int> infoDict = new Dictionary<string, int>();
/// <summary>
/// Lua表映射到List
/// 【注意1】前提保证:值Value的类型是一致的
/// 【注意2】值拷贝
/// </summary>
public List<int> infoList = new List<int>();
/// <summary>
/// Lua表映射到LuaTable
/// 【注意1】无需生成代码,性能比interface接口慢一个数量级,且无类型检查
/// 【注意2】引用型映射,修改同步
/// </summary>
public LuaTable infoTable;
Action method1;
/// <summary>
/// Lua Function函数映射到Delegate委托
/// 【注意1】性能好,类型安全。但需要生成代码
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="b2"></param>
/// <param name="b3"></param>
/// <returns></returns>
public delegate int Method2(int a, int b, ref int b2, out int b3);
Method2 method2;
/// <summary>
/// Lua Function函数映射到Delegate委托,并返回另外一个委托
/// </summary>
/// <returns></returns>
public delegate Action Method3();
Method3 method3;
/// <summary>
/// Lua 函数映射到LuaFunction
/// 【注意1】不需要生成代码,性能代价较大
/// </summary>
private LuaFunction luaFunction;
void Awake()
{
luaEnv = new LuaEnv();
luaEnv.DoString("require 'CSCallLua'");
}
// Use this for initialization
void Start () {
Debug.Log("Lua Global Num="+ luaEnv.Global.Get<int>("Num"));
Debug.Log("Lua Global Name="+luaEnv.Global.Get<string>("Name"));
Debug.Log("Lua Global IsOk="+luaEnv.Global.Get<bool>("IsOk"));
exampleTable = luaEnv.Global.Get<ExampleTable>("example_table");
Debug.Log("Lua ExampleTable普通类映射 Id=" + exampleTable.id + " Age=" + exampleTable.age + " Sex=" + exampleTable.sex);
exampleStruct = luaEnv.Global.Get<ExampleStruct>("example_table");
Debug.Log("Lua ExampleTable结构体映射 Id=" + exampleStruct.id + " Age=" + exampleStruct.age + " Sex=" + exampleStruct.sex);
exampleInterface = luaEnv.Global.Get<ExampleInterface>("example_table");
Debug.Log("Lua ExampleTable接口映射 Id=" + exampleInterface.id + " Age=" + exampleInterface.age + " Sex=" + exampleInterface.sex);
Debug.Log("Lua ExampleTable接口映射 函数setSex(5)");
exampleInterface.setSex(5);
Debug.Log("Lua ExampleTable接口映射 函数 getSex():"+exampleInterface.getSex());
infoDict = luaEnv.Global.Get<Dictionary<string, int>>("example_table2");
foreach (KeyValuePair<string, int> v in infoDict)
{
Debug.Log("Lua ExampleTable2 字典映射 key="+v.Key+" value="+v.Value);
}
infoList = luaEnv.Global.Get<List<int>>("example_table3");
for (int i = 0; i < infoList.Count; i++)
{
Debug.Log("Lua ExampleTable3 List映射 i="+i+" value= "+infoList);
}
//===============LuaTable==================
infoTable = luaEnv.Global.Get<LuaTable>("example_table");
Debug.Log("Lua ExampleTableLuaTable类 映射 Id = "+infoTable.Get<string>("id")+" Age="+infoTable.Get<int>("age")+" Sex="+infoTable.Get<int>("sex"));
//=======Delegate========
method1 = luaEnv.Global.Get<Action>("Method1");
method1();
method2 = luaEnv.Global.Get<Method2>("Method2");
int b2 = 0;
int b3 = 0;
int b1= method2(2,6,ref b2,out b3);
Debug.Log("Lua Function 函数映射method2b1="+b1+" b2="+b2+" b3="+b3);
method3 = luaEnv.Global.Get<Method3>("Method3");
Action res = method3();
res();
//======LuaFunction=======
luaFunction = luaEnv.Global.Get<LuaFunction>("Method2");
object[] resArr = luaFunction.Call(new object[] { 1, 2 }, new Type[] { typeof(int), typeof(int),typeof(int)});
Debug.Log("LuaFunction映射到LuaFunction函数:b1="+(int)resArr+" b2="+(int)resArr+" b3="+(int)resArr);
}
// Update is called once per frame
void Update () {
if (luaEnv != null)
luaEnv.Tick();
}
void OnDestroy()
{
if (luaEnv != null)
luaEnv.Dispose();
}
}运行结果如下
页:
[1]