Ulua_toLua_基本案例(九)_LuaDictionary

Ulua_toLua_基本案例(九)_LuaDictionary



using UnityEngine;
using System.Collections.Generic;
using LuaInterface;

public class TestAccount
{
    public int id;
    public string name;
    public int sex;

    public TestAccount(int id, string name, int sex)
    {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }
}

public class UseDictionary : MonoBehaviour 
{
    Dictionary<int, TestAccount> map = new Dictionary<int, TestAccount>();

    string script =
        @"              
            function TestDict(map)                        
                local iter = map:GetEnumerator() 
                
                while iter:MoveNext() do
                    local v = iter.Current.Value
                    print('id: '..v.id ..' name: '..v.name..' sex: '..v.sex)                                
                end
            end                        
        ";

	void Awake () 
    {
        map.Add(1, new TestAccount(2, "隔壁老王", 2));
        map.Add(2, new TestAccount(1, "王伟", 1));
        map.Add(3, new TestAccount(2, "王芳", 0));
        
        LuaState luaState = new LuaState();
        luaState.Start();
        BindMap(luaState);        

        luaState.DoString(script);        
        LuaFunction func = luaState.GetFunction("TestDict");
        func.BeginPCall();
        func.Push(map);
        func.PCall();
        func.EndPCall();        

        func.Dispose();
        func = null;
        luaState.CheckTop();
        luaState.Dispose();
        luaState = null;    
	}

    //示例方式,正常导出无需手写下面代码
    void BindMap(LuaState L)
    {
        L.BeginModule(null);
        TestAccountWrap.Register(L);
        L.BeginModule("System");
        L.BeginModule("Collections");
        L.BeginModule("Generic");
        System_Collections_Generic_Dictionary_int_TestAccountWrap.Register(L);
        System_Collections_Generic_KeyValuePair_int_TestAccountWrap.Register(L);
        L.EndModule();
        L.EndModule();
        L.EndModule();
        L.EndModule();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值