TestInherit.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. public class TestInherit : MonoBehaviour
  5. {
  6. private string script =
  7. @" LuaTransform =
  8. {
  9. }
  10. function LuaTransform.Extend(u)
  11. local t = {}
  12. local _position = u.position
  13. tolua.setpeer(u, t)
  14. t.__index = t
  15. local get = tolua.initget(t)
  16. local set = tolua.initset(t)
  17. local _base = u.base
  18. --重写同名属性获取
  19. get.position = function(self)
  20. return _position
  21. end
  22. --重写同名属性设置
  23. set.position = function(self, v)
  24. if _position ~= v then
  25. _position = v
  26. _base.position = v
  27. end
  28. end
  29. --重写同名函数
  30. function t:Translate(...)
  31. print('child Translate')
  32. _base:Translate(...)
  33. end
  34. return u
  35. end
  36. --既保证支持继承函数,又支持go.transform == transform 这样的比较
  37. function Test(node)
  38. local v = Vector3.one
  39. local transform = LuaTransform.Extend(node)
  40. --local transform = node
  41. local t = os.clock()
  42. for i = 1, 200000 do
  43. transform.position = transform.position
  44. end
  45. print('LuaTransform get set cost', os.clock() - t)
  46. transform:Translate(1,1,1)
  47. local child = transform:FindChild('child')
  48. print('child is: ', tostring(child))
  49. if child.parent == transform then
  50. print('LuaTransform compare to userdata transform is ok')
  51. end
  52. transform.xyz = 123
  53. transform.xyz = 456
  54. print('extern field xyz is: '.. transform.xyz)
  55. end
  56. ";
  57. LuaState lua = null;
  58. void Start ()
  59. {
  60. lua = new LuaState();
  61. lua.Start();
  62. LuaBinder.Bind(lua);
  63. lua.DoString(script, "TestInherit.cs");
  64. float time = Time.realtimeSinceStartup;
  65. for (int i = 0; i < 200000; i++)
  66. {
  67. Vector3 v = transform.position;
  68. transform.position = v;
  69. }
  70. time = Time.realtimeSinceStartup - time;
  71. Debugger.Log("c# Transform get set cost time: " + time);
  72. LuaFunction func = lua.GetFunction("Test");
  73. func.BeginPCall();
  74. func.Push(transform);
  75. func.PCall();
  76. func.EndPCall();
  77. lua.CheckTop();
  78. lua.Dispose();
  79. lua = null;
  80. }
  81. }

?