You can compile libraries with Emscripten and use the libraries in Unity. It can be more beneficial to call functions from libraries that encapsulate C++ code rather than C++ code directly from the plug-inA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
See in Glossary directory. The benefits include:
To compile a static library that you can use as a Unity plug-in:
You must download the Emscripten SDK that matches your version of Unity.
To find the appropriate version of Emscripten for your Unity version, refer to Web native plug-ins for Emscripten.
For download links and more information about how to install the Emscripten SDK, refer to Download and install (Emscripten).
Emscripten works as a replacement for the gcc or clang compiler. If you have existing C/C++ code, you need to make the following changes in your C++ project:
For more information, refer to the Emscripten documentation on Compiling and Running Projects.
You might need to add some compiler options to your C/C++ project if your Unity project has the following Player settings (menu: Edit > Project Settings > Player).
Property | Description |
---|---|
Enable Exceptions | If this property is set to None, add the compiler option “-fno-exceptions”. |
Enable Native C/C++ Multithreading | If this property is enabled, include the compiler option “-pthread”. |
Enable WebAssembly 2023 | If this property is enabled, include the following compiler options:
|
Compile your static library files and then import your files into your Unity project in the Assets/Plugins
folder.
Note: If you don’t have a Plugins folder, you need to create your own.
You can call your static library functions from your Unity C# scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary. The way to call the functions is the same as if you call a C or C++ function from C# scripts. For an example, refer to Call C/C++/C# functions from Unity C# scripts.