You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-17Lines changed: 43 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,31 +2,58 @@
2
2
3
3
A library to allow writing Unity scripts in native code: C, C++, assembly.
4
4
5
+
## Purpose
6
+
7
+
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.
8
+
9
+
## Goals
10
+
11
+
* Make scripting in C++ as easy as C#
12
+
* Low performance overhead
13
+
* Easy integration with any Unity project
14
+
* Fast compile, build, and code generation times
15
+
5
16
# Reasons to Prefer C++ Over C# #
6
17
7
-
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. You can even code with compiler intrinsics or assembly to directly write machine code and take advantage of CPU features like [SIMD](http://jacksondunstan.com/articles/3890) and hardware AES encryption for massive performance gains.
18
+
## Fast Compile Times
19
+
20
+
C++ compiles much more quickly than C#. Moderate size projects typically take 10+ seconds to compile in C# but only about 1 second to compile in C++. Faster compilation adds up over time to productivity gains. Quicker iteration times make it easier to stay in the "flow" of programming.
21
+
22
+
## Fast Device Build Times
23
+
24
+
Changing one line of C# code requires you to make a new build of the game. Typical iOS build times tend to be at least 10 minutes because IL2CPP has to run and then Xcode has to compile a huge amount of C++.
25
+
26
+
By using C++, we can compile the game as a C++ plugin in about 1 second, swap the plugin into the Xcode project, and then immediately run the game. That's a huge productivity boost!
27
+
28
+
## No Garbage Collector
29
+
30
+
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.
8
31
9
-
C++ is also a much larger language than C# and some developers will prefer having more tools at their disposal. Here are a few differences:
32
+
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](http://jacksondunstan.com/articles/3829), 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](http://jacksondunstan.com/articles/3850).
33
+
34
+
C++ has no required garbage collector and features optional automatic memory management via "smart pointer" types like [shared_ptr](http://en.cppreference.com/w/cpp/memory/shared_ptr). It offers excellent alternatives to Unity's primitive garbage collector.
35
+
36
+
## Total Control
37
+
38
+
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](http://jacksondunstan.com/articles/3890) and hardware AES encryption for massive performance gains.
39
+
40
+
## More Features
41
+
42
+
C++ is a much larger language than C# and some developers will prefer having more tools at their disposal. Here are a few differences:
10
43
11
44
* Its template system is much more powerful than C# generics
12
45
* There are macros for extreme flexibility by generating code
13
-
*Function pointers instead of just delegates
46
+
*Cheap function pointers instead of heavyweight delegates
14
47
* No-overhead [algorithms](http://en.cppreference.com/w/cpp/algorithm) instead of LINQ
15
48
* Bit fields for easy memory savings
16
-
* Pointers and never-null references instead of just managed referneces
49
+
* Pointers and never-null references instead of just managed references
17
50
* Much more. C++ is huge.
18
51
19
-
There are also some problems with C# code running under Unity that you'll automatically avoid. For one, Unity's garbage collector is very slow. It runs on the main thread, which blocks rendering and input handling. It collects all objects at once, which causes frame hitches. And it fragments memory, so eventually you may run out and crash.
20
-
21
-
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](http://jacksondunstan.com/articles/3829), which essentially make memory management manual. You've also got to avoid boxing value types to managed types, `foreach` loops in some situations, and various other [gotchas](http://jacksondunstan.com/articles/3850).
22
-
23
-
C++ has no required garbage collector and features optional automatic memory management via "smart pointer" types like [shared_ptr](http://en.cppreference.com/w/cpp/memory/shared_ptr). It offers an excellent alternative to Unity's primitive garbage collector.
52
+
## No IL2CPP Surprises
24
53
25
54
While IL2CPP transforms C# into C++ already, it generates a lot of overhead. There are many [surprises](http://jacksondunstan.com/articles/3916) 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.
26
55
27
-
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.
28
-
29
-
# Features
56
+
# UnityNativeScripting Features
30
57
31
58
* Supports Windows, macOS, iOS, and Android (editor and standalone)
32
59
* Plays nice with other C# scripts- no need to use 100% C++
@@ -55,7 +82,7 @@ This project aims to give you a viable alternative to C#. Scripting in C++ isn't
tl;dr - Most projects will not be noticeably impacted by C++ overhead and many projects will benefit from reducing garbage collection and IL2CPP overhead.
85
+
tl;dr - Most projects will not be noticeably impacted by C++ overhead and many projects will benefit from reducing garbage collection, eliminating IL2CPP overhead, and access to compiler intrinsics and assembly.
59
86
60
87
# Project Structure
61
88
@@ -77,14 +104,12 @@ With C++, the workflow looks like this:
77
104
3. Switch to the Unity editor window. Nothing to compile.
78
105
4. Run the game
79
106
80
-
One of the project's goals is to make it just as easy to work with C++ as it is to work with C#, if not easier.
81
-
82
107
# Getting Started
83
108
84
109
1. Download or clone this repo
85
110
2. Copy everything in `Unity/Assets` directory to your Unity project's `Assets` directory
86
111
3. Copy the `Unity/CppSource` directory to your Unity project directory
87
-
4. Edit `NativeScriptTypes.json` and specify what parts of the Unity API you want access to from C++. Some examples are provided, but feel free to delete them if you're not using those features.
112
+
4. Edit `NativeScriptTypes.json` and specify what parts of the Unity, .NET, and custom DLL APIs you want access to from C++.
88
113
5. Edit `Unity/CppSource/Game/Game.cpp` 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.
89
114
90
115
# Building the C++ Plugin
@@ -159,12 +184,13 @@ The code generator supports:
159
184
* Enumerations
160
185
* Exceptions
161
186
* Overloaded operators
187
+
* Arrays (single- and multi-dimensional)
162
188
163
189
The code generator does not support (yet):
164
190
165
-
* Arrays (single- or multi-dimensional)
166
191
* Delegates
167
192
*`MonoBehaviour` contents (e.g. fields) except for "message" functions
0 commit comments