Opengl Camera to Unity, how?

I have datasets with cameras represented by OpenGL Matrix4x4.

How can I apply these matrixes to an Unity 3D camera?

What I do now is trying to extract the position and rotation, using this code:

public static Quaternion ExtractRotation(Matrix4x4 matrix,bool convertFromRighthanded=true)
    {
        float s = 1; if (convertFromRighthanded) s = -1;

        Vector3 forward;
        forward.x = matrix.m02;
        forward.y = matrix.m12;
        forward.z = matrix.m22*s;

        Vector3 upwards;
        upwards.x = matrix.m01;
        upwards.y = matrix.m11;
        upwards.z = matrix.m21*s;

        return Quaternion.LookRotation(forward, upwards);
    }

    public static Vector3 ExtractPosition(Matrix4x4 matrix, bool convertFromRighthanded = true)
    {
        float s = 1; if (convertFromRighthanded) s = -1;

        Vector3 position;
        position.x = matrix.m03;
        position.y = matrix.m13;
        position.z = matrix.m23*s;
        return position;
    }

//Then, to extract camera rotation and position:
Camera.main.tranform.position=ExtractPosition(openGLcamMatrix);
Camera.main.tranform.rotation=ExtractRotation(openGLcamMatrix);

Note that I am taking into account the handedness being different in Unity and OpenGL.

Still, the code above is not able to recreate the cameras in my opengl data. Am I doing this wrong?

I cannot look at that matrix flip-it-over math and say “yeah you forgot this part,” but one suggestion I have is to go to OpenGL, make a very specific camera shape, at a certain spot and view frustum, then export its matrix.

Now do the same in Unity and compare the two matrices visuall. This might give you some insight as to how the handedness differs, or if there’s anything else quirky. It won’t help with fields that are 0 or 1 but it might help with other intermediate value fields.