Singleton in Unity – Love or hate?

Those who know me, know that I have a rough relationship with the Singleton Design Pattern. Mostly I don’t like using it very often or even at all. This approach has a lot of benefits, starting with cleaner code architecture, less coupling, and in general fewer problems. ?

When to use it?

So when you would implement singleton? When you would like to have only one instance of a specific object. Example of what could be a custom resource manager where you store or load files that you downloaded from the web or any other data storage.

Continue reading “Singleton in Unity – Love or hate?”

How to implement Object Pooling in Unity

Object pooling is one of the easiest forms of optimization in Unity. Just recently we implemented the Factory Design Pattern which is related to today’s subject.

Object Pooling?

Let’s begin by explaining what Object Pooling is. So this is an optimization practice which is based on instance reusability. In Factory, we created a new instance of prefab, and later we destroyed it which is not an issue when we want to create just a few instances.

Problems start arriving when we create a lot of new instances, for example when shooting bullets from a gun, or we spawn a lot of special effects.

Continue reading “How to implement Object Pooling in Unity”

Handling UI with State Machine in Unity

One of my favorite use of State Machine design pattern is using states as different controllers of a game or an app. And today I’m going to present to you how you can implement such a thing in your Unity project! ?

As previously on AI subject with State Machine, we won’t change the implementation of State Machine and Base State scripts. They are staying the same. Maybe besides one reference and function call… ?

Continue reading “Handling UI with State Machine in Unity”

Using State Machine for AI in Unity

Just a few days ago I made a post about implementing State Machine in Unity. I know that having design pattern implementation and nothing else is less useful, so I decided to make small AI that will use State Machine in a bigger context.

What this example is?

In the example that I’ve prepared we are making logic for units in some RTS game. By default, they are aggressive, and they are trying to get rid of the enemies nearby them.
We are going to have 2 types of units. First one will be a knight who will attack other groups. The second type will siege machine that will only be able to attack buildings. Both will have the same components, but parameters might differ a little bit.

Continue reading “Using State Machine for AI in Unity”