Drag and Drop in Unity

Recently I got a question to help solve some problem in Drag and Drop implementation in Unity. So as a bonus I decided to make my example of how to implement that! ?

Drag and Drop mechanic is often used in mobile games, but you can also encounter then in PC games. But thanks to Unity’s Event System this implementation will work fine on both platforms! ?

So let’s get started!

First of all, we need to create UI for our example! It will look like that:

UI setup example

Great! We are over the hardest part!  ?

Continue reading “Drag and Drop in Unity”

Design Patterns in Unity

You might already notice that I wrote a lot about design patterns and examples of use for them in Unity. I am genuinely interested in writing clean code and designing project architecture. This brings me huge enjoyment and satisfaction, especially when the team is following project guidelines and everything looks so perfect. ?

Being a programmer means that you need to improve your skills constantly. So today let’s summarize things that I wrote on this blog so far in term of Design Patterns! ?

What is Design Pattern?

The first thing that you need to understand is what “design pattern” means. So design pattern stands for a programming concept which solves conceptual problems. It can be related to creating new objects, making the code more independent or modular, or even split your project into different layers of responsibility.

Continue reading “Design Patterns in Unity”

Events or Interfaces Listeners?

If you’re reading my posts and you’re checking my public repositories, you might notice that I’m implementing UI with Events that are passing input to the controllers. This is really convenient as you have a clear separation between logic and display layers.

Background

To recap what this UI implementation looks like, let me first explain where it comes from.

From the beginning, I always tried to make my code modular, but I couldn’t grasp how to do it in a really clean way. It was literally taking me years, multiple projects and few programming languages to get to digging into design patterns finally. With them, I started to understand more of what I was doing and how easy it was to start taking care of your code and separate different layers from each other! ?

Continue reading “Events or Interfaces Listeners?”

Simple MVC for Unity

There are a lot of different implementations of MVC design pattern available for Unity, but I’ve noticed one problem with most of them.
They require a lot of knowledge about programming in general, and some of them use some fancy techniques which unnecessarily complicates your code.

And who wants to have hard to read and maintain code? ?

MVC

MVC stands for ModelViewController and is one of most used design pattern in IT, but not necessarily in Game Dev. This design pattern is used to separate your code based on use.

Here we have three parts of this pattern:
Model – these are classes responsible for storing and handling data.
View – these are responsible for handling displaying data and receiving input from the user.
Controller – and these are responsible for the logic behind the app in general. They also handle respond to the input from view, and they are passing data into views.

Continue reading “Simple MVC for Unity”

Use strings in a clean way!

By the title of this post, you might already know what I’m going to tell you today. You might think that using strings in code might not be that harmful to your project but believe me, it will.

Why?

Each advice you get should start with the question “why?”. So let me explain why you should care about using strings in your code in a proper way! ?

The first of all, you shouldn’t hardcode values in your code in the first place! Of course, this is not always possible, but the programmer should always strive for greatness. ?

When using a lot of strings in your code, it’s starting to get messy. You have to remember where you put specific text, and if you are using the same strings in multiple places, then you have a problem. Especially when you want to make some changes… ?

Continue reading “Use strings in a clean way!”