https://github.com/offchan42/unitycppclassdllexample
:bulb: Example exporting CPP class as DLL and importing in Unity C# as native plugin
https://github.com/offchan42/unitycppclassdllexample
cpp csharp dll exporting-cpp native-plugin unity
Last synced: 2 months ago
JSON representation
:bulb: Example exporting CPP class as DLL and importing in Unity C# as native plugin
- Host: GitHub
- URL: https://github.com/offchan42/unitycppclassdllexample
- Owner: offchan42
- License: mit
- Created: 2018-09-20T09:55:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-10T09:37:03.000Z (over 4 years ago)
- Last Synced: 2025-03-20T21:15:08.053Z (2 months ago)
- Topics: cpp, csharp, dll, exporting-cpp, native-plugin, unity
- Language: C++
- Homepage:
- Size: 29.1 MB
- Stars: 13
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UnityCPPClassDLLExample
Example exporting CPP class as DLL and importing in Unity C# as native plugin
There will be times when you want to run your C++ code inside Unity. So you will need to export the code into DLL format and then import it in Unity. This project is showing you an example of exporting a **Student** class written in C++, and import it in Unity C#.
## How to run NOW
Open Unity project `TestImportCPPClass` and press Play button or Ctrl+P. You will see the output is being printed in the console.

Then take a look at [TestImportCPPClass/Assets/TestImportCPPClass.cs](TestImportCPPClass/Assets/TestImportCPPClass.cs) to see how it is implemented.
You will see quickly that it's just a bunch of function calls to the DLL files.## How to reproduce result from scratch
1. Open Visual Studio 2017 and create a new DLL project.
2. Copy content of file `TestExportCPPClass.cpp` from `TestExportCPPClass/TestExportCPPClass/` directory.
3. Go to Build -> Build solution, make sure the build is set to x64 to get a
`dll` file ready for Unity project with x64 architecture.
(Optional: You could also build x86 version too)
4. Create a new Unity project.
5. Copy `dll` file from `TestExportCPPClass/x64/Debug` into Unity's `Assets/Plugins/x86_64` directory.
6. Open Unity project and copy the C# script from `TestImportCPPClass/Assets/` directory.
7. Create an empty GameObject in the scene and attach the script to it.
8. Press Play button to see the output in the console.
## Related resources