Passing a Texture reference from Unity to xcode

I am trying to pass a reference of a cameras texture into xcode so that I can then use it for face detection. Most methods I have tried have left me stumped, but I think I am close with what I currently have.

In unity I get the texture id and pass this through a plugin:

[DllImport("__Internal")]
	public static extern void SendBackground(int id, int width, int height);

//in Update
Texture bg_Img = GetComponentInChildren<VideoTextureBehaviour>().GetTexture(); //this is from Vuforia
int texture_Id = bg_Img.GetNativeTextureID();
SendBackground(texture_Id, bg_Img.width, bg_Img.height);

xcode receives the texture ID fine, I then try to access it from OpenGL and get it to a CIImage

- (void) DetectFaces:(int)tex_id :(int)width :(int)height
{
    glGenTextures(1, (GLuint)&tex_id);
    glBindTexture(GL_TEXTURE_2D, (GLuint)tex_id);

    CIImage *image = [[CIImage alloc]  initWithTexture:(GLuint)tex_id size:CGSizeMake(width, height) flipped:YES colorSpace: (CGColorSpaceRef) kCGColorSpaceModelRGB];
}

Currently that last line results in a bad access exception at run time. Any help is greatly appreciated.

wrong - if this is the texture id you grabbed from your texture - you should just use it (as in - it is proper gl texture already)
by adding glGenTextures you effectively create new texture with undefined data, so just kill this line and you should be good

Hi Alexey, if I wanna update the texture using glTexImage2D in c++ function, do I have to do it in the Update() function in unity? Is it possible to call glTexImage2D in a created thread funcino in c++ when I obtained a texture data in that thread in c++?

I’m trying to achieve a similar effect and even though I don’t have a call to glGenTextures I still am receiving a bad access exception. Has anyone been able to confirm a solution to this?

can you please bug report with small repro? drop case number here
Also it seems i really need to create sample project about tweaking unity texture (instead of using external texture from get-go)