Use LINQ in UNITY Games

LINQ makes it easier for game developers to develop games

Benny
Bina Nusantara IT Division
3 min readDec 27, 2021

--

Image from ShutterStock

Are you developing a game that uses a lot of data? When you develop a game that has a lot of data, you must have difficulty managing that data. There may be many ways and algorithms that can be used, but it will take up a lot of your time.

Let’s speed up our game development process to manage that troublesome data using LINQ.

What is LINQ?

LINQ (Language Integrated Query) is uniform query syntax in C# to retrieve data from different sources and formats. It is integrated in C#, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

Image from www.tutorialsteacher.com

Getting Started

To be able to use LINQ in your C# script, it’s very simple. You just need to add a namespace like this.

using System.Linq;

How to use LINQ in your code

LINQ can be used in 2 ways, including Method syntax and Query syntax. Here’s how to use the two syntaxes, as an example we want to get an item of type “Weapon”.

Method syntax

var item = listItem.Where(x => x.itemType.Equals("Weapon"));

Query syntax

var item = from x in listItem
where x.itemType.Equals("Weapon")
select x;

Let’s see how it works in Unity

Let’s create a store system where there will be a feature to search for items by type. The first step, of course, is to create a new scene. After that, make the UI look like the image below.

Simple Shop UI

And don’t forget to create a GameObject which we will use as a place to display our list of items. The GameObject that we will use is a UI Button and we design it as shown below.

Item Button Object

Now we will need to create a script to help us attach our item property to the Button. The script can be made like this.

ItemButton.cs

Usually I use Query syntax more often than Method syntax. The reason I use it is because the Query syntax is more similar to SQL Query in general hence increases code readability. So, let’s create a main script that will be used to process item search using LINQ.

TutorialLINQ.cs

As you can see, I am using the Query syntax in my LINQ usage. Maybe someone is confused with the word “data” in the Query syntax script snippet. Don’t worry, it’s just a variable name for a data which in this case is item data. You can use any name other than “data” like “item”, “x”, “someItem”, or whatever you like.

Then, how to apply it if you use Method syntax? It can be done using code like this.

var theData = listItem.Where(data => data.itemType.Equals(itemTypeList.captionText.text)).ToList();

Okay now let’s try to run the game and see the results.

GIF by Author

Conclusion

LINQ is a very useful feature for game developers. LINQ can be used in many ways such as searching, filtering, sorting, and much more, and all that can be done easily without having to think about complex algorithms. Outside of game development, LINQ is also widely used in the development of other applications that use the C# language.

--

--