I'm trying to write a native plugin to have access to some of the GameKit functionnalities not yet exposed by the Social.localUser unity API (namely the fact that the local user is multiplayer restricted).
I've written a GameKitWrapper.mm file where I declare a function, and its Csharp counterpart. Everything works fine on IOS (the plugin is found and the methods return the right values), but on appleTV and macOSX the .mm file can't be found.
As I understand it, on macOSX and appleTv I should dynamically link the plugin instead of using the "__Internal" keyword which stands for static-linked librairies. But even when I change the [DllImport("__Internal")]
to the name of my plugin (I tried DllImport["GameKitNativeWrapper"]
and DllImport["GameKitNativeWrapper.mm"]
) when building for appleTV or macOSX, it doesn't work and I got this error:
DllNotFoundException: Unable to load DLL 'GameKitNativeWrapper': The specified module could not be found
.
As I have read on the documentation it should work. Is there anything missing on my setup to make the plugin work on appleTV and macOSX?
Should I compile the .mm file into a bundle? so that it can be dynamically linked when running the app on AppleTV and/or Mac?
Below is the relative C# source file GameKitHelper.cs
namespace Plugins.GameKit
{
using System.Runtime.InteropServices;
public static class GameKitHelper
{
[DllImport("__Internal")]
private static extern bool IOSIsMultiplayerGamingRestricted();
public static bool IsMultiplayerRestricted()
{
if (DeviceHelper.IsAppleDevice())
{
return IOSIsMultiplayerGamingRestricted();
}
else
{
return false;
}
}
}
}
and this is the releveant GameKitNativeWrapper.mm file:
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
#include <Availability.h>
#include <TargetConditionals.h>
@interface GameKitNativeWrapper: NSObject
{
}
@end
@implementation GameKitNativeWrapper
static GameKitNativeWrapper *_sharedInstance;
(GameKitNativeWrapper*) sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Creating GameKitNativeWrapper shared instance.");
_sharedInstance = [[GameKitNativeWrapper alloc] init];
});
return _sharedInstance;
}
-(id)init
{
self = [super init];
if (self)
[self initHelper];
return self;
}
-(void)initHelper
{
NSLog(@"initHelper called");
}
-(bool)isMultiplayerGamingRestricted
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
return localPlayer.isMultiplayerGamingRestricted;
}
@end
extern "C"
{
bool IOSIsMultiplayerGamingRestricted()
{
return [[GameKitNativeWrapper sharedInstance] isMultiplayerGamingRestricted];
}
}
CodePudding user response:
If you want to use your Objective-C (.mm) to implement the Mac OS plug-in for unity, you should deploy it as a bundle.
To create the bundle project with XCode:
Open XCode. Select File > New > Project > macOS > Framework & Library > Bundle.