- ToLua的Example示例学习笔记08_AccessingA
- ToLua的Example示例学习笔记_总集篇
- ToLua的Example示例学习笔记03_CallLuaFun
- ToLua的Example示例学习笔记13_CustomLoad
- ToLua的Example示例学习笔记22_UseList
- ToLua的Example示例学习笔记11_Delegate
- ToLua的Example示例学习笔记02_ScriptsFro
- ToLua的Example示例学习笔记05_LuaCorouti
- ToLua的Example示例学习笔记04_AccessingL
- ToLua的Example示例学习笔记09_Dictionary
展示了Lua对C#中的数组对象的几种基本访问方法。
「1」代码
操作代码如下:
function TestArray(array)
local len = array.Length
for i = 0, len - 1 do
print('Array: '..tostring(array[i]))
end
local iter = array:GetEnumerator()
while iter:MoveNext() do
print('iter: '..iter.Current)
end
local t = array:ToTable()
for i = 1, #t do
print('table: '.. tostring(t[i]))
end
local pos = array:BinarySearch(3)
print('array BinarySearch: pos: '..pos..' value: '..array[pos])
pos = array:IndexOf(4)
print('array indexof bbb pos is: '..pos)
return 1, '123', true
end
c#代码如下:
new LuaResLoader();
lua = new LuaState();
lua.Start();
lua.DoString(script, "AccessingArray.cs");
tips = "";
int[] array = { 1, 2, 3, 4, 5 };
func = lua.GetFunction("TestArray");
func.BeginPCall();
func.Push(array);
func.PCall();
double arg1 = func.CheckNumber();
string arg2 = func.CheckString();
bool arg3 = func.CheckBoolean();
Debugger.Log("return is {0} {1} {2}", arg1, arg2, arg3);
func.EndPCall();
//调用通用函数需要转换一下类型,避免可变参数拆成多个参数传递
object[] objs = func.LazyCall((object)array);
if (objs != null)
{
Debugger.Log("return is {0} {1} {2}", objs[0], objs[1], objs[2]);
}
lua.CheckTop();
「2」需要了解的部分
-
tostring(array[i])
将数组中的值作为字符串输出 -
通过迭代器iter的Current和Movenext来获取当前和下一个,从而遍历。
local iter = array:GetEnumerator()
while iter:MoveNext() do
print('iter: '..iter.Current)
end
- 通过
local t = array:ToTable()
将数组转换为table的形式,使用array.IndexOf(value)和array:BinarySearch(value)的方法。
「3」值得注意的方法
-
LuaFunction.LazyCall((object)array);
调用通用函数需要转换一下类型,避免可变参数拆成多个参数按序传递。
网友评论