fbpx

Create an OSX plugin for Unity

CREATE AN OSX PLUGIN FOR UNITY

Create an OSX plugin for Unity

Requirements : Xcode (above version 9) and Unity (above version 2017)

In this blog , I will show a step by step process to create a plugin and how to call native methods from Unity and vice versa.

On Mac OSX, plugins are deployed as bundles. You can create the bundle project with XCode by selecting File->NewProject… and then selecting Bundle-> Carbon/Cocoa Loadable Bundle (in XCode 3) or OS X -> Framework & Library -> Bundle (in XCode 4)

 

It will then ask for details like organisation name and project name. The below picture shows dummy values. You can give your own custom names. You can select your team if you have a signing certificate as well.

Once done now we need to create a .mm file where we will define our methods. You can do the same by selecting File->New File->C++ File.

On Selecting this option, you will be shown a window where you need to give the name of the C++ File. Please check the also create header file option. The header file(.h file) is a file which contains the declarations of the methods we define in the .mm file.

Once done you will see that the files are there under your project hierarchy. Now we will write the methods.

 

If you are using C++ (.cpp) or Objective-C (.mm) to implement the plugin then you must ensure the functions are declared with C linkage to avoid name mangling issues.

 

So in the TestMethods.h file we declare all the methods that we are going to use

PrintHello – method to return a string

PrintANumber – method to return a number

AddTwoIntegers – method to take 2 integers as arguments and return their sum

AddTwoFloats – method to take 2 floats as arguments and return their sum

 

All these methods are declared within the extern “C” block so that we can access them in unity.

Now we will define these methods in TestMethods.mm

Once done now you can run the Xcode.It will create a.bundle file , this is the file which we will use in Unity.

Now in Unity , keep the .bundle file inside Plugins Folder and now create a .cs script to use the methods inside the bundle.

 

To use the methods inside the bundle ,

You need to declare them using static extern keywords and you need to add the following above the methods :

[DllImport (“YourPluginName”)]

Run unity and you can see that all the methods are working.

 

Now in the next section I will explain how to call a method of Unity from inside the bundle (this is mostly used for callbacks).

 

So Basically we will create a delegate and pass the same as a parameter while calling a method of the Bundle.The bundle will then call the method inside that delegate as callback method.

 

So for taking it as a parameter we declare the method inside TestMethods.h we define the method using typedef . Refer to the link below :

https://stackoverflow.com/questions/47780330/typedef-void-print-type-const-char

Now ,we define the method that takes the same as a parameter. So you can call a method passing the delegate as a parameter. Xcode will call that method using the delegate. So it will look like this:

In Unity , Declare a delegate that uses string as a parameter

And declare the above method in the script like the previous methods . So now the script looks like this :

You can now call this method in start method and this is how you can pass delegates as a parameter in order to call methods in Unity from Xcode

The source code for the above can be found here : https://github.com/gamedev1991/OS-X-Plugin-For-Unity

Enquire Now
close slider