I’m trying to get my feet wet with native plugin programming and my goal is to write a OSX / Mac plugin that can be used for Push notifications on the Mac. So far I’ve followed the basic steps to create a cocoa bundle and created an objective C class named “Plugin” (Plugin.h/.m).
For a simple test I would like to call from Unity a NSLog output in the plugin via the SoundTest method. The .m file looks like this:
#include
#import "Plugin.h"
@implementation Plugin
- (void)DebugLog:(NSString *)msg {
NSLog(@"###################################");
}
- (void)SoundTest { // <-------------- WANT TO CALL THIS ONE FROM C# CODE
NSLog(@"======================================");
}
...
...
I created a corresponding C# file to import the plugin:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
public class PushNotificationPlugin {
//Calls to the native plugin
[DllImport ("PushNotification")]
public static extern void SoundTest();
Now I got in Xcode a pre-defined PCH file and I read that I have to define in there via some extern “C” the method call. But how?
This is the generated PushNotification-Prefix.pch file:
#ifdef __OBJC__
#import
extern "C" {
static extern void SoundTest(); // <----- doesn't work
}
#endif
Hey Martin,
you cannot call Obj-C code directly from C#. The line:
static extern void SoundTest();
declares a C prototype for a function SoundTest(), but your code does not actually define such a function, so it cannot be found. The Obj-C method you have is not a C function and won’t match that prototype. In your case, I don’t see a reason to write SoundTest as an Obj-C method at all, just make it a normal C function:
void SoundTest() { // <-------------- WANT TO CALL THIS ONE FROM C# CODE
NSLog(@"======================================");
}
Lastly, make sure you are compiling with the correct architecture (Unity 4.1 will only load 32-bit plugins, but latest versions of Xcode default to building 64 bit binaries).