ud = { ["fake_id"] = -1 ["len"] = 12 //存储3个float值的大小,的对应xyz,float是4字节32位。 ["data"][0] = x ["data"][1] = y ["data"][0] = z ["__index"] = obj_meta }
static int _g_get_position(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); UnityEngine.Transform gen_to_be_invoked = (UnityEngine.Transform)translator.FastGetCSObj(L, 1); translator.PushUnityEngineVector3(L, gen_to_be_invoked.position); } catch(System.Exception gen_e) { return LuaAPI.luaL_error(L, "c# exception:" + gen_e); } return 1; } public void PushUnityEngineVector3(RealStatePtr L, UnityEngine.Vector3 val) { if (UnityEngineVector3_TypeID == -1) { bool is_first; UnityEngineVector3_TypeID = getTypeId(L, typeof(UnityEngine.Vector3), out is_first); } //创建一个大小为(12+4+4)字节的userdata,元表Vector3的元表 IntPtr buff = LuaAPI.xlua_pushstruct(L, 12, UnityEngineVector3_TypeID); if (!CopyByValue.Pack(buff, 0, val)) //把vector3拆成3个float传入cc,在c的结构体buff存储数据 { throw new Exception("pack fail fail for UnityEngine.Vector3 ,value="+val); } }
LUA_API void *xlua_pushstruct(lua_State *L, unsigned int size, int meta_ref) { CSharpStruct *css = (CSharpStruct *)lua_newuserdata(L, size + sizeof(int) + sizeof(unsigned int)); css->fake_id = -1; css->len = size; lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref); lua_setmetatable(L, -2); return css; } LUALIB_API int xlua_pack_float3(void *p, int offset, float f1, float f2, float f3) { CSharpStruct *css = (CSharpStruct *)p; if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) { return 0; } else { float *pos = (float *)(&(css->data[0]) + offset); pos[0] = f1; pos[1] = f2; pos[2] = f3; return 1; } }
static int _s_set_position(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); UnityEngine.Transform gen_to_be_invoked = (UnityEngine.Transform)translator.FastGetCSObj(L, 1); UnityEngine.Vector3 gen_value; translator.Get(L, 2, out gen_value); gen_to_be_invoked.position = gen_value; } catch(System.Exception gen_e) { return LuaAPI.luaL_error(L, "c# exception:" + gen_e); } return 0; } public void Get(RealStatePtr L, int index, out UnityEngine.Vector3 val) { LuaTypes type = LuaAPI.lua_type(L, index); if (type == LuaTypes.LUA_TUSERDATA ) { IntPtr buff = LuaAPI.lua_touserdata(L, index); if (!CopyByValue.UnPack(buff, 0, out val)) { throw new Exception("unpack fail for UnityEngine.Vector3"); } } else if (type ==LuaTypes.LUA_TTABLE) { CopyByValue.UnPack(this, L, index, out val); } else { val = (UnityEngine.Vector3)objectCasters.GetCaster(typeof(UnityEngine.Vector3))(L, index, null); } } public static bool UnPack(IntPtr buff, int offset, out UnityEngine.Vector3 field) { field = default(UnityEngine.Vector3); float x = default(float); float y = default(float); float z = default(float); if(!LuaAPI.xlua_unpack_float3(buff, offset, out x, out y, out z)) { return false; } field.x = x; field.y = y; field.z = z; return true; }
public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out UnityEngine.Vector3 val) { val = new UnityEngine.Vector3(); int top = LuaAPI.lua_gettop(L); if (Utils.LoadField(L, idx, "x")) { translator.Get(L, top + 1, out val.x); } LuaAPI.lua_pop(L, 1); if (Utils.LoadField(L, idx, "y")) { translator.Get(L, top + 1, out val.y); } LuaAPI.lua_pop(L, 1); if (Utils.LoadField(L, idx, "z")) { translator.Get(L, top + 1, out val.z); } LuaAPI.lua_pop(L, 1); } public static bool LoadField(RealStatePtr L, int idx, string field_name) { idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index LuaAPI.xlua_pushasciistring(L, field_name); LuaAPI.lua_rawget(L, idx); return !LuaAPI.lua_isnil(L, -1); }
LUALIB_API int xlua_unpack_float3(void *p, int offset, float *f1, float *f2, float *f3) { CSharpStruct *css = (CSharpStruct *)p; if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) { return 0; } else { float *pos = (float *)(&(css->data[0]) + offset); *f1 = pos[0]; *f2 = pos[1]; *f3 = pos[2]; return 1; } }
流程其实跟transform.position差不多,先把自身的ud转成Vector3,再把Vector3.x压栈返回给lua。 static int _g_get_x(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); UnityEngine.Vector3 gen_to_be_invoked; translator.Get(L, 1, out gen_to_be_invoked); LuaAPI.lua_pushnumber(L, gen_to_be_invoked.x); } catch(System.Exception gen_e) { return LuaAPI.luaL_error(L, "c# exception:" + gen_e); } return 1; }
static int __CreateInstance(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); if(LuaAPI.lua_gettop(L) == 4 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4)) { float _x = (float)LuaAPI.lua_tonumber(L, 2); float _y = (float)LuaAPI.lua_tonumber(L, 3); float _z = (float)LuaAPI.lua_tonumber(L, 4); UnityEngine.Vector3 gen_ret = new UnityEngine.Vector3(_x, _y, _z); translator.PushUnityEngineVector3(L, gen_ret); return 1; } } catch(System.Exception gen_e) { return LuaAPI.luaL_error(L, "c# exception:" + gen_e); } }
int addObject(object obj, bool is_valuetype, bool is_enum) { int index = objects.Add(obj); if (is_enum) { enumMap[obj] = index; } else if (!is_valuetype) { reverseMap[obj] = index; } return index; }
原文:https://www.cnblogs.com/wang-jin-fu/p/13508789.html