Version: 2020.1
Managed stack traces with IL2CPP
Low-level native plug-in interface

Building plug-ins for desktop platforms

Plug-ins for desktop platforms are libraries of native code written in C, C++ and Objective C. This page describes plug-insA 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 Windows, macOS and Linux. For further information see documentation on 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
.

Plugin Inspector
Plugin Inspector

macOS plug-ins

You can deploy macOS plug-ins as bundles or, if you are using IL2CPPA Unity-developed scripting back-end which you can use as an alternative to Mono when building projects for some platforms. More info
See in Glossary
, loose C++ files which you can use [DllImport(“__Internal”)] syntax to invoke. For further information on loose C++ plug-ins see documentation on C++ source code plugins for IL2CPP.

To create the bundle project with XCode, open XCode 11 and select File > New > ProjectIn Unity, you use a project to design and develop a game. A project stores all of the files that are related to a game, such as the asset and Scene files. More info
See in Glossary
, then navigate to macOS > Framework & Library > Bundle. For more information on working with XCode see Apple’s documentation on XCode.

You must build your plug-in as a universal binary that contains 64-bit architectures. Alternatively, you can provide separate dylib files. If you are using C++ (.cpp) or Objective-C (.mm) to implement the plug-in then you must make sure you declare the functions with C linkage to avoid name mangling issues.

extern "C"
{
  float FooPluginFunction ();
}

Windows plug-ins

Plug-ins on Windows are either .dll files with exported functions, or loose C++ files if you are using IL2CPP. You can use most languages and development environments that can create .dll files to create plug-ins. You must declare any C++ functions with C linkage to avoid name mangling issues.

Linux plug-ins

Plug-ins on Linux are .so files with exported functions. While these libraries are typically in C or C++, you can use any language. As with the other platforms, you must declare any C++ functions with C linkage to avoid name mangling issues.

Managing plug-ins inside Unity

In Unity, the Plugin Inspector manages your plug-ins. To access 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
, select a plug-in file in the Project windowA window that shows the contents of your Assets folder (Project tab) More info
See in Glossary
. For Standalone platforms you can choose the CPU architecture with which the library is compatible. For cross platform plug-ins you must include the .bundle file (for macOS), the .dll file(for Windows), and the .so file (for Linux). Unity automatically picks the right plug-in for the target platform and includes it with the player. For further information see documentation on the Plugin Inspector.

Using your plug-in from C#

Once you have built the bundle, you must place it in the AssetsAny media or data that can be used in your game or project. An asset may come from a file created outside of Unity, such as a 3D Model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. More info
See in Glossary
folder (or the appropriate architecture-specific sub-directory) in your Unity Project. Unity then finds it by name when you define a function like the following in the C# script: [DllImport ("PluginName")] private static extern float FooPluginFunction ();

Note: PluginName should not include the library prefix nor file extension. For example, the actual name of the plug-in file would be PluginName.dll on Windows and libPluginName.so on Linux.

Example plug-ins

Project Description Link
Simplest Plugin Example This project implements basic operations such as; print a number, print a string, add two floats and add two integers. Follow the link to the Unity GitHub repository to download the Simplest Plugin Example project. This project includes Windows, macOS and Linux project files.
Native Renderer Plugin This is an example of a low-level rendering plugin. This project demonstrates the following:
- Renders a rotating triangle from C++ code after all regular rendering is done.
- Fills a procedural texture from C++ code, using Texture.GetNativeTexturePtr to access it.
Follow the link provided to the Unity GitHub repository to download the Native Renderer Plugin. This project includes Windows, UWP, macOS, WebGL and Android files.
Managed stack traces with IL2CPP
Low-level native plug-in interface