Пример #1
0
        public void PushCSObject(object o)
        {
            if (o == null)
            {
                LuaLib.lua_pushnil(L);
                return;
            }

            int refId;

            if (!_objectUserdataRefIds.TryGetValue(o, out refId))
            {
                LuaBindMetatable metatable = Bindings.GetMetatable(o.GetType(), LuaBindMetatableType.Instance);

                int objectUserdataValue = _newObjectUserdataValue++;
                LuaLib.wutlua_newuserdata(L, objectUserdataValue);                              // |ud
                metatable.Push();                                                               // |ud|mt
                LuaLib.lua_setmetatable(L, -2);                                                 // |ud		// ud.metatable = mt

                refId = LuaLib.luaL_ref(L, LuaIndices.LUA_REGISTRYINDEX);                       // |

                _objects[objectUserdataValue] = o;
                _objectUserdataRefIds[o]      = refId;
            }
            LuaLib.lua_rawgeti(L, LuaIndices.LUA_REGISTRYINDEX, refId);                 // |ud
        }
Пример #2
0
        public void PushObject(object o)
        {
            if (o == null)
            {
                LuaLib.lua_pushnil(L);
                return;
            }

            Type type = o.GetType();

            if (type.IsPrimitive)
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                {
                    PushObject((bool)o);
                    break;
                }

                default:
                {
                    PushObject(Convert.ToDouble(o));
                    break;
                }
                }
            }
            else if (type.IsValueType)
            {
                if (type == typeof(UnityEngine.Color))
                {
                    PushObject((UnityEngine.Color)o);
                }
                else if (type == typeof(UnityEngine.Quaternion))
                {
                    PushObject((UnityEngine.Quaternion)o);
                }
                else if (type == typeof(UnityEngine.Vector2))
                {
                    PushObject((UnityEngine.Vector2)o);
                }
                else if (type == typeof(UnityEngine.Vector3))
                {
                    PushObject((UnityEngine.Vector3)o);
                }
                else if (type == typeof(UnityEngine.Vector4))
                {
                    PushObject((UnityEngine.Vector4)o);
                }
                else
                {
                    // TODO
                }
            }
            else
            {
                if (type == typeof(string))
                {
                    LuaLib.lua_pushstring(L, (string)o);
                }
                else if (type == typeof(LuaCSFunction))
                {
                    LuaLib.lua_pushcsfunction(L, (LuaCSFunction)o);
                }
                else if (type.IsSubclassOf(typeof(LuaObjectBase)))
                {
                    ((LuaObjectBase)o).Push();
                }
                else
                {
                    PushCSObject(o);
                }
            }
        }