如何在 C 函数中调用其他 lua 函数后返回 lua 函数的值

所以我有这样一段代码:

public static int Window(ILuaState lua)
{
    int id = lua.L_CheckInteger(1);
    Rect rect = RectLib.CheckRect(lua, 2);
    string title = lua.L_CheckString(3);
    int fRef = lua.L_Ref(LuaDef.LUA_REGISTRYINDEX); // 作为参数传递的函数

    Rect r = GUI.Window(id, rect, (i) => // 在 RectLib.PushRect 之前执行这些函数
    {
        lua.RawGetI(LuaDef.LUA_REGISTRYINDEX, fRef);
        lua.PushInteger(i);
        if (lua.PCall(1, 0, 0) != ThreadStatus.LUA_OK)
            Debug.LogError(lua.ToString(-1));
    }, title);

    RectLib.PushRect(lua, r); // 基本上是将一个表推到堆栈上

    return 0;
}

问题在于,在调用传递的函数之后,栈上的值出了一些问题,返回值变成了 nil。我尝试在 lua.PCall 之后放置了 lua.Pop(1)lua.Pop(2)lua.Pop(3),但是失败了。我该怎么办?

调用之后的堆栈如下所示:

5: none
4: table
3: string (字符串标题)
2: table
1: 数字 (int id)
0: 数字 (需要返回的表的第四个元素)
-1: table
-2: string (字符串标题)
-3: table
-4: 数字
-5: 函数

请帮忙!

点赞