CSCallLua.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Tencent is pleased to support the open source community by making xLua available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. using UnityEngine;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using XLua;
  12. using System;
  13. namespace Tutorial
  14. {
  15. public class CSCallLua : MonoBehaviour
  16. {
  17. LuaEnv luaenv = null;
  18. string script = @"
  19. a = 1
  20. b = 'hello world'
  21. c = true
  22. d = {
  23. f1 = 12, f2 = 34,
  24. 1, 2, 3,
  25. add = function(self, a, b)
  26. print('d.add called')
  27. return a + b
  28. end
  29. }
  30. function e()
  31. print('i am e')
  32. end
  33. function f(a, b)
  34. print('a', a, 'b', b)
  35. return 1, {f1 = 1024}
  36. end
  37. function ret_e()
  38. print('ret_e called')
  39. return e
  40. end
  41. ";
  42. public class DClass
  43. {
  44. public int f1;
  45. public int f2;
  46. }
  47. [CSharpCallLua]
  48. public interface ItfD
  49. {
  50. int f1 { get; set; }
  51. int f2 { get; set; }
  52. int add(int a, int b);
  53. }
  54. [CSharpCallLua]
  55. public delegate int FDelegate(int a, string b, out DClass c);
  56. [CSharpCallLua]
  57. public delegate Action GetE();
  58. // Use this for initialization
  59. void Start()
  60. {
  61. luaenv = new LuaEnv();
  62. luaenv.DoString(script);
  63. Debug.Log("_G.a = " + luaenv.Global.Get<int>("a"));
  64. Debug.Log("_G.b = " + luaenv.Global.Get<string>("b"));
  65. Debug.Log("_G.c = " + luaenv.Global.Get<bool>("c"));
  66. DClass d = luaenv.Global.Get<DClass>("d");//映射到有对应字段的class,by value
  67. Debug.Log("_G.d = {f1=" + d.f1 + ", f2=" + d.f2 + "}");
  68. Dictionary<string, double> d1 = luaenv.Global.Get<Dictionary<string, double>>("d");//映射到Dictionary<string, double>,by value
  69. Debug.Log("_G.d = {f1=" + d1["f1"] + ", f2=" + d1["f2"] + "}, d.Count=" + d1.Count);
  70. List<double> d2 = luaenv.Global.Get<List<double>>("d"); //映射到List<double>,by value
  71. Debug.Log("_G.d.len = " + d2.Count);
  72. ItfD d3 = luaenv.Global.Get<ItfD>("d"); //映射到interface实例,by ref,这个要求interface加到生成列表,否则会返回null,建议用法
  73. d3.f2 = 1000;
  74. Debug.Log("_G.d = {f1=" + d3.f1 + ", f2=" + d3.f2 + "}");
  75. Debug.Log("_G.d:add(1, 2)=" + d3.add(1, 2));
  76. LuaTable d4 = luaenv.Global.Get<LuaTable>("d");//映射到LuaTable,by ref
  77. Debug.Log("_G.d = {f1=" + d4.Get<int>("f1") + ", f2=" + d4.Get<int>("f2") + "}");
  78. Action e = luaenv.Global.Get<Action>("e");//映射到一个delgate,要求delegate加到生成列表,否则返回null,建议用法
  79. e();
  80. FDelegate f = luaenv.Global.Get<FDelegate>("f");
  81. DClass d_ret;
  82. int f_ret = f(100, "John", out d_ret);//lua的多返回值映射:从左往右映射到c#的输出参数,输出参数包括返回值,out参数,ref参数
  83. Debug.Log("ret.d = {f1=" + d_ret.f1 + ", f2=" + d_ret.f2 + "}, ret=" + f_ret);
  84. GetE ret_e = luaenv.Global.Get<GetE>("ret_e");//delegate可以返回更复杂的类型,甚至是另外一个delegate
  85. e = ret_e();
  86. e();
  87. LuaFunction d_e = luaenv.Global.Get<LuaFunction>("e");
  88. d_e.Call();
  89. }
  90. // Update is called once per frame
  91. void Update()
  92. {
  93. if (luaenv != null)
  94. {
  95. luaenv.Tick();
  96. }
  97. }
  98. void OnDestroy()
  99. {
  100. luaenv.Dispose();
  101. }
  102. }
  103. }