Alternate to UnitySendMessage in iOS native

Hey Guys,

Currently I am working on creating a Plugin for Unity3D-iOS. I want to send data from iOS native to Unity3D. For function called from iOS native to Unity3D I need to use UnitySendMessage function. But the limitation of UnitySendMessage is that there should be some speciface GameObject there in Unity3D and it’s name should be fixed and that name i have to pass in UnitySendMessage as a parameter. While in my case this solution is not suitable for me.

For example :

code in iOS (xCode) :

[self callThisInUnity];

code in Unity3D c# :

void callThisInUnity(){

   Debug.Log("Call from iOS");
}

So if I call “callThisInUnity” function from iOS then i should be called in Unity3d.

So is there any alternate way of UnitySendMessage for sending iOS data to Unity3D ?

Thanks in advance.

The only alternative way I know about is to pass integers as function pointers from Unity to the native layer, recast them and store them as handles. It’s pretty rough tbh, but if you’re feeling so strong about not using UnitySendMessage & GameObjects - that’s the way to go :slight_smile:

unity code:

class MyUnityClass
{
   [DllImport ("iosplugin")]
   private static extern int DefinePointer(int pointer);
    
   void TestPointer()
   {
      Debug.Log("hello world");
   }

   void InitializePointers()
   {
      IntPtr p = Marshal.GetFunctionPointerForDelegate(TestPointer as System.Action);

      // pass the function pointer to the native layer
      DefinePointer(p.ToInt32());
   }
}

iosplugin code:

extern "C"
{
   void DefinePointer(int pointer)
   {
      void (*unityfunc)() = reinterpret_cast<void(*)()>(pointer);
        
      unityfunc();
   }
}

…and the unity console prints out “hello world”.

In the example above I cast the pointer right away and call the function, but you can just store them in a global member and call them whenever you like.

Hope that helps :wink: Cheers