I see the topic of choosing the “right language” when it comes to games brought up too many times in discussions all over the internet. The argument is usually between C++, C#, and Java. People do get religious about their favourite languages. At its heart this is an inherently flawed question, because the real decision should be on what framework or engine is best suited for a particular game, and that choice will usually dictate what language to use. Of course, the languages supported by a specific framework or engine can definitely be an important factor that may affect your decision.
And the truth is, assuming you have a choice, Java comes short in the race. When this is pointed out, some people erroneously claim that it’s a matter of “opinion”. That’d be true if it wasn’t so easy to prove that Java is ill-suited for games development. “You’d use the right tool for the right job” is very noble statement, and one that applies a lot in any kind of software development. But it’s also very abused and it gives the false impression that Java is sometimes the right tool.
Delegates are one of the nicer features of C#. They’re essentially high-level and type-safe references to methods. But, like pretty much every fancy C# feature, delegate values are not serializable by default. If you hold a reference to a delegate, it will be lost during code hot-reload. I will show you a way to serialize them and explain how they work.
Note: This solution is inspired by the uFAction tool in Unity, which I am not affiliated with. Go check it out here.
Starting from Unity 4.5, any class can be made to inherit from
ISerializationCallbackReceiver. This allows us to write methods that can be used to control seialization and deserialization, and it demands from the class to implement two public methods:
OnAfterDeserialize(). These methods are automatically called when that class inherits from
UnityEngine.Object or is marked with the
The idea behind this concept is both simple and powerful, but there are certain things you need to have in mind. Not only I will show you how to use this interface, I will also provide some information on things you should avoid or on things that won’t work as expected.
ScriptableObjects become (unfortunately) necessary when you start developing editor tools. That, and every other class that inherits from
UnityEngine.Object, has the unique ability to maintain proper references after deserialization. Every other class creates a copy per reference (as if it were a struct) after deserialization, which can be very troublesome.
One of the least pleasant aspects of ScriptableObject is that you cannot control their instantiation. Using their constructor to create them does not work as expected. The only proper way to create a ScriptableObject from scratch is to call the
ScriptableObject.CreateInstance<DerivedType>() static method, where
DerivedType is a type which derives from
ScriptableObject. Unfortunately, This method does not take any parameters. I will show you an easy way to help control their instantiation, so that the users of your ScriptableObject classes will be less prone to use them incorrectly.
C# Properties are one of the nicer features of C#, but there are things to keep in mind when using them in Unity. The framework doesn’t seem to like them too much. I will show you what the Unity-specific problems on using properties are and how to easily address them.
I love C#. I’m not necessarily a fan of Microsoft or any company; I just think that, as a language, it’s far better than Java, its older cousin. Don’t get me wrong, I know that there is not one language to rule them all, and that it’s more about the framework and not the language. But, from a purely language feature point of view, C# is clearly much more expressive (unless you are biased against Microsoft) and it manages to combine multiple programming paradigms pretty well without being a mess (C++, I’m looking at you).
The LINQ library is one of its strongest features, but it wouldn’t be as amazing without the special keyword
yield which it uses internally. I will show you how to use that to do fun stuff in C#, and how Unity takes advantage of its power to run co-routines.