Unity is a great tool for game development; however, the engine stands over antiquated foundations.

The engine is built on Mono, an open-source implementation of the .NET framework created by Microsoft. The problem is that Unity works with .NET 2.0 (both complete and a subset) , while its current version is 4.5.

One of the features of the most recent version of .NET, not included in Unity, is a Tuple type. More often than not, there’s always that special moment while programming when you need to return more than one element. You could simply use a List or an Array, but all the elements in such collections must be of the same type. You could also use structs, but they are not serializable by Unity.

After some research (here and there) and work, I managed to create a Tuple class that would satisfy my programming needs. The code is included in a set of tools and scripts for Unity that I’ve been maintaining, available here: Unitilities.

This post will focus on the classic duple, the expansion for more dimensions is left up to the reader (and available on the aforementioned link).

 This Tuple is generic and will work with any types of objects. You could have, for example, a tuple of an int and a string.

The downside is that Unity’s Inspector doesn’t get along with certain elements (such as Dictionaries and this Tuple), because of their generic nature. The solution I’ve found so far is to create a new class that derives from Tuple. For example, for a tuple of two int, that class would be:

A collection of Tuples can also be sorted with no greater hassle, without the need of an IComparer passed to the Sort function.

As a bonus, here are the functions to get the min and max elements of a tuple that has elements of the same type (given that the element’s type is comparable):

I hope this codes serves well for anyone in the need of something like it. Any suggestion or bug report will be welcomed.

Share.

About Author

½ computer engineer + ½ videogame developer + ½ gamer. I eat my pizzas 8-bits at a time and develop games for a living.

1 Comment

Leave A Reply