用于创建插件和使用 IssuePluginEvent 的 Unity 示例涵盖了您想要/需要直接调用 C 函数的情况,并且此函数仅接受 eventId 作为参数。如果读者来自 C\C++ 背景并且不熟悉 C#,则可能会产生误导。事实上,您实际上可以使用这个简单的技巧调用 C# 函数,并在该函数内部调用传递多个参数的 C 函数。步骤是:
- 获取 C# 函数的函数指针(注意该函数将是静态的)
- 将该函数指针传递给 GL.IssuePluginEvent
- 在您的静态 C# 函数中,根据 eventId 的情况,调用适当的 C 函数
void Update()
{
GL.IssuePluginEvent(RenderThreadHandlePtr, GL_INIT_EVENT); // example
}
/// <summary> Renders the event delegate described by eventID. </summary>
/// <param name="eventID"> Identifier for the event.</param>
private delegate void RenderEventDelegate(int eventID);
/// <summary> Handle of the render thread. </summary>
private static RenderEventDelegate RenderThreadHandle = new RenderEventDelegate(RunOnRenderThread);
/// <summary> The render thread handle pointer. </summary>
public static IntPtr RenderThreadHandlePtr = Marshal.GetFunctionPointerForDelegate(RenderThreadHandle);
public const int GL_INIT_EVENT = 0x0001;
public const int GL_DRAW_EVENT = 0x0002;
/// <summary> Executes the 'on render thread' operation. </summary>
/// <param name="eventID"> Identifier for the event.</param>
[MonoPInvokeCallback(typeof(RenderEventDelegate))]
private static void RunOnRenderThread(int eventID)
{
switch (eventID)
{
case GL_INIT_EVENT:
glInit(par1, par2, par3); // C function with 3 parameters
break;
case GL_DRAW_EVENT:
glStep();
GL.InvalidateState();
break;
}
}
[DllImport("hello-jni")]
private static extern void glInit(int par1, int par2, int par3);
[DllImport("hello-jni")]
private static extern void glStep();