Callback from native code
Understand how to use native-to-managed callback functionality.
Read time 1 minuteLast updated 20 days ago
Unity iOS supports limited native-to-managed callback functionality using one of the following methods:
Use UnitySendMessage
UnitySendMessageUnitySendMessage("GameObjectName1", "MethodName1", "Message to send");There are three parameters:
- The name of the target GameObject
- The script method to call on that object
- The message string to pass to the called method
UnitySendMessage- From native code, you can only call script methods that correspond to the following signature: .
void MethodName(string message); - Calls to are asynchronous and have a delay of one frame.
UnitySendMessage - If two or more GameObjects have the same name, this can cause conflicts when you use .
UnitySendMessage
Use delegates
When you use delegates, the method on the C# side must be static and marked with the attribute.
MonoPInvokeCallbackTo use delegates:
- Pass the method as a delegate to the extern method.
- Implement the extern method in native code as a function that accepts a pointer to functions with the corresponding signature.
The function pointer in the native code then points back to the static method in C#.
The C# code for this method looks like this:
delegate void MyFuncType();[AOT.MonoPInvokeCallback(typeof(MyFuncType))]static void MyFunction() { }[DllImport ("__Internal")] static extern void RegisterCallback(MyFuncType func);
The C code that accepts the callback then looks like this:
typedef void (*MyFuncType)();void RegisterCallback(MyFuncType func) {}