This page describes how to build native code plug-ins for the iOSApple’s mobile operating system. More info
See in Glossary platform.
To build an application with a native plug-inA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary for iOS, follow these steps:
For each native function you want to call, define an extern method in the C# file as follows:
[DllImport ("__Internal")]
private static extern float FooPluginFunction();
Add your native code source files to the Unity Project.
Customize the plug-in’s settings in the Plugin Inspector window. For example, if your native code is iOS-specific, make sure the plug-in is only enabled for iOS.
Note: If you are using C++ (.cpp) or Objective-C++ (.mm) to implement the plug-in, you must declare functions with C linkage to avoid issues with name mangling:
extern "C" {
float FooPluginFunction();
}
Plug-ins written in C or Objective-C don’t need this, because these languages don’t use name mangling.
Your app can only call iOS native plug-insA platform-specific native code library that is created outside of Unity for use in Unity. Allows you can access features like OS calls and third-party code libraries that would otherwise not be available to Unity. More info
See in Glossary when it’s deployed on an actual device, so you should wrap all native code methods with an additional C# code layer. This code should either use UNITY_IOS && !UNITY_EDITOR
conditional compilation, or check Application.platform
and only calll native methods when the app is running on a device. A simple way to implement this is:
void MyMethod()
{
#if UNITY_IOS && !UNITY_EDITOR
CallNativeMethodImplementation();
#else
CallEditorMethodImplementation();
#endif
}
For a more detailed implementation, download the Bonjour Browser Sample below.
Unity iOS supports limited native-to-managed callback functionality. You can do this in one of two ways:
* Using UnitySendMessage
* Via delegates
UnitySendMessage
This option is simpler, but has some limitations. It looks like this:
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
There are three parameters:
* The name of the target GameObject
* The script method to call on that object
* The message string to pass to the called method
Using UnitySendMessage
has the following limitations:
void MethodName(string message);
.UnitySendMessage
are asynchronous and have a delay of one frame.UnitySendMessage
.This is the more complex option. When you use delegates, the method on the C# side must be static and be marked with the MonoPInvokeCallback
attribute. You must pass the method as a delegate to the extern method which is implemented in native code as a function that takes a pointer to function with the corresponding signature. The function pointer in the native code then leads back to the C# static method.
The C# code for this method looks like this:
delegate void MyFuncType();
[AOT.MonoPInvokeCallback(typeof(MyFuncType))]
static void MyFunction() { }
static extern void RegisterCallback(MyFuncType func);
The C code that accepts the callback then looks like this:
typedef void (*MyFuncType)();
void RegisterCallback(MyFuncType func) {}
Unity supports automated plug-in integration and copies all files with the following extensions to the generated Xcode project if you enable them for iOS in the Plugin InspectorA Unity window that displays information about the currently selected GameObject, Asset or Project Settings, alowing you to inspect and edit the values. More info
See in Glossary window: .a
, .m
, .mm
, .c
, .cpp
, .h
. If any files with these extensions are located n the Assets/Plugins/iOS
folder, Unity only enables them for the iOS platform.
Note: After the files are copied to the generated Xcode project, they’re no longer linked to their counterparts in your Unity Project. If you change these files in Xcode, you must copy them back into your Unity Project. Otherwise, Unity will overwrite them the next time you build your Project.
Managed-to-unmanaged calls are quite processor-intensive on iOS. Try to avoid calling multiple native methods per frame.
Wrap your native methods with an additional C# layer that calls native code on the device and returns dummy values in the Editor.
String values returned from a native method should be UTF–8 encoded and allocated on the heap. Mono marshalling calls free for strings like this.
You can download a simple example of how to use a native code plug-in here: Bonjour Browser Sample.
This example demonstrates how you can invoke bjective-C code from a Unity iOS application. This application implements a simple Bonjour client and consists of:
Plugins\Bonjour.cs
is the C# interface to the native code, and BonjourTest.cs
is the script that implements the application logicAssets/Plugins/iOS
) that should be added to the built Xcode project as described in the Automated plug-in integration section above