将lua的table表转化为C#HashTable


Lua的table 表格式

local myParams = {
animation = {
duration = 10.0;
time_function = "ease-in";
delay = 0;
islocal = true;
};


keyframes = {
[0] = {pos = {x = -85,y = -143,z = 0}, alpha = 1, scale = {x = 1,y=1,z=1}, rotate = {x=0,y=0,z=0}, color = {x = 255,y = 255,z = 255},};
[50] = {pos = {x = 0,y = 0,z = 0}, alpha = 0.4, scale = {x = 2,y=2,z=2}, rotate = {x=180,y=180,z=0}, color = {x = 255,y = 0,z = 0},};
[100]  = {pos = {x = 100,y = -143,z = 0}, alpha = 1, scale = {x = 1,y=1,z=1}, rotate = {x=0,y=0,z=0}, color = {x= 255,y = 255, z = 10},};
};
};


将之转换为HashTable格式


private int Lua_FlyAction(ILuaState lua)
        {
            LuaObject luaObj = LuaObject.GetLuaObject(lua, 2);
            if (luaObj == null)
                return 0;


            if (lua.Type(3) != LuaType.LUA_TTABLE)
            {
                return 0;
            }
            Hashtable hashTable = MoveControllerAdaptHasHTable(lua);


            moveController.DoFlyEffect(luaObj.transform, hashTable);

            return 0;
        }
        // zsy add
        private Hashtable MoveControllerAdaptHasHTable(ILuaState lua)
        {
            LuaObjWithCallFun callBackFun = new LuaObjWithCallFun();
            //print("*********** state len" + lua.GetTop());
            Hashtable hashTable = new Hashtable();
            lua.PushValue(3);
            lua.PushNil();
            while (lua.Next(-2))
            {
                string key = lua.L_CheckString(-2);
                switch (key)
                {
                    case "animation":
                        {
                            Hashtable animation = new Hashtable();
                            lua.PushValue(-1);
                            lua.PushNil();


                            float value = 0;
                            string strtype = "ease";
                            bool blean = true;


                            while (lua.Next(-2))
                            {
                                string childkey = lua.L_CheckString(-2);
                                switch (lua.Type(-1))
                                {
                                    case LuaType.LUA_TNUMBER:
                                        value = (float)lua.L_CheckNumber(-1);
                                        animation.Add(childkey, value);
                                        break;
                                    case LuaType.LUA_TBOOLEAN :
                                        blean = lua.ToBoolean(-1);
                                        animation.Add(childkey, blean);
                                        break;
                                    case LuaType.LUA_TSTRING:
                                        strtype = lua.L_CheckString(-1);
                                        animation.Add(childkey, strtype);
                                        break;
                                    default:
                                        break;
                                }
                                lua.Pop(1);
                            }
                            lua.Pop(1);


                            hashTable.Add(key, animation);
                            break;
                        }
                    case "keyframes":
                        {
                            Hashtable keyframes = new Hashtable();
                            lua.PushValue(-1);
                            lua.PushNil();


                            int numkey = -1;
                            string strkey = "from";


                            while (lua.Next(-2))
                            {
                                Hashtable tempHash = new Hashtable();
                                if (lua.Type(-1) == LuaType.LUA_TTABLE)
                                {
                                    lua.PushValue(-1);
                                    lua.PushNil();
                                    while (lua.Next(-2))
                                    {
                                        string child2key = lua.L_CheckString(-2);
                                        switch (child2key)
                                        {
                                            case "pos":
                                            case "scale":
                                            case "rotate":
                                            case "color":
                                                Vector3 vecValue = GetVector3(lua, -1);
                                                tempHash.Add(child2key, vecValue);
                                                break;                                                
                                            case "alpha":
                                                float alpha = (float)lua.L_CheckNumber(-1);
                                                tempHash.Add(child2key, alpha);
                                                break;
                                            default:
                                                break;
                                        }
                                        lua.Pop(1);
                                    }
                                    lua.Pop(1);
                                }
                                if (lua.Type(-2) == LuaType.LUA_TSTRING)
                                {
                                    strkey = lua.L_CheckString(-2);
                                    keyframes.Add(strkey, tempHash);
                                }
                                else if (lua.Type(-2) == LuaType.LUA_TNUMBER)
                                {
                                    numkey = (int)lua.L_CheckInteger(-2);
                                    keyframes.Add(numkey, tempHash);
                                }
                                lua.Pop(1);
                            }
                            lua.Pop(1);


                            hashTable.Add(key, keyframes);
                            break;
                        }
                    default:
                        break;


                }
                lua.Pop(1);
            }
            lua.Pop(1);
            //print("***********AAAAAAA state len" + lua.GetTop());
            return hashTable;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值