今天写Lua脚本的时候把一个Vector3的值赋给了position.x,但是运行unity的时候也不报错,看了半天才发现,但是为什么不报错呢,就去看了XLua的Vector3赋值实现:
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int _m_Set(RealStatePtr L)
{
try {
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
UnityEngine.Vector3 gen_to_be_invoked;translator.Get(L, 1, out gen_to_be_invoked);
{
float _newX = (float)LuaAPI.lua_tonumber(L, 2);
float _newY = (float)LuaAPI.lua_tonumber(L, 3);
float _newZ = (float)LuaAPI.lua_tonumber(L, 4);
gen_to_be_invoked.Set( _newX, _newY, _newZ );
translator.UpdateUnityEngineVector3(L, 1, gen_to_be_invoked);
return 0;
}
} catch(System.Exception gen_e) {
return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
}
}
lua_tonumer()方法会把除了数字以外的数据类型都返回nil:
>print(tonumber(1))
>1
>print(tonumber(1.1))
>1.1
>print(tonumber("1"))
>1
>print(tonumber(true))
>nil
所以经过(float)强制转换应该是把nil转成了0,因此不会改变原值也不会报错。