I'm writing a plugin for unity, I need to send a texture from ios to unity. There is a UnitySendMessage function which takes char* as parameter, but I didn't find a way to convert id<MTLTexture> to char*.

How can I send id<MTLTexture> from ios and receive it in unity?

My current code :

//ios side ...
_texture = CVMetalTextureGetTexture(texture);
UnitySendMessage(CALLBACK_OBJECT, CALLBACK_TEXTURE_READY,_texture);//error

//unity side
private void OnTextureReady(string texture_str)
   IntPtr texture = new IntPtr(Int32.Parse(texture_str));
   int width = 256;
   int height = 256;
   rawImage.texture = Texture2D.CreateExternalTexture(width, height, 
   TextureFormat.ARGB32, false, false, texture);

Don't try to cast string to IntPtr. char* is just pointer and any pointer(char*, void*,...) coming from native side unity treat it as IntPtr.

id<MTLTexture> tex = yourTexture;
char* ptr = &tex;

And now you have a ptr from native side

| improve this answer | |
  • \$\begingroup\$ I don't understand what you are saying, It doesn't fix the problem, I still have to convert void* to char*(because UnitySendMessage accept char* as the third parameter) \$\endgroup\$ – omid May 21 '18 at 7:48
  • \$\begingroup\$ It's doesn't matter I changed my answer I don't understand why try cast string to IntPtr \$\endgroup\$ – buffalo94 May 21 '18 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.