InvokeLua.cs 4.5 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 System;
  9. using UnityEngine;
  10. using XLua;
  11. namespace XLuaTest
  12. {
  13. public class PropertyChangedEventArgs : EventArgs
  14. {
  15. public string name;
  16. public object value;
  17. }
  18. public class InvokeLua : MonoBehaviour
  19. {
  20. [CSharpCallLua]
  21. public interface ICalc
  22. {
  23. event EventHandler<PropertyChangedEventArgs> PropertyChanged;
  24. int Add(int a, int b);
  25. int Mult { get; set; }
  26. object this[int index] { get; set; }
  27. }
  28. [CSharpCallLua]
  29. public delegate ICalc CalcNew(int mult, params string[] args);
  30. private string script = @"
  31. local calc_mt = {
  32. __index = {
  33. Add = function(self, a, b)
  34. return (a + b) * self.Mult
  35. end,
  36. get_Item = function(self, index)
  37. return self.list[index + 1]
  38. end,
  39. set_Item = function(self, index, value)
  40. self.list[index + 1] = value
  41. self:notify({name = index, value = value})
  42. end,
  43. add_PropertyChanged = function(self, delegate)
  44. if self.notifylist == nil then
  45. self.notifylist = {}
  46. end
  47. table.insert(self.notifylist, delegate)
  48. print('add',delegate)
  49. end,
  50. remove_PropertyChanged = function(self, delegate)
  51. for i=1, #self.notifylist do
  52. if CS.System.Object.Equals(self.notifylist[i], delegate) then
  53. table.remove(self.notifylist, i)
  54. break
  55. end
  56. end
  57. print('remove', delegate)
  58. end,
  59. notify = function(self, evt)
  60. if self.notifylist ~= nil then
  61. for i=1, #self.notifylist do
  62. self.notifylist[i](self, evt)
  63. end
  64. end
  65. end,
  66. }
  67. }
  68. Calc = {
  69. New = function (mult, ...)
  70. print(...)
  71. return setmetatable({Mult = mult, list = {'aaaa','bbbb','cccc'}}, calc_mt)
  72. end
  73. }
  74. ";
  75. // Use this for initialization
  76. void Start()
  77. {
  78. LuaEnv luaenv = new LuaEnv();
  79. Test(luaenv);//调用了带可变参数的delegate,函数结束都不会释放delegate,即使置空并调用GC
  80. luaenv.Dispose();
  81. }
  82. void Test(LuaEnv luaenv)
  83. {
  84. luaenv.DoString(script);
  85. CalcNew calc_new = luaenv.Global.GetInPath<CalcNew>("Calc.New");
  86. ICalc calc = calc_new(10, "hi", "john"); //constructor
  87. Debug.Log("sum(*10) =" + calc.Add(1, 2));
  88. calc.Mult = 100;
  89. Debug.Log("sum(*100)=" + calc.Add(1, 2));
  90. Debug.Log("list[0]=" + calc[0]);
  91. Debug.Log("list[1]=" + calc[1]);
  92. calc.PropertyChanged += Notify;
  93. calc[1] = "dddd";
  94. Debug.Log("list[1]=" + calc[1]);
  95. calc.PropertyChanged -= Notify;
  96. calc[1] = "eeee";
  97. Debug.Log("list[1]=" + calc[1]);
  98. }
  99. void Notify(object sender, PropertyChangedEventArgs e)
  100. {
  101. Debug.Log(string.Format("{0} has property changed {1}={2}", sender, e.name, e.value));
  102. }
  103. // Update is called once per frame
  104. void Update()
  105. {
  106. }
  107. }
  108. }