Controlling ScriptableObject instantiation

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.

Continue reading

Having fun with yield

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.

Continue reading

Serialization in Unity: Introduction

Serializing is the process in which a binary object (i.e. a class or a struct) is converted to a binary stream. Deserializing is the process in which an object is created from a binary stream. In layman’s terms, this is just a fancy schmancy way to say that a class or a struct is Saved and then Loaded from memory (be it virtual memory or an actual file on disk).

Warning: This does not necessarily mean that serialization is only used when you’re trying to save or load the game to or from a disk! If you’re not planning to have that in your game, it doesn’t make this article any less important. The fact is, Unity serializes and deserializes data all the time, so you have already used serialization whether you’ve realized it or not. And, if you don’t know what you’re doing, serialization can fail  when you need to use custom classes (or even many default .NET classes), leading to very strange errors which seemingly come out of nowhere.

Continue reading

A better method to recalculate normals in Unity

A visible seam showing after recalculating normals in runtime.

You might have noticed that, for some meshes, calling Unity’s built-in function to   RecalculateNormals(), things look different (i.e. worse) than when calculating them from the import settings. A similar problem appears when recalculating normals after combining meshes, with obvious seams between them. For this post I’m going to show you how RecalculateNormals()  in Unity works and how and why it is very different from Normal calculation on importing a model. Moreover, I will offer you a fast solution that fixes this problem.

This article is also very useful to those who want to generate 3D meshes dynamically during gameplay and not just those who encountered this problem.

Continue reading

Tiling sprites in Unity

The current state of 2D in Unity is currently a bit underdeveloped, due to the fact it was only recently released. After all, Unity is still primarily a 3D engine. One of the obvious things that are missing right now is the ability to have tiled sprites. As of version 4.5.4, you cannot do that out of the box without some nasty trickery. Current solutions you find online will usually have you replicate a sprite multiple times, but that’s far from the best solution; a Sprite is not just an image, it’s its own entity that comes with additional overhead.

Continue reading

Hello World!

Behold, the secret project that I had been working on for the past few days: A new blog/site or whatever you want to call it. If you wish to know what this is about, then, well, go to the About page. Or click here.

Yes, I know I’m using the default WordPress theme, which is usually a shameful thing for anyone who bestows the title of “developer” upon themselves – let alone the title of a scheming developer – but I like it! It’s simple, clean, mobile-friendly and it looks good. Okay, maybe not too good by default, because I did tinker with it a little to get it to look nicer. And, in doing so, I redeemed myself.

There is nothing more that can be said here, except to stay tuned for actual content that should be coming. Any day now. See you soon!