This page describes how to build native code plug-ins for the iOS 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:
UnitySendMessage
UnitySendMessage
This option is simpler, but has some limitations. It looks like this:
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
There are three parameters:
GameObject
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, allowing 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 in 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 Objective-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 aboveWhen you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.