Tag Archives: Unity

A better method to recalculate normals in Unity – Part 2

It’s been over 2 years since I posted about a method to recalculate normals in Unity that fixes on some of the issues of Unity’s default RecalculateNormals()  method. I’ve used this algorithm (and similar variations) myself in non-Unity projects and I’ve since made minor adjustments, but I never bothered to update the Unity version of the code. Someone recently reported to me that it fails when working with meshes with multiple materials, so I decided to go ahead and update it.

Continue reading

Unity vector swizzle operations

Swizzle operators are convenient syntactic sugar for creating new vectors based on the combination of elements of other vectors. They are used in shading languages quite a lot.

Example:

This is equal to:

They make more sense if you want to do some composition like so:

GLSL allows you to supply any amount of vectors or scalars in a vector constructor, as long as the count of all elements matches the dimension of the created vector.

However, swizzling isn’t allowed in most non-shading vector libraries. So, why not write simple extension methods that give that functionality to Unity?

Continue reading

New project

I have been trying to start working a new game, but I’ve just not had enough free time since I started working for Mcor Technologies. Each time I sit down to think about the design of a game, it eventually turns out to be very ambitious in every department. I have planned out a couple of games with promising ideas, and I intend to work on them… eventually. But, that probably won’t happen while I work solo, especially due to the fact I’m mostly a programmer and not an artist.

There’s also another problem I was not expecting: I don’t have enough motivation to work on 3D games (which all my good ideas are based on), because I work with 3D programming every day at work  for a non-gaming application. Not enough 3D juice left in me, I suppose.

For this reason, I thought it’d be a good idea to go back to the basics and work on a very simple, sprite-based puzzle game. I’ve laid out the basic design, did some of the programming and drew a few of the sprites. There is a title, but I won’t mention it until I have something more substantial to show.

For now, here is a walking Troll. This is the controllable character in the game:

troll_walk_right
3x size

Serializing delegates in Unity

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.

Continue reading

Custom Serialization with ISerializationCallbackReceiver

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: OnBeforeSerialize() and OnAfterDeserialize(). These methods are automatically called when that class inherits from UnityEngine.Object or is marked with the [Serializable] attribute.

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.

Continue reading

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