Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

unity-output-console

Then take a look at 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.

    TestExportCPPClass.cpp

  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)

    dll-directory

  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.

    TestImportCPPClass.cs

  7. Create an empty GameObject in the scene and attach the script to it.

    unity-hierarchy

  8. Press Play button to see the output in the console.

    unity-output-console

Related resources

https://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-sharp-code/315064#315064

https://github.com/wm4n/unity-cpp-lib

https://github.com/Meach/UnitySimpleNativeLibrary

About

💡 Example exporting CPP class as DLL and importing in Unity C# as native plugin

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages