展示了Lua访问在C#中带out参数的函数。
「1」代码
操作代码如下:
local box = UnityEngine.BoxCollider
function TestPick(ray)
local _layer = 2 ^ LayerMask.NameToLayer('Default')
local time = os.clock()
local flag, hit = UnityEngine.Physics.Raycast(ray, nil, 5000, _layer)
--local flag, hit = UnityEngine.Physics.Raycast(ray, RaycastHit.out, 5000, _layer)
if flag then
print('pick from lua, point: '..tostring(hit.point))
end
end
c#代码如下:
void Start ()
{
new LuaResLoader();
state = new LuaState();
LuaBinder.Bind(state);
state.Start();
state.DoString(script, "TestOutArg.cs");
func = state.GetFunction("TestPick");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Camera camera = Camera.main;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool flag = Physics.Raycast(ray, out hit, 5000, 1 << LayerMask.NameToLayer("Default"));
if (flag)
{
Debugger.Log("pick from c#, point: [{0}, {1}, {2}]", hit.point.x, hit.point.y, hit.point.z);
}
func.BeginPCall();
func.Push(ray);
func.PCall();
func.EndPCall();
}
state.CheckTop();
state.Collect();
}
「2」需要了解的部分
-
bool flag = Physics.Raycast(ray, out hit, 5000, 1 << LayerMask.NameToLayer("Default"));
在Lua代码中被写成了
local flag, hit = UnityEngine.Physics.Raycast(ray, nil, 5000, _layer)
可见需要写out的参数直接填写nil,参数放入返回值中即可。 -
值得一提的是,Lua的没有移位操作,1 << a 写为 2 ^ a
网友评论