- Home /
OS X Native plugin callback functions
I've written a small iOS plugin, and am trying to make an OS X port. In particular, the plugin is for an augmented reality application, so it uses the webcam. Having to build to a device every time I want to test something is slow, so I want to make a version that works in the editor as well (similar to Vuforia).
Since OS X/Desktop doesn't support UnitySendMessage I've been trying to write a simple callback function to replace it.
My native code:
// .h
extern "C" {
typedef void (*callbackFunc)(const char *);
}
static callbackFunc cameraFrameCallback;
// .mm
extern "C" {
void _initWithCamera(const char * camera, GLuint texID, GLfloat width, GLfloat height, callbackFunc callback) {
// some other init here
cameraFrameCallback = callback;
if (cameraFrameCallback != NULL) {
cameraFrameCallback([@"IS THIS PRINTING?" UTF8String]);
}
}
}
C# code:
public delegate void cameraFrameCallback(string message);
[MonoPInvokeCallback(typeof(cameraFrameCallback))]
public static void PrintLog(string message) {
Debug.Log(message);
}
[DllImport("DesktopWrapper")]
private static extern void _initWithCamera(string camera, int texID, float width, float height, cameraFrameCallback callback);
public static void InitWithCamera(string camera, int texID, float width, float height) {
if (Application.platform == RuntimePlatform.OSXEditor) {
_initWithCamera(camera, texID, width, height, PrintLog);
}
}
When I call the init method, I would expect the callback to get registered then send back the call to PrintLog, which would print my message in the Unity console. However, when I run this, I get nothing back. My plugin is actually running though, because I'm getting camera frames back as I would expect, just the callback function isn't firing.
Am I missing something obvious, or does this just not work in the Editor?
Answer by cbaltzer · Sep 03, 2013 at 01:03 PM
For people from the future/Google:
The issue was that the Editor doesn't reload plugins until you restart it. The code above actually works fine, but replacing my bundle with new versions when I made changes doesn't actually cause the plugin to get reloaded, you need to replace the bundle then restart the editor.
Thanks for the great code example, @cbaltzer !
(Yes, it is infuriating that you have to restart the editor:
https://stackoverflow.com/a/54018091/294884
)
Cheers....
Your answer
Follow this Question
Related Questions
GL.IssuePluginEvent with multiple plugins 1 Answer
How to access an Android native plugin in unity ? 0 Answers
Referencing a DLL in C# 0 Answers
Unity iOS - function pointers 1 Answer
DllNotFound when the plugin c++ dll links another c++ dll 2 Answers