ToLua lua对c#字典的访问
private string luaString = @"
	function test(dic)
		
		iter = dic:GetEnumerator() 
		while iter:MoveNext() do --遍历字典
			print('key'.. iter.Current.Key .. 'value' .. iter.Current.Value)
		end

		keys = dic.Keys --获取字典的键值数组
		values = dic.Values  --获取字典值的数组
		iter = values:GetEnumerator()
		while iter:MoveNext() do
		print(iter.Current)
		end

		local hehe,val = dic:TryGetValue(1,nil)  --尝试获取键的值,第一个返回状态,第二个才是值
		print(val)

		dic:Remove(2) --移除键值为2的元素
		--跟C#字典的操作一样
	end

";

private Dictionary<int,string> dicc = new Dictionary<int, string>();

void Start () {
	LuaState lua = new LuaState ();
	lua.Start ();
	lua.DoString (luaString);
	dicc.Add (1, "hello");
	dicc.Add (2, "world");

	LuaFunction func = lua ["test"] as LuaFunction;
	func.BeginPCall ();
	func.Push (dicc);
	func.PCall ();
	func.EndPCall ();

	foreach (var item in dicc) {
		Debug.Log (item.Value); //只输出了hello
	}

	func.Dispose ();
	lua.CheckTop ();
	lua.Dispose ();
}



首页 我的博客
粤ICP备17103704号