2

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];
        }
    }

Plugins setup in unity

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
downstroy
  • 960
  • 1
  • 9
  • 27

1 Answers1

1

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.

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
  • Thanks for the answer, that's what I guessed after looking more into it. The main project still doesn't compile because of a library linkage error (my bundle depends on Gamekit, so I have included it in the bundle project, but the main project including the bundle fail to link because of missing GameKit functions). I'll keep this thread updated when I found the solution. – downstroy Nov 05 '21 at 15:39
  • Alright, so after struggling for a day, I found out what was the problem. I was generating the main project using unity, and the native **GameKitNativebundle**, as well as the .mm files are inside the project, under the Assets folder. The problem was I had ticked the .mm files as to be included in the standalone version. So xcode was trying to compile this .mm file directly (in addition to the bundle) and was trying to link it, which failed. – downstroy Nov 09 '21 at 09:09