Version: 2021.1
Native plug-ins
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-ins for Windows, macOS and Linux. For further information see documentation on Native plug-ins.

Plugin Inspector
Plugin Inspector

macOS plug-ins

You can deploy macOS plug-ins as bundles or, if you are using IL2CPP, 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 > Project, 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 Inspector, select a plug-in file in the Project window. 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 Assets 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.
Native plug-ins
Low-level native plug-in interface