Once you have organized your library, then it’s time to package it! The Unity command-line interface includes a command to “export” a package file which can then be imported into other Unity projects. From there, any C/C++ function declared from C# can be called at runtime. For other platforms, use the name of the plugin, removing the “lib” prefix (if any) and file extension.
CREATE REACT NATIVE GAME CODE
Myplugin.c: - CODE language-cpp - // declare functions with C linkage to ensure name-mangling does not break references extern "C" įor precompiled libraries, the import declaration differs per-platform. These functions are considered “internal” because they will be compiled alongside the C++ code generated by Unity projects rather than into a separate library. The functions defined in the source file can then be declared as static references in your C# scripts and referenced like regular functions, declaring that the function is imported from _Internal. Otherwise if the source differs for different platforms, include it in the platform-specific Plugins directories: - CODE language-plaintext - project/ └ Assets/ └ Plugins/ └ Android/myplugin.cpp └ iOS/myplugin.cpp └ OSX/myplugin.cpp └ Windows/myplugin.cpp If the source is the same on every platform, it can be included in the top-level Plugins directory: - CODE language-plaintext - project/ └ Assets/ └ myplugin.c If you are using the IL2CPP backend to build your project, you can include C/C++ source files directly in your plugin directories to have the file compiled and included in the project by Unity itself. There are two ways to add native code to a plugin. If you use this scheme, Unity will generally automatically detect which platform is needed for a particular file. Platform-specific resources are easy to manage when placed in separate Assets/Plugins directories, with a defined name for each platform: Create a new empty project using either the Unity editor or the command-line interface: - CODE language-plaintext - /path/to/Unity -batchmode -createProject MyPluginĪny scripts which should be accessible from the Unity editor should be included inside the plugin directory in Assets/. Plugins have the same structure as other Unity projects, and creating a new project is the best place to start for a new plugin. NET APIs (“managed” plugins) or in C-based languages (“unmanaged” or “native” plugins). While functionality is generally added to Unity games in the form of “scripts”, which are C# source files which are developed in the Unity environment, code and other resources can also be compiled and packaged for use in several projects and these packages are called “plugins”. This post is a guide to including platform-specific code in your game while supporting multiple platforms. While Unity games are primarily scripted in C#, the game engine also supports platform-specific code written in C-based languages, which is great for interacting with APIs specific to the targeted platform, reusing third-party libraries written in C or C++, and low-level graphics processing. Unity is a cross-platform game engine and development environment for creating 2D and 3D games for Windows, Linux, macOS, mobile devices, web browsers, and gaming consoles.