Introduction:
This is the first post in the series where we will create a native C++ DLL plugin that will work with both Unity3D and Unreal engine. The plugin will also demonstrate using SQLite databases on the Windows platform. There could be several uses for having data stored and retrieved from SQLite databases. For this series, we will only demonstrate a basic use case of storing the player’s saved data in the database locally on the player’s machine.
By the end of the series, we will be able to
- Pass and received different data types to and from native code.
- Pass logs from within the C++ code that can be displayed in the editor window of the engines for debugging.
- Handle data storage into and retrieval from database file.
I will be creating the DLL on a windows machine and the functionality would be tested on windows but I will try and keep the code base as close to platform independent as possible.
In this post, we will create a basic C++ project, compile a simple function into a DLL.
Create C++ Project in Visual Studio:
Let’s start by creating the C++ project using visual studio. We will start with an empty C++ project and add in all the items that we will need. Let’s call this project “SQLite3NativeDataStorage“.
First, we change the configuration of the project to output .DLL file instead of .EXE files.
Next, we will need to create 2 new project configurations. One for Unity3d related code and the other for Unreal Engine related code. Since Unreal Engine uses C++ and Unity3D uses C# for scripting there are a few differences when we compile for the two different platforms. Once we have the configurations set-up we will be able to compile for the engines separately by just selecting the platform from the drop-down box.
Start by opening the project properties and click on the “Configuration Manager” button on the top right corner of the properties window (shown in the screenshot above). Next click on the drop down box that says “Active Solution Configuration” in the pop-up dialog that is shown. Here you should be able to see “New…“. Select the option to create a new configuration for “Unity3D” in the pop-up that is shown. You will need to set the “Copy settings from” option to Release. We do the same process and create another configuration for “Unreal4“. Now we are almost set-up for creating separate DLLs for Unreal Engine and Unity3D. There is one last step we will need to perform that is to set the pre-processor directive for each of the platforms. We will do this after we add a class.
Adding C++ Code to the Project:
Let’s start by adding a new class to the project. This class will be named “DataStorageInterface” and it will expose the functions of the project to the game engine. Let’s also add “stdafx.h” header file to the project. We will use this class later to have global definitions and pre-processor switches based on the platform. For now, this class will be empty and won’t contain any code.
Let’s now start by adding a simple function in the interface class that we have created. This function will only have the functionality to take two integers as arguments from the game engine, add them together and return the sum back to the game engine. Once we have this basic functionality working in the game engines we can start adding the functionality for more complex operations.
Here is the code for the header file.
#define DataStorage_API __declspec(dllexport) #ifdef __cplusplus //if C++ is used convert it to C to prevent C++'s name mangling of method names extern "C" { #endif DataStorage_API int SumOf(int a, int b); #ifdef __cplusplus } #endif
Here is the .cpp file with the implementation for the “SumOf” function:
#include "DataStorageInterface.h" int SumOf(int a, int b) { return (a + b); }
As mentioned earlier, this is a simple function that we will import into Unity3D and Unreal Engine to test if the configuration is working so far. Let’s compile the code that we have so far to check if the .DLLs are being created for both configurations.
Set Preprocessor Definitions for Unreal Engine and Unity3D:
In the “Create C++ Project in Visual Studio” section of this post, it was mentioned that we will need to set the pre-processor directive for Unity and Unreal separately so we can leverage this to separate platform specific code. Let’s do that now.
In the project properties pages, we need to set the preprocessor directives for Unity and Unreal. Under the “C/C++” section of the properties, in the “Preprocessor“, Let’s add “UNITY” for “Unity3D” configuration and “UNREAL” for “Unreal4” configuration as shown in the screenshot below.
Compiling for Unreal Engine and Unity3D:
Although we have created separate profiles for Unity3D and Unreal Engine, at this point we have not yet added any platform dependent code. Still, we can compile the project for both the platforms by selecting the “Solution Configuration” in Visual Studio toolbar.
Building the project for “Unity3D” configuration will create the output .DLL file in the solution folder under “\x64\Unity3D” folder. Similarly, you should be able to build for “Unreal4” configuration and find the DLL in the “\x64\Unreal4” folder.
Conclusion:
We now have a basic DLL that can be used in Unity3D and Unreal Engine. You can download the source from the repository from the tag “Simple_CPP_Plugin“.
In the next post, We will import the plugin that we have created in this post into Unity3D and access the function that we have written.
4 thoughts on “Simple C++ Plugin for Unity3D and Unreal Engine 4”