CS调用Lua


  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using XLua;
  5. using System;
  6. public class CSCallLua : MonoBehaviour {
  7. LuaEnv luaenv = null;
  8. string script = @"
  9. a = 1
  10. b = 'hello world'
  11. c = true
  12. d = {
  13. f1 = 12, f2 = 34,
  14. 1, 2, 3,
  15. add = function(self, a, b)
  16. print('d.add called')
  17. return a + b
  18. end
  19. }
  20. function e()
  21. print('i am e')
  22. end
  23. function f(a, b)
  24. print('a', a, 'b', b)
  25. return 1, {f1 = 1024}
  26. end
  27. function ret_e()
  28. print('ret_e called')
  29. return e
  30. end
  31. ";
  32. public class DClass
  33. {
  34. public int f1;
  35. public int f2;
  36. }
  37. [CSharpCallLua]
  38. public interface ItfD
  39. {
  40. int f1 { get; set; }
  41. int f2 { get; set; }
  42. int add(int a, int b);
  43. }
  44. [CSharpCallLua]
  45. public delegate int FDelegate(int a, string b, out DClass c);
  46. [CSharpCallLua]
  47. public delegate Action GetE();
  48. // Use this for initialization
  49. void Start()
  50. {
  51. luaenv = new LuaEnv();
  52. luaenv.DoString(script);
  53. Debug.Log("_G.a = " + luaenv.Global.Get<int>("a"));
  54. Debug.Log("_G.b = " + luaenv.Global.Get<string>("b"));
  55. Debug.Log("_G.c = " + luaenv.Global.Get<bool>("c"));
  56. DClass d = luaenv.Global.Get<DClass>("d");//映射到有对应字段的class,by value
  57. Debug.Log("_G.d = {f1=" + d.f1 + ", f2=" + d.f2 + "}");
  58. Dictionary<string, double> d1 = luaenv.Global.Get<Dictionary<string, double>>("d");//映射到Dictionary<string, double>,by value
  59. Debug.Log("_G.d = {f1=" + d1["f1"] + ", f2=" + d1["f2"] + "}, d.Count=" + d1.Count);
  60. List<double> d2 = luaenv.Global.Get<List<double>>("d"); //映射到List<double>,by value
  61. Debug.Log("_G.d.len = " + d2.Count);
  62. ItfD d3 = luaenv.Global.Get<ItfD>("d"); //映射到interface实例,by ref,这个要求interface加到生成列表,否则会返回null,建议用法
  63. d3.f2 = 1000;
  64. Debug.Log("_G.d = {f1=" + d3.f1 + ", f2=" + d3.f2 + "}");
  65. Debug.Log("_G.d:add(1, 2)=" + d3.add(1, 2));
  66. LuaTable d4 = luaenv.Global.Get<LuaTable>("d");//映射到LuaTable,by ref
  67. Debug.Log("_G.d = {f1=" + d4.Get<int>("f1") + ", f2=" + d4.Get<int>("f2") + "}");
  68. Action e = luaenv.Global.Get<Action>("e");//映射到一个delgate,要求delegate加到生成列表,否则返回null,建议用法
  69. e();
  70. FDelegate f = luaenv.Global.Get<FDelegate>("f");
  71. DClass d_ret;
  72. int f_ret = f(100, "John", out d_ret);//lua的多返回值映射:从左往右映射到c#的输出参数,输出参数包括返回值,out参数,ref参数
  73. Debug.Log("ret.d = {f1=" + d_ret.f1 + ", f2=" + d_ret.f2 + "}, ret=" + f_ret);
  74. GetE ret_e = luaenv.Global.Get<GetE>("ret_e");//delegate可以返回更复杂的类型,甚至是另外一个delegate
  75. e = ret_e();
  76. e();
  77. LuaFunction d_e = luaenv.Global.Get<LuaFunction>("e");
  78. d_e.Call();
  79. }
  80. // Update is called once per frame
  81. void Update()
  82. {
  83. if (luaenv != null)
  84. {
  85. luaenv.Tick();
  86. }
  87. }
  88. void OnDestroy()
  89. {
  90. luaenv.Dispose();
  91. }
  92. }

?