Today’s article continues the series by adding support for C++ to call the various overloaded operators and indexers that are written in C#. This includes support for all 24 overloadable operators in C# plus the explicit
and implicit
type conversion operators. Indexers aren’t quite overloaded operators, but they allow for array-like indexing into C# types so they’re included today. Read on to learn how all this support was implemented in the GitHub project!
Archive for category Unity
Like them or not, exceptions are the standard way of handling programming errors in C#. We need to be able to catch C# exceptions in our C++ code so we can gracefully recover from them. Likewise, we need uncaught C++ exceptions to behave like unhandled C# exceptions: display an error and move on instead of crashing. Today’s article continues the series by implementing both those features in the GitHub project and explaining how that implementation was done.
The series to build a viable system to write Unity scripts in C++ continues! While these 11 articles have covered a lot of ground toward making a usable C++ scripting system, there’s still a lot to do. Writing the code for these articles takes quite a lot of time, so today I’m officially calling for collaborators on the GitHub project. If you’d like to join in, please leave a comment, send an e-mail, or submit a pull request. There’s plenty to do and your help would be greatly appreciated! Aside from that, today’s article is all about adding support for struct and enum types so we can use types like Vector3
and TextureFormat
from our C++ scripts.
C# APIs are chock-full of generics. Generic types, generic method parameters, generic return types, generic fields, generic properties, deriving from generic types, and generic constructors. We can find all of these in the Unity and .NET APIs. Some are more frequent than others, but we’re going to need support for all of them to make C++ scripting a viable alternative to C#. Today’s article continues the series by adding just that: support for all of these kinds of generics. Let’s dive into how to use them as well as some bonus items added to the project this week.
It’s been quite a while in the series since we’ve added any fundamental C# language features. Today we’ll address one of the limitations of the C#/C++ communication: the lack of support for out
and ref
parameters. This is important as they’re commonly used by both the Unity API and .NET and we’d like C++ to be able to call functions with these kinds of parameters. So let’s delve into what it means for C++ to use out
and ref
parameters and see how to implement support for that across the language boundary.
The series continues today to fill a small, but important gap in the C++ plugin’s build script. Unity helpfully provides symbols like UNITY_IOS
for us to check with #if
. This lets us add and remove blocks of code that should only be present in a certain build of the game. We’d like the same functionality in C++ that Unity provides to C#, so today we’ll upgrade the build script to allow that.
The series continues this week by addressing a pretty important issue. Previously, we were limited to doing all our work in just two C++ functions: PluginMain
and PluginUpdate
. This isn’t at all the normal way to work in Unity. It’d be a lot more natural to write our code in MonoBehaviour
classes. So today we’ll come up with some tricks to allow us to write our MonoBehaviour
code in C++ so we are truly scripting in C++.
Today we’ll continue the series by addressing a nagging problem: how do we build the C++ plugin? With C# we inherit, for better or worse, Unity’s build system where we just edit .cs
files and press the play button. This doesn’t work with C++, so we’ll need to build something similar that’s just as easy to use.
Last week in the series we took a step back to verify that the C++ plugin’s performance was acceptable. With that confirmed, we’ll continue this week by making our programming lives easier. One pain point so far has been with exposing new Unity APIs to C++. It’s not that it’s difficult to do this, but there’s a lot of boilerplate required. That boilerplate takes time to write and it’s easy to make mistakes copying and pasting existing functions. So this week’s article introduces a code generator that will write the boilerplate for us! We’ll also reorganize the project a little so the code that supports C++ scripting is separated away from our game code. That’ll make it easy to add support for C++ scripting to any Unity project.
In the first three parts of this series, we focused on setting up a development environment that makes it easy and safe to write our game code in C++. Today’s article takes a step back to assess where we are in terms of performance. Is what we’ve built so far viable, or are the calls between C# and C++ too expensive? To find out we’ll use the existing framework to write some simple performance tests.