NoGc

NoGc.cs

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

?