glReadPixels in native plugin shows different results for different Unity versions

Hi,

I’m trying to use glReadPixels in a native plugin to get every frame and store it into an image on Windows. The C# code(bound to a camera) is:

IEnumerator OnPostRender()
{
        TakeSnapshot();
        yield return null;
}

and the C++ code of the plugin is:

extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API TakeSnapshot()
{
        cv::Mat m_ScrFrm;
	glReadPixels(0, 0, m_ScrFrm.cols, m_ScrFrm.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, m_ScrFrm.data);
        int errorcode=glGetError();
}

in which m_ScrFrm is an opencv mat format.
I have forced the program to be built with OpenGLCore.

When using Unity 5.1, I can get the image out correctly; However when I use Unity 5.3 or 5.4 build the same project and run, it fails and glGetError() returns 1282 error.
I also tried to call the TakeSnapshot() function after yield return new WaitForEndOfFrame(), the same problem exists.

Has anyone met the same problem please? How to solve it?

The Unity docs can help you there:

In order to do any rendering from the plugin, you should call GL.IssuePluginEvent from your script, which will cause your plugin to be called from the render thread. For example, if you call GL.IssuePluginEvent from the camera’s OnPostRender function, you get a plugin callback immediately after the camera has finished rendering.