xlua-C#代码访问lua的变量和函数

C#代码如何访问lua代码中数据呢?主要基本数据类型、table以及function函数等。

lua代码脚本如下

--Lua全局变量

--[[基本数据类型]]--
Num = 100;
Name = 'Hello';
IsOk = true;

--[[table表数据]]--

 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;

	/// 
	/// Lua表映射到Class类
	/// 【注意1】值拷贝-》修改不会同步 ,并且针对复杂类型,性能代价大
	/// 【注意2】不能映射Lua函数
	/// 
	public class ExampleTable
	{
		public string id;
		public int age;
		public int sex;		
	}


	private ExampleTable exampleTable;

	/// 
	/// Lua表映射到struct
	/// 注意点与class相同
	/// 
	public struct ExampleStruct
	{
		public string id;
		public int age;
		public int sex;
	}

	private ExampleStruct exampleStruct;

	/// 
	/// Lua表映射到Interface
	/// 【注意1】可以映射Lua函数
	/// 【注意2】需要加上[CSharpCallLua]标签,并且点击“Xlua-》Generate Code”菜单命令生成Bride结尾的C#脚本代码,
	/// 在Assets/Xlua/Gen文件夹中可查看
	/// 【注意3】属于引用型映射,同步修改
	/// 
	[CSharpCallLua]
	public interface ExampleInterface
	{
		string id { get; set; }
		int age { get; set; }

		int sex { get; set; }

		void setSex(int sexV);

		int getSex();
	}

	private ExampleInterface exampleInterface;

	/// 
	/// Lua表映射到字典Dictionary
	/// 【注意1】前提保证:键key与值Value的类型是一致的
	/// 【注意2】值拷贝
	/// 
	public Dictionary infoDict = new Dictionary();

	/// 
	/// Lua表映射到List
	/// 【注意1】前提保证:值Value的类型是一致的
	/// 【注意2】值拷贝
	/// 
	public List infoList = new List();

	/// 
	/// Lua表映射到LuaTable
	/// 【注意1】无需生成代码,性能比interface接口慢一个数量级,且无类型检查
	/// 【注意2】引用型映射,修改同步
	/// 
	public LuaTable infoTable;


	Action method1;

	/// 
	/// Lua Function函数映射到Delegate委托
	/// 【注意1】性能好,类型安全。但需要生成代码
	/// 
	/// 
	/// 
	/// 
	/// 
	/// 
   [CSharpCallLua]
	public delegate int Method2(int a, int b, ref int b2, out int b3);

	Method2 method2;

	/// 
	/// Lua Function函数映射到Delegate委托,并返回另外一个委托
	/// 
	/// 
	[CSharpCallLua]
	public delegate Action Method3();

	Method3 method3;
	/// 
	/// Lua 函数映射到LuaFunction
	/// 【注意1】不需要生成代码,性能代价较大
	/// 
	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("Num"));
		Debug.Log("Lua Global Name="+luaEnv.Global.Get("Name"));
		Debug.Log("Lua Global IsOk="+luaEnv.Global.Get("IsOk"));

		exampleTable = luaEnv.Global.Get("example_table");
		Debug.Log("Lua ExampleTable  普通类映射 Id=" + exampleTable.id + " Age=" + exampleTable.age + " Sex=" + exampleTable.sex);

		exampleStruct = luaEnv.Global.Get("example_table");
		Debug.Log("Lua ExampleTable  结构体映射 Id=" + exampleStruct.id + " Age=" + exampleStruct.age + " Sex=" + exampleStruct.sex);

		exampleInterface = luaEnv.Global.Get("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>("example_table2");
		foreach (KeyValuePair v in infoDict)
		{
			Debug.Log("Lua ExampleTable2 字典映射 key="+v.Key+" value="+v.Value);
		}

		infoList = luaEnv.Global.Get>("example_table3");
		for (int i = 0; i < infoList.Count; i++)
		{
			Debug.Log("Lua ExampleTable3 List映射 i="+i+" value= "+infoList[i]);
		}

		//===============LuaTable==================
		infoTable = luaEnv.Global.Get("example_table");
		Debug.Log("Lua ExampleTable  LuaTable类 映射 Id = "+infoTable.Get("id")+" Age="+infoTable.Get("age")+" Sex="+infoTable.Get("sex"));

		//=======Delegate========
		method1 = luaEnv.Global.Get("Method1");
		method1();

		method2 = luaEnv.Global.Get("Method2");
		int b2 = 0;
		int b3 = 0;
		int b1= method2(2,6,ref b2,out b3);
		Debug.Log("Lua Function 函数映射method2  b1="+b1+" b2="+b2+" b3="+b3);

		method3 = luaEnv.Global.Get("Method3");
		Action res = method3();
		res();

		//======LuaFunction=======
		luaFunction = luaEnv.Global.Get("Method2");		
		object[] resArr = luaFunction.Call(new object[] { 1, 2 }, new Type[] { typeof(int), typeof(int),typeof(int)});
		Debug.Log("Lua  Function映射到LuaFunction函数:b1="+(int)resArr[0]+" b2="+(int)resArr[1]+" b3="+(int)resArr[2]);

	}




	
	// Update is called once per frame
	void Update () {
		if (luaEnv != null)
			luaEnv.Tick();
	}

	void OnDestroy()
	{
		if (luaEnv != null)
			luaEnv.Dispose();
	}

}

运行结果如下

xlua-C#代码访问lua的变量和函数_第1张图片

xlua-C#代码访问lua的变量和函数_第2张图片

 

你可能感兴趣的:(Lua,Unity,xlua,C#访问Lua变量和函数)