Import C++ Plugin into Unity3D

In the previous post, we created a simple plugin in C++ which takes 2 integers as arguments and adds them together. In this post, we will import this plugin into a new Unity3D project and access the functionality.

Project Set-Up:

Let’s start off by creating a new Unity3D project. We will call this new project “DataStorageSample“. We will now need to create the folder structure to load the DLL plugin that we created in the previous post. In the Assets folder of the project, we create “\Plugins\x64“. To learn more about the folder structure of different plugins, please refer to the documentation provided by Unity3D.

Import the Plugin:

Next, we will need to paste the DLL into the newly created x64 folder. Please note that if Unity3D editor is open, you will need to close it before you paste the DLL. Copy the DLL file from the “\x64\Unity3D” folder of the CPP plugin (visual studio) project and paste into the Plugins\x64 folder that we created.

Let’s open the Unity3D project now. We should be able to see that the DLL is present and imported into the folder. Clicking on it should show the import settings.


Access Plugin Functionality in Unity3D:

Let’s now create a script in Unity3D that will use the function that we just created in C++. Create a C# script in the Unity3D project and call it “Storagemanager.cs“.

To access the functionality of the plugin that we just created, we will first need to start using the interopservices library. At the top of the script, let’s add the following line

using System.Runtime.InteropServices;

Next, we will import the plugin and declare the function that we want to use in the script.

//Filename of the DLL.
const string c_strDLLName = "SQLite3NativeDataStorage";
//Import the function
[DllImport(c_strDLLName, CallingConvention = CallingConvention.Cdecl)]
private static extern int SumOf(int a, int b);

Please note here that the DLL name that we have mentioned in the script should match the file name of the DLL that we created and the function name should match the function that is declared and exposed in the DLL’s source code for the function to be loaded and bound.

All that is left now is to use the function and test if everything works. Here is the complete “StorageManager.cs“. Just add the component to the main camera in the scene and hit play. We should see the debug log in the console window.

using UnityEngine;
using System.Runtime.InteropServices;
public class Storagemanager : MonoBehaviour
//File name of the DLL.
const string c_strDLLName = "SQLite3NativeDataStorage";
//Import the function
[DllImport(c_strDLLName, CallingConvention = CallingConvention.Cdecl)]
private static extern int SumOf(int a, int b);

// Use this for initialization
void Start ()
int num1 = 10;
int num2 = 87;
int sum = SumOf(num1, num2);
string msg = string.Format("The Sum of {0} and {1} is {2}", num1, num2, sum);

We have now successfully created and imported a C++ plugin into Unity3D. In the next post, we will import the DLL plugin into an Unreal Engine 4 Project. You can download the entire source so far at the tag “Import_Unity” from BitBucket.

2 thoughts on “Import C++ Plugin into Unity3D

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.