飞道的博客

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

488人阅读  评论(0)

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

lua代码脚本如下


  
  1. --Lua全局变量
  2. --[[基本数据类型]] --
  3. Num = 100;
  4. Name = 'Hello';
  5. IsOk = true;
  6. --[[table表数据]] --
  7. example_table={
  8. id = 'W',
  9. age = 10,
  10. sex = 0,
  11. 1, --多余变量1
  12. 2, --多余变量2
  13. setSex = function(self,sexValue)
  14. sex = sexValue;
  15. end,
  16. getSex = function()
  17. return sex;
  18. end
  19. }
  20. example_table2={
  21. id = 102,
  22. num = 25,
  23. vip = 3
  24. }
  25. example_table3 = {
  26. 1,
  27. 2,
  28. 3
  29. }
  30. --[[函数function]] --
  31. function Method1()
  32. print( '=====lua method1=====');
  33. end
  34. function Method2(a,b)
  35. print( '=====lua method2=====');
  36. c = a+b;
  37. a=a+ 2;
  38. b= 3;
  39. return c,a,b;
  40. end
  41. function Method3()
  42. print( '=====lua method3=====');
  43. return Method1;
  44. end

C#代码


  
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XLua;
  6. /*
  7. * Author:W
  8. * C#访问Lua中的数据变量,方法的方式
  9. */
  10. public class CSCallLuaTest : MonoBehaviour {
  11. private LuaEnv luaEnv = null;
  12. /// <summary>
  13. /// Lua表映射到Class类
  14. /// 【注意1】值拷贝-》修改不会同步 ,并且针对复杂类型,性能代价大
  15. /// 【注意2】不能映射Lua函数
  16. /// </summary>
  17. public class ExampleTable
  18. {
  19. public string id;
  20. public int age;
  21. public int sex;
  22. }
  23. private ExampleTable exampleTable;
  24. /// <summary>
  25. /// Lua表映射到struct
  26. /// 注意点与class相同
  27. /// </summary>
  28. public struct ExampleStruct
  29. {
  30. public string id;
  31. public int age;
  32. public int sex;
  33. }
  34. private ExampleStruct exampleStruct;
  35. /// <summary>
  36. /// Lua表映射到Interface
  37. /// 【注意1】可以映射Lua函数
  38. /// 【注意2】需要加上[CSharpCallLua]标签,并且点击“Xlua-》Generate Code”菜单命令生成Bride结尾的C#脚本代码,
  39. /// 在Assets/Xlua/Gen文件夹中可查看
  40. /// 【注意3】属于引用型映射,同步修改
  41. /// </summary>
  42. [ CSharpCallLua]
  43. public interface ExampleInterface
  44. {
  45. string id { get; set; }
  46. int age { get; set; }
  47. int sex { get; set; }
  48. void setSex(int sexV);
  49. int getSex();
  50. }
  51. private ExampleInterface exampleInterface;
  52. /// <summary>
  53. /// Lua表映射到字典Dictionary
  54. /// 【注意1】前提保证:键key与值Value的类型是一致的
  55. /// 【注意2】值拷贝
  56. /// </summary>
  57. public Dictionary<string, int> infoDict = new Dictionary< string, int>();
  58. /// <summary>
  59. /// Lua表映射到List
  60. /// 【注意1】前提保证:值Value的类型是一致的
  61. /// 【注意2】值拷贝
  62. /// </summary>
  63. public List<int> infoList = new List< int>();
  64. /// <summary>
  65. /// Lua表映射到LuaTable
  66. /// 【注意1】无需生成代码,性能比interface接口慢一个数量级,且无类型检查
  67. /// 【注意2】引用型映射,修改同步
  68. /// </summary>
  69. public LuaTable infoTable;
  70. Action method1;
  71. /// <summary>
  72. /// Lua Function函数映射到Delegate委托
  73. /// 【注意1】性能好,类型安全。但需要生成代码
  74. /// </summary>
  75. /// <param name="a"></param>
  76. /// <param name="b"></param>
  77. /// <param name="b2"></param>
  78. /// <param name="b3"></param>
  79. /// <returns></returns>
  80. [ CSharpCallLua]
  81. public delegate int Method2(int a, int b, ref int b2, out int b3);
  82. Method2 method2;
  83. /// <summary>
  84. /// Lua Function函数映射到Delegate委托,并返回另外一个委托
  85. /// </summary>
  86. /// <returns></returns>
  87. [ CSharpCallLua]
  88. public delegate Action Method3();
  89. Method3 method3;
  90. /// <summary>
  91. /// Lua 函数映射到LuaFunction
  92. /// 【注意1】不需要生成代码,性能代价较大
  93. /// </summary>
  94. private LuaFunction luaFunction;
  95. void Awake()
  96. {
  97. luaEnv = new LuaEnv();
  98. luaEnv.DoString( "require 'CSCallLua'");
  99. }
  100. // Use this for initialization
  101. void Start () {
  102. Debug.Log( "Lua Global Num="+ luaEnv.Global.Get< int>( "Num"));
  103. Debug.Log( "Lua Global Name="+luaEnv.Global.Get< string>( "Name"));
  104. Debug.Log( "Lua Global IsOk="+luaEnv.Global.Get< bool>( "IsOk"));
  105. exampleTable = luaEnv.Global.Get<ExampleTable>( "example_table");
  106. Debug.Log( "Lua ExampleTable 普通类映射 Id=" + exampleTable.id + " Age=" + exampleTable.age + " Sex=" + exampleTable.sex);
  107. exampleStruct = luaEnv.Global.Get<ExampleStruct>( "example_table");
  108. Debug.Log( "Lua ExampleTable 结构体映射 Id=" + exampleStruct.id + " Age=" + exampleStruct.age + " Sex=" + exampleStruct.sex);
  109. exampleInterface = luaEnv.Global.Get<ExampleInterface>( "example_table");
  110. Debug.Log( "Lua ExampleTable 接口映射 Id=" + exampleInterface.id + " Age=" + exampleInterface.age + " Sex=" + exampleInterface.sex);
  111. Debug.Log( "Lua ExampleTable 接口映射 函数setSex(5)");
  112. exampleInterface.setSex( 5);
  113. Debug.Log( "Lua ExampleTable 接口映射 函数 getSex():"+exampleInterface.getSex());
  114. infoDict = luaEnv.Global.Get<Dictionary< string, int>>( "example_table2");
  115. foreach (KeyValuePair< string, int> v in infoDict)
  116. {
  117. Debug.Log( "Lua ExampleTable2 字典映射 key="+v.Key+ " value="+v.Value);
  118. }
  119. infoList = luaEnv.Global.Get<List< int>>( "example_table3");
  120. for ( int i = 0; i < infoList.Count; i++)
  121. {
  122. Debug.Log( "Lua ExampleTable3 List映射 i="+i+ " value= "+infoList[i]);
  123. }
  124. //===============LuaTable==================
  125. infoTable = luaEnv.Global.Get<LuaTable>( "example_table");
  126. Debug.Log( "Lua ExampleTable LuaTable类 映射 Id = "+infoTable.Get< string>( "id")+ " Age="+infoTable.Get< int>( "age")+ " Sex="+infoTable.Get< int>( "sex"));
  127. //=======Delegate========
  128. method1 = luaEnv.Global.Get<Action>( "Method1");
  129. method1();
  130. method2 = luaEnv.Global.Get<Method2>( "Method2");
  131. int b2 = 0;
  132. int b3 = 0;
  133. int b1= method2( 2, 6, ref b2, out b3);
  134. Debug.Log( "Lua Function 函数映射method2 b1="+b1+ " b2="+b2+ " b3="+b3);
  135. method3 = luaEnv.Global.Get<Method3>( "Method3");
  136. Action res = method3();
  137. res();
  138. //======LuaFunction=======
  139. luaFunction = luaEnv.Global.Get<LuaFunction>( "Method2");
  140. object[] resArr = luaFunction.Call( new object[] { 1, 2 }, new Type[] { typeof( int), typeof( int), typeof( int)});
  141. Debug.Log( "Lua Function映射到LuaFunction函数:b1="+( int)resArr[ 0]+ " b2="+( int)resArr[ 1]+ " b3="+( int)resArr[ 2]);
  142. }
  143. // Update is called once per frame
  144. void Update () {
  145. if (luaEnv != null)
  146. luaEnv.Tick();
  147. }
  148. void OnDestroy()
  149. {
  150. if (luaEnv != null)
  151. luaEnv.Dispose();
  152. }
  153. }

运行结果如下

 


转载:https://blog.csdn.net/hppyW/article/details/116308587
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场