访问数组

AccessingArray.cs

  1. using UnityEngine;
  2. using LuaInterface;
  3. public class AccessingArray : MonoBehaviour
  4. {
  5. private string script =
  6. @"
  7. function TestArray(array)
  8. local len = array.Length
  9. for i = 0, len - 1 do
  10. print('Array: '..tostring(array[i]))
  11. end
  12. local t = array:ToTable()
  13. for i = 1, #t do
  14. print('table: '.. tostring(t[i]))
  15. end
  16. local iter = array:GetEnumerator()
  17. while iter:MoveNext() do
  18. print('iter: '..iter.Current)
  19. end
  20. local pos = array:BinarySearch(3)
  21. print('array BinarySearch: pos: '..pos..' value: '..array[pos])
  22. pos = array:IndexOf(4)
  23. print('array indexof bbb pos is: '..pos)
  24. return 1, '123', true
  25. end
  26. ";
  27. LuaState lua = null;
  28. LuaFunction func = null;
  29. void Start()
  30. {
  31. lua = new LuaState();
  32. lua.Start();
  33. lua.DoString(script, "AccessingArray.cs");
  34. int[] array = { 1, 2, 3, 4, 5};
  35. func = lua.GetFunction("TestArray");
  36. func.BeginPCall();
  37. func.Push(array);
  38. func.PCall();
  39. double arg1 = func.CheckNumber();
  40. string arg2 = func.CheckString();
  41. bool arg3 = func.CheckBoolean();
  42. Debugger.Log("return is {0} {1} {2}", arg1, arg2, arg3);
  43. func.EndPCall();
  44. //转换一下类型,避免可变参数拆成多个参数传递
  45. object[] objs = func.Call((object)array);
  46. if (objs != null)
  47. {
  48. Debugger.Log("return is {0} {1} {2}", objs[0], objs[1], objs[2]);
  49. }
  50. lua.CheckTop();
  51. }
  52. void OnApplicationQuit()
  53. {
  54. func.Dispose();
  55. lua.Dispose();
  56. }
  57. }

?