ReImplementInLua.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. using System.Collections;
  3. using XLua;
  4. namespace XLuaTest
  5. {
  6. [GCOptimize(OptimizeFlag.PackAsTable)]
  7. public struct PushAsTableStruct
  8. {
  9. public int x;
  10. public int y;
  11. }
  12. public class ReImplementInLua : MonoBehaviour
  13. {
  14. // Use this for initialization
  15. void Start()
  16. {
  17. LuaEnv luaenv = new LuaEnv();
  18. //这两个例子都必须生成代码才能正常运行
  19. //例子1:改造Vector3
  20. //沿用Vector3原来的映射方案Vector3 -> userdata,但是把Vector3的方法实现改为lua实现,通过xlua.genaccessor实现不经过C#直接操作内存
  21. //改为不经过C#的好处是性能更高,而且你可以省掉相应的生成代码以达成省text段的效果
  22. //仍然沿用映射方案的好处是userdata比table更省内存,但操作字段比table性能稍低,当然,你也可以结合例子2的思路,把Vector3也改为映射到table
  23. luaenv.DoString(@"
  24. function test_vector3(title, v1, v2)
  25. print(title)
  26. print(v1.x, v1.y, v1.z)
  27. print(v1, v2)
  28. print(v1 + v2)
  29. v1:Set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
  30. print(v1)
  31. print(CS.UnityEngine.Vector3.Normalize(v1))
  32. end
  33. test_vector3('----before change metatable----', CS.UnityEngine.Vector3(1, 2, 3), CS.UnityEngine.Vector3(7, 8, 9))
  34. local get_x, set_x = xlua.genaccessor(0, 8)
  35. local get_y, set_y = xlua.genaccessor(4, 8)
  36. local get_z, set_z = xlua.genaccessor(8, 8)
  37. local fields_getters = {
  38. x = get_x, y = get_y, z = get_z
  39. }
  40. local fields_setters = {
  41. x = set_x, y = set_y, z = set_z
  42. }
  43. local ins_methods = {
  44. Set = function(o, x, y, z)
  45. set_x(o, x)
  46. set_y(o, y)
  47. set_z(o, z)
  48. end
  49. }
  50. local mt = {
  51. __index = function(o, k)
  52. --print('__index', k)
  53. if ins_methods[k] then return ins_methods[k] end
  54. return fields_getters[k] and fields_getters[k](o)
  55. end,
  56. __newindex = function(o, k, v)
  57. return fields_setters[k] and fields_setters[k](o, v) or error('no such field ' .. k)
  58. end,
  59. __tostring = function(o)
  60. return string.format('vector3 { %f, %f, %f}', o.x, o.y, o.z)
  61. end,
  62. __add = function(a, b)
  63. return CS.UnityEngine.Vector3(a.x + b.x, a.y + b.y, a.z + b.z)
  64. end
  65. }
  66. xlua.setmetatable(CS.UnityEngine.Vector3, mt)
  67. test_vector3('----after change metatable----', CS.UnityEngine.Vector3(1, 2, 3), CS.UnityEngine.Vector3(7, 8, 9))
  68. ");
  69. Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  70. //例子2:struct映射到table改造
  71. //PushAsTableStruct传送到lua侧将会是table,例子里头还为这个table添加了一个成员方法SwapXY,静态方法Print,打印格式化,以及构造函数
  72. luaenv.DoString(@"
  73. local mt = {
  74. __index = {
  75. SwapXY = function(o) --成员函数
  76. o.x, o.y = o.y, o.x
  77. end
  78. },
  79. __tostring = function(o) --打印格式化函数
  80. return string.format('struct { %d, %d}', o.x, o.y)
  81. end,
  82. }
  83. xlua.setmetatable(CS.XLuaTest.PushAsTableStruct, mt)
  84. local PushAsTableStruct = {
  85. Print = function(o) --静态函数
  86. print(o.x, o.y)
  87. end
  88. }
  89. setmetatable(PushAsTableStruct, {
  90. __call = function(_, x, y) --构造函数
  91. return setmetatable({x = x, y = y}, mt)
  92. end
  93. })
  94. xlua.setclass(CS.XLuaTest, 'PushAsTableStruct', PushAsTableStruct)
  95. ");
  96. PushAsTableStruct test;
  97. test.x = 100;
  98. test.y = 200;
  99. luaenv.Global.Set("from_cs", test);
  100. luaenv.DoString(@"
  101. print('--------------from csharp---------------------')
  102. assert(type(from_cs) == 'table')
  103. print(from_cs)
  104. CS.XLuaTest.PushAsTableStruct.Print(from_cs)
  105. from_cs:SwapXY()
  106. print(from_cs)
  107. print('--------------from lua---------------------')
  108. local from_lua = CS.XLuaTest.PushAsTableStruct(4, 5)
  109. assert(type(from_lua) == 'table')
  110. print(from_lua)
  111. CS.XLuaTest.PushAsTableStruct.Print(from_lua)
  112. from_lua:SwapXY()
  113. print(from_lua)
  114. ");
  115. luaenv.Dispose();
  116. }
  117. // Update is called once per frame
  118. void Update()
  119. {
  120. }
  121. }
  122. }