NoGc.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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;
  10. using XLua;
  11. namespace XLuaTest
  12. {
  13. [GCOptimize]
  14. [LuaCallCSharp]
  15. public struct Pedding
  16. {
  17. public byte c;
  18. }
  19. [GCOptimize]
  20. [LuaCallCSharp]
  21. public struct MyStruct
  22. {
  23. public MyStruct(int p1, int p2)
  24. {
  25. a = p1;
  26. b = p2;
  27. c = p2;
  28. e.c = (byte)p1;
  29. }
  30. public int a;
  31. public int b;
  32. public decimal c;
  33. public Pedding e;
  34. }
  35. [LuaCallCSharp]
  36. public enum MyEnum
  37. {
  38. E1,
  39. E2
  40. }
  41. [CSharpCallLua]
  42. public delegate int IntParam(int p);
  43. [CSharpCallLua]
  44. public delegate Vector3 Vector3Param(Vector3 p);
  45. [CSharpCallLua]
  46. public delegate MyStruct CustomValueTypeParam(MyStruct p);
  47. [CSharpCallLua]
  48. public delegate MyEnum EnumParam(MyEnum p);
  49. [CSharpCallLua]
  50. public delegate decimal DecimalParam(decimal p);
  51. [CSharpCallLua]
  52. public delegate void ArrayAccess(Array arr);
  53. [CSharpCallLua]
  54. public interface IExchanger
  55. {
  56. void exchange(Array arr);
  57. }
  58. [LuaCallCSharp]
  59. public class NoGc : MonoBehaviour
  60. {
  61. LuaEnv luaenv = new LuaEnv();
  62. IntParam f1;
  63. Vector3Param f2;
  64. CustomValueTypeParam f3;
  65. EnumParam f4;
  66. DecimalParam f5;
  67. ArrayAccess farr;
  68. Action flua;
  69. IExchanger ie;
  70. LuaFunction add;
  71. [NonSerialized]
  72. public double[] a1 = new double[] { 1, 2 };
  73. [NonSerialized]
  74. public Vector3[] a2 = new Vector3[] { new Vector3(1, 2, 3), new Vector3(4, 5, 6) };
  75. [NonSerialized]
  76. public MyStruct[] a3 = new MyStruct[] { new MyStruct(1, 2), new MyStruct(3, 4) };
  77. [NonSerialized]
  78. public MyEnum[] a4 = new MyEnum[] { MyEnum.E1, MyEnum.E2 };
  79. [NonSerialized]
  80. public decimal[] a5 = new decimal[] { 1.00001M, 2.00002M };
  81. public float FloatParamMethod(float p)
  82. {
  83. return p;
  84. }
  85. public Vector3 Vector3ParamMethod(Vector3 p)
  86. {
  87. return p;
  88. }
  89. public MyStruct StructParamMethod(MyStruct p)
  90. {
  91. return p;
  92. }
  93. public MyEnum EnumParamMethod(MyEnum p)
  94. {
  95. return p;
  96. }
  97. public decimal DecimalParamMethod(decimal p)
  98. {
  99. return p;
  100. }
  101. // Use this for initialization
  102. void Start()
  103. {
  104. luaenv.DoString(@"
  105. function id(...)
  106. return ...
  107. end
  108. function add(a, b) return a + b end
  109. function array_exchange(arr)
  110. arr[0], arr[1] = arr[1], arr[0]
  111. end
  112. local v3 = CS.UnityEngine.Vector3(7, 8, 9)
  113. local vt = CS.XLuaTest.MyStruct(5, 6)
  114. function lua_access_csharp()
  115. monoBehaviour:FloatParamMethod(123) --primitive
  116. monoBehaviour:Vector3ParamMethod(v3) --vector3
  117. local rnd = math.random(1, 100)
  118. local r = monoBehaviour:Vector3ParamMethod({x = 1, y = 2, z = rnd}) --vector3
  119. assert(r.x == 1 and r.y == 2 and r.z == rnd)
  120. monoBehaviour:StructParamMethod(vt) --custom struct
  121. r = monoBehaviour:StructParamMethod({a = 1, b = rnd, e = {c = rnd}})
  122. assert(r.b == rnd and r.e.c == rnd)
  123. monoBehaviour:EnumParamMethod(CS.XLuaTest.MyEnum.E2) --enum
  124. monoBehaviour:DecimalParamMethod(monoBehaviour.a5[0])
  125. monoBehaviour.a1[0], monoBehaviour.a1[1] = monoBehaviour.a1[1], monoBehaviour.a1[0] -- field
  126. end
  127. exchanger = {
  128. exchange = function(self, arr)
  129. array_exchange(arr)
  130. end
  131. }
  132. A = { B = { C = 789}}
  133. GDATA = 1234;
  134. ");
  135. luaenv.Global.Set("monoBehaviour", this);
  136. luaenv.Global.Get("id", out f1);
  137. luaenv.Global.Get("id", out f2);
  138. luaenv.Global.Get("id", out f3);
  139. luaenv.Global.Get("id", out f4);
  140. luaenv.Global.Get("id", out f5);
  141. luaenv.Global.Get("array_exchange", out farr);
  142. luaenv.Global.Get("lua_access_csharp", out flua);
  143. luaenv.Global.Get("exchanger", out ie);
  144. luaenv.Global.Get("add", out add);
  145. luaenv.Global.Set("g_int", 123);
  146. luaenv.Global.Set(123, 456);
  147. int i;
  148. luaenv.Global.Get("g_int", out i);
  149. Debug.Log("g_int:" + i);
  150. luaenv.Global.Get(123, out i);
  151. Debug.Log("123:" + i);
  152. }
  153. // Update is called once per frame
  154. void Update()
  155. {
  156. // c# call lua function with value type but no gc (using delegate)
  157. f1(1); // primitive type
  158. f2(new Vector3(1, 2, 3)); // vector3
  159. MyStruct mystruct1 = new MyStruct(5, 6);
  160. f3(mystruct1); // custom complex value type
  161. f4(MyEnum.E1); //enum
  162. decimal dec1 = -32132143143100109.00010001010M;
  163. f5(dec1); //decimal
  164. // using LuaFunction.Func<T1, T2, TResult>
  165. add.Func<int, int, int>(34, 56); // LuaFunction.Func<T1, T2, TResult>
  166. // lua access c# value type array no gc
  167. farr(a1); //primitive value type array
  168. farr(a2); //vector3 array
  169. farr(a3); //custom struct array
  170. farr(a4); //enum arry
  171. farr(a5); //decimal arry
  172. // lua call c# no gc with value type
  173. flua();
  174. //c# call lua using interface
  175. ie.exchange(a2);
  176. //no gc LuaTable use
  177. luaenv.Global.Set("g_int", 456);
  178. int i;
  179. luaenv.Global.Get("g_int", out i);
  180. luaenv.Global.Set(123.0001, mystruct1);
  181. MyStruct mystruct2;
  182. luaenv.Global.Get(123.0001, out mystruct2);
  183. decimal dec2 = 0.0000001M;
  184. luaenv.Global.Set((byte)12, dec1);
  185. luaenv.Global.Get((byte)12, out dec2);
  186. int gdata = luaenv.Global.Get<int>("GDATA");
  187. luaenv.Global.SetInPath("GDATA", gdata + 1);
  188. int abc = luaenv.Global.GetInPath<int>("A.B.C");
  189. luaenv.Global.SetInPath("A.B.C", abc + 1);
  190. luaenv.Tick();
  191. }
  192. void OnDestroy()
  193. {
  194. f1 = null;
  195. f2 = null;
  196. f3 = null;
  197. f4 = null;
  198. f5 = null;
  199. farr = null;
  200. flua = null;
  201. ie = null;
  202. add = null;
  203. luaenv.Dispose();
  204. }
  205. }
  206. }