UseList

  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. using System.Collections.Generic;
  5. using System;
  6. //需要导出委托类型如下:
  7. //System.Predicate<int>
  8. //System.Action<int>
  9. //System.Comparison<int>
  10. public class UseList : LuaClient
  11. {
  12. private string script =
  13. @"
  14. function Exist2(v)
  15. return v == 2
  16. end
  17. function IsEven(v)
  18. return v % 2 == 0
  19. end
  20. function NotExist(v)
  21. return false
  22. end
  23. function Compare(a, b)
  24. if a > b then
  25. return 1
  26. elseif a == b then
  27. return 0
  28. else
  29. return -1
  30. end
  31. end
  32. function Test(list, list1)
  33. list:Add(123)
  34. print('Add result: list[0] is '..list[0])
  35. list:AddRange(list1)
  36. print(string.format('AddRange result: list[1] is %d, list[2] is %d', list[1], list[2]))
  37. local const = list:AsReadOnly()
  38. print('AsReadOnley:'..const[0])
  39. index = const:IndexOf(123)
  40. if index == 0 then
  41. print('const IndexOf is ok')
  42. end
  43. local pos = list:BinarySearch(1)
  44. print('BinarySearch 1 result is: '..pos)
  45. if list:Contains(123) then
  46. print('list Contain 123')
  47. else
  48. error('list Contains result fail')
  49. end
  50. if list:Exists(Exist2) then
  51. print('list exists 2')
  52. else
  53. error('list exists result fail')
  54. end
  55. if list:Find(Exist2) then
  56. print('list Find is ok')
  57. else
  58. print('list Find error')
  59. end
  60. local fa = list:FindAll(IsEven)
  61. if fa.Count == 2 then
  62. print('FindAll is ok')
  63. end
  64. --注意推导后的委托声明必须注册, 这里是System.Predicate<int>
  65. local index = list:FindIndex(System.Predicate_int(Exist2))
  66. if index == 2 then
  67. print('FindIndex is ok')
  68. end
  69. index = list:FindLastIndex(System.Predicate_int(Exist2))
  70. if index == 2 then
  71. print('FindLastIndex is ok')
  72. end
  73. index = list:IndexOf(123)
  74. if index == 0 then
  75. print('IndexOf is ok')
  76. end
  77. index = list:LastIndexOf(123)
  78. if index == 0 then
  79. print('LastIndexOf is ok')
  80. end
  81. list:Remove(123)
  82. if list[0] ~= 123 then
  83. print('Remove is ok')
  84. end
  85. list:Insert(0, 123)
  86. if list[0] == 123 then
  87. print('Insert is ok')
  88. end
  89. list:RemoveAt(0)
  90. if list[0] ~= 123 then
  91. print('RemoveAt is ok')
  92. end
  93. list:Insert(0, 123)
  94. list:ForEach(function(v) print('foreach: '..v) end)
  95. local count = list.Count
  96. list:Sort(System.Comparison_int(Compare))
  97. print('--------------sort list over----------------------')
  98. for i = 0, count - 1 do
  99. print('for:'..list[i])
  100. end
  101. list:Clear()
  102. print('list Clear not count is '..list.Count)
  103. end
  104. ";
  105. protected override LuaFileUtils InitLoader()
  106. {
  107. return new LuaResLoader();
  108. }
  109. //屏蔽,例子不需要运行
  110. protected override void CallMain() { }
  111. protected override void OnLoadFinished()
  112. {
  113. base.OnLoadFinished();
  114. luaState.DoString(script, "UseList.cs");
  115. List<int> list1 = new List<int>();
  116. list1.Add(1);
  117. list1.Add(2);
  118. list1.Add(4);
  119. LuaFunction func = luaState.GetFunction("Test");
  120. func.BeginPCall();
  121. func.Push(new List<int>());
  122. func.Push(list1);
  123. func.PCall();
  124. func.EndPCall();
  125. func.Dispose();
  126. func = null;
  127. }
  128. }

?