A library to allow writing Unity scripts in native code: C, C++, assembly.
This project aims to give you a viable alternative to C#. Scripting in C++ isn't right for all parts of every project, but now it's an option.
Changing one line of C# code requires you to make a new build of the game. Typical Android build times tend to be at least 10 minutes because IL2CPP has to run and then a huge amount of C++ must be compiled.
By using C++, we can compile the game as a C++ plugin in about 1 second, swap the plugin into the APK, and then immediately install and run the game. That's a huge productivity boost!
C++ compiles much more quickly than C#. Incremental builds when just one file changes-- the most common builds-- can be 15x faster than with C#. Faster compilation adds up over time to productivity gains. Quicker iteration times make it easier to stay in the "flow" of programming.
Unity's garbage collector is mandatory and has a lot of problems. It's slow, runs on the main thread, collects all garbage at once, fragments the heap, and never shrinks the heap. So your game will experience "frame hitches" and eventually you'll run out of memory and crash.
A significant amount of effort is required to work around the GC and the resulting code is difficult to maintain and slow. This includes techniques like object pools, which essentially make memory management manual. You've also got to avoid boxing value types like int
to to managed types like object
, not use foreach
loops in some situations, and various other gotchas.
C++ has no required garbage collector and features optional automatic memory management via "smart pointer" types like shared_ptr. It offers excellent alternatives to Unity's primitive garbage collector.
While using some .NET APIs will still involve garbage creation, the problem is contained to only those APIs rather than being a pervasive issue for all your code.
By using C++ directly, you gain complete control over the code the CPU will execute. It's much easier to generate optimal code with a C++ compiler than with a C# compiler, IL2CPP, and finally a C++ compiler. Cut out the middle-man and you can take advantage of compiler intrinsics or assembly to directly write machine code using powerful CPU features like SIMD and hardware AES encryption for massive performance gains.
C++ is a much larger language than C# and some developers will prefer having more tools at their disposal. Here are a few differences:
While IL2CPP transforms C# into C++ already, it generates a lot of overhead. There are many surprises if you read through the generated C++. For example, there's overhead for any function using a static variable and an extra two pointers are stored at the beginning of every class. The same goes for all sorts of features such as sizeof()
, mandatory null checks, and so forth. Instead, you could write C++ directly and not need to work around IL2CPP.
C++ is the standard language for video games as well as many other fields. By programming in C++ you can more easily transfer your skills and code to and from non-Unity projects. For example, you can avoid lock-in by using the same language (C++) that you'd use in the Unreal or Lumberyard engines.
GameObject go; Transform transform = go.GetTransform(); Vector3 position(1.0f, 2.0f, 3.0f); transform.SetPosition(position);
MonoBehaviour
messages in C++void MyScript::Start() { String message("MyScript has started"); Debug::Log(message); }
#if TARGET_OS_ANDROID
)The core of this project is a code generator. It generates C# and C++ code called "bindings" that make C# APIs available to C++ game code. It supports a wide range of language features:
class
struct
enum
Action
)decimal
get
and set
like obj.x
)get
and set
like obj[x]
)add
and remove
delegates)int
to object
and visa versa)out
and ref
parametersNote that the code generator does not yet support:
Array
, string
, and object
methods (e.g. GetHashCode
)params
parameter (a.k.a. "var args") passingTo configure the code generator, open Unity/Assets/NativeScriptTypes.json
and notice the existing examples. Add on to this file to expose more C# APIs from Unity, .NET, or custom DLLs to your C++ code.
To run the code generator, choose NativeScript > Generate Bindings
from the Unity editor.
Almost all projects will see a net performance win by reducing garbage collection, eliminating IL2CPP overhead, and access to compiler intrinsics and assembly. Calls from C++ into C# incur only a minor performance penalty. In the rare case that almost all of your code is calls to .NET APIs then you may experience a net performance loss.
Testing and benchmarks article
When scripting in C++, C# is used only as a "binding" layer so Unity can call C++ functions and C++ functions can call the Unity API. A code generator is used to generate most of these bindings according to the needs of your project.
All of your code, plus a few bindings, will exist in a single "native" C++ plugin. When you change your C++ code, you'll build this plugin and then play the game in the editor or in a deployed build (e.g. to an Android device). There won't be any C# code for Unity to compile unless you run the code generator, which is infrequent.
The standard C# workflow looks like this:
With C++, the workflow looks like this:
Unity/Assets
directory to your Unity project's Assets
directoryNativeScriptTypes.json
and specify what parts of the Unity, .NET, and custom DLL APIs you want access to from C++.Unity/Assets/CppSource/Game/Game.cpp
and Unity/Assets/CppSource/Game/Game.h
to create your game. Some example code is provided, but feel free to delete it. You can add more C++ source (.cpp
) and header (.h
) files here as your game grows./Applications/Utilities
cd /path/to/your/build/directory
cmake -G MyGenerator -DCMAKE_TOOLCHAIN_FILE=/path/to/your/project/CppSource/iOS.cmake /path/to/your/project/CppSource
. Replace MyGenerator
with the generator of your choice. To see the options, execute cmake --help
and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE.make
if you chose Unix Makefiles
as your generator or open NativeScript.xcodeproj
and click Product > Build
if you chose Xcode./Applications/Utilities
cd /path/to/your/build/directory
cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/CppSource
. Replace MyGenerator
with the generator of your choice. To see the options, execute cmake --help
and look at the list at the bottom. Common choices include "Unix Makefiles" to build from command line or "Xcode" to use Apple's IDE. Remove -DEDITOR=TRUE
for standalone builds.make
if you chose Unix Makefiles
as your generator or open NativeScript.xcodeproj
and click Product > Build
if you chose Xcode.cd /path/to/your/build/directory
cmake -G "Visual Studio VERSION YEAR Win64" -DEDITOR=TRUE /path/to/your/project/CppSource
. Replace VERSION
and YEAR
with the version of Visual Studio you want to use. To see the options, execute cmake --help
and look at the list at the bottom. For example, use "Visual Studio 15 2017 Win64"
for Visual Studio 2017. Any version, including Community, works just fine. Remove -DEDITOR=TRUE
for standalone builds. If you are using Visual Studio 2019, execute cmake -G "Visual Studio 16" -A "x64" -DEDITOR=TRUE /path/to/your/project/CppSource
instead.NativeScript.sln
and click Build > Build Solution
.cd /path/to/your/build/directory
cmake -G "MyGenerator" -DEDITOR=TRUE /path/to/your/project/CppSource
. Replace MyGenerator
with the generator of your choice. To see the options, execute cmake --help
and look at the list at the bottom. The most common choice is "Unix Makefiles" to build from command line, but there are IDE options too. Remove -DEDITOR=TRUE
for standalone builds.make
if you chose Unix Makefiles
as your generator.cd /path/to/your/build/directory
cmake -G MyGenerator -DANDROID_NDK=/path/to/android/ndk /path/to/your/project/CppSource
. Replace MyGenerator
with the generator of your choice. To see the options, execute cmake --help
and look at the list at the bottom. To make a build for any platform other than Android, omit the -DANDROID_NDK=/path/to/android/ndk
part.make
if you chose Unix Makefiles
as your generator.To update to a new version of this project, overwrite your Unity project's Assets/NativeScript
directory with this project's Unity/Assets/NativeScript
directory and re-run the code generator.
Articles by the author describing the development of this project.
Please feel free to fork and send pull requests or simply submit an issue for features or bug fixes.
All code is licensed MIT, which means it can usually be easily used in commercial and non-commercial applications.
All writing is licensed CC BY 4.0, which means it can be used as long as attribution is given.