👨🏼‍💻How to use Native Code (C/C++) in Unity

Enes Ă–zdemir
Huawei Developers
Published in
4 min readMay 11, 2022

--

Use C/C++ code in Unity

Hello everyone,

In this article, we will talk about how to use Native Code in Unity.

We will be using DLLs to integrate C/C++ code with Unity to allow us to call C/C++ written functions from a Unity C# script.

Introduction

Unity is a cross-platform game engine that is primarily used to develop video games and simulations for computers, consoles, and mobile devices.

Even though Unity uses C# for its scripts, we can use Native plugins to use C/C++ in our projects.

Create Dynamic-Link Library (DLL) Project

The first thing we have to do is create a Dynamic-Link Library project.

Now create a Sample.cpp and Sample.h files in our project.

The header file in C++ is for us to declare our classes and functions.

Here is our sample.h file.

We can export data, functions, classes, or class member functions from a DLL using the __declspec(dllexport) keyword. __declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.

Our cpp file includes the body of our methods.

Here is our sample.cpp file.

We have 5 functions that we want to call from Unity scripts.

With these functions we will be able to;

  • Return an integer
  • Return an integer with given parameters from C#
  • Return a pointer
  • Get a string from C++
  • Return a Boolean

Now let’s configure our project to get built on 64-bit.

Right-click on the Project → Properties

Change the platform to x64.

Click to Configuration Manager → Change the project platform to x64 → Change the active platform to x64.

Now build the solution to get a dll file.

After the project builds successfully right-click to solution and select Open Folder in File Explorer.

Now under SampleNativeProject\x64\Debug, we have the SampleNativeProject.dll file

This dll contains our C++ code that we want to use.

Now create a Unity project and open it.

Simply drag the dll file to the project folder in Unity.

Now create a script to import the dll’s and use the C++ functions

For this script, we are Importing our dll with [DllImport(“SampleNativeProject.dll”)] and then by using the same method name we are binding our methods to C++ codes.

Now that we are able to import the dll let’s use them in our Unity scene.

Here is our full NativeSampleScript.cs

Here is the demo scene I prepared for this specific scenario.

Now we can give our script to any GameObject on the scene and assign the Text components to the script.

And here is our end result.

We successfully used the C++ functions in Unity.

Tips & Tricks

  • Since string in C++ can not be directly cast to C# we are using the void FillString() method to build the string in C#.
  • To be able to use C++ in Visual studio use the Visual studio installer → Modify and select the Desktop Development with C++.
  • Make sure to check References to learn how to return more complex types.

References

Project Repositories

  1. Unity

2. C++

--

--