Simple native texture manipulation plugin

Hi,

We would like to be able to change textures natively on iOS and made a very simple setup. But it keeps giving error 0x0502 (invalid_operation) although nothing seems to be wrong at all. Any help would greatly appreciated. Changing ios version to 4.3 didn’t help nor did changing to require armv7.

EDIT: The texture was set with power of 2 toNearest in Unity, setting it to none solved the errors…Now however only a quarter of the texture gets filled with black. Will update when we fix this, any ideas on this ofcourse appreciated!

EDIT: Code below is working:

This function was added to AppController.mm in Xcode:

    void DoStuff(int texId,int width,int height)
    {
        
        glBindTexture(GL_TEXTURE_2D, texId);
        
        int size = width * height * 3;
        
        unsigned char *pixels = (unsigned char*)malloc(size);
        
        int byteWidth = 3 * width;
        
        for(int y=0;y<height;y++)
        {
            for(int x=0;x<byteWidth;x+=3)
            {
                if(x < 10)
                {
                    pixels[x + y * byteWidth] = 255;
                    pixels[x + y * byteWidth+1] = 0;
                    pixels[x + y * byteWidth+2] = 0;
                
                }
                else
                {
                    pixels[x + y * byteWidth] = 0;
                    pixels[x + y * byteWidth+1] = 255;
                    pixels[x + y * byteWidth+2] = 0;
                }
            }
        }       
                
        glPixelStorei(GL_UNPACK_ALIGNMENT,1);
        
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB, GL_UNSIGNED_BYTE,pixels);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        //glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_RGB, GL_UNSIGNED_BYTE,pixels);
        
        free(pixels );
        
        
    }
    
}

And this is a script added to a camera in Unity:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class LoadNative : MonoBehaviour 
{
	[DllImport ("__Internal")]
	private static extern float DoStuff (int handle,int width,int height);
	
	// Use this for initialization
	void OnPostRender () {
		GameObject cube = GameObject.Find("Cube");
		Texture texture = cube.renderer.sharedMaterial.mainTexture;
		
		DoStuff(texture.GetNativeTextureID(),texture.width,texture.height);
		
		GL.InvalidateState();
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

I dont know the solution to your code, but a question came to mind…

Doesn’t doing a Mono to Extern C call produce too much of a performance impact to do every frame? I was under the impression there was overhead to do such a call and it was best not to do it every frame.

Hi,

For testing we did it every frame, in the end we will only do it once per scene. It is working now more or less , will post it soon.

For anyone googling here, for 2019 there’s a totally new system from Unity …

https://github.com/keijiro/TextureUpdateExample

1 Like

that’s great! works well. thanks.

Yup, its’ a huge change. It has some limitations though, you sometimes end up having to use the “conventional” approach.