Reader small image

You're reading from  Hands-On Unity Game Development - Fourth Edition

Product typeBook
Published inJan 2024
PublisherPackt
ISBN-139781835085714
Edition4th Edition
Tools
Right arrow
Authors (2):
Nicolas Alejandro Borromeo
Nicolas Alejandro Borromeo
author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo

Juan Gabriel Gomila Salas
Juan Gabriel Gomila Salas
author image
Juan Gabriel Gomila Salas

Juan graduated in mathematics with a master's degree in Teacher Training, specializing in Game Design for social casino video games and as a video game developer using Unity and Unreal Engine for both PC and mobile. He was an online teacher on Udemy from 2015 to 2022 and subsequently on his own platform Frogames Training with over 150 online courses and over 500,000 students across 130 countries.
Read more about Juan Gabriel Gomila Salas

View More author details
Right arrow

Massive Worlds: Introduction to DOTS

While Unity is a powerful and performant engine (when used properly), there’s a limit on how many GameObjects you can have without reaching performance limits. While the Object-Oriented Programming (OOP) paradigm provides a convenient way to code a game, due to its internal works, it won’t always take advantage of all the modern CPU features, like caching, Single Instruction Multiple Data (SIMD) operations, and multi-threading. While it isn’t impossible to use such features in OOP, doing so is not a trivial task and can lead to difficult-to-track bugs. Unity’s response to that issue was releasing the first production-ready version of the DOTS packages, which will help us with these problems.

In this chapter, we will examine the following build concepts:

  • Understanding what DOTS is
  • Creating our first DOTS game
  • Creating gameplay in DOTS

Let’s start by getting an idea of what DOTS...

Understanding what DOTS is

The Unity Data-Oriented Technology Stack (DOTS) is a set of Unity packages that allows us to write data-oriented code easily. While there are plenty of packages in the DOTS stack, let’s focus on the three key ones that serve as pillars for the rest:

  • Entities: Unity takes on the ECS pattern. It replaces GameObjects and MonoBehaviours with Entities, Components, and Systems (ECS, a paradigm in Unity that separates data (components) from logic (systems), enhancing performance and scalability), a cache-friendly way to store and update our scene objects. It provides considerable performance boosts, especially in games with lots of objects.
  • Jobs: The Unity way to create multi-threaded code. It groups data and code that processes jobs. They are executed in parallel thanks to the job scheduler, which guarantees that explicit dependencies between jobs are respected. This avoids classic multi-threading issues like deadlocks and race conditions...

Creating our first DOTS game

There is plenty to learn in order to create performant games with DOTS, so let’s start by creating a simple project featuring cubes moving forward. Emphasis on cubes – we are going to have thousands of them! I know it is not the most exciting project, but bear with me, as in the Creating Gameplay in the DOTS section in this chapter, we will convert it into an actual (but simple) game.

In this section, we will examine the following DOTS concepts:

  • Creating a DOTS project
  • Creating entities with subscenes
  • Creating components and bakers
  • Creating systems using Jobs and Burst
  • Debugging systems jobs and queries

Let’s start by discussing how to set up a project to be compatible with DOTS.

Creating a DOTS project

The first step for creating a DOTS project is the one you should already know by this point in the book: creating a brand-new URP project. If you are not sure how to do it, please...

Creating gameplay in DOTS

Lots of things we do in MonoBehaviours, like instantiating objects or getting other object data (for example, their position), are quite differently implemented in DOTS. In this section, we will examine how to achieve these things in DOTS:

  • Moving using input and tag components
  • Creating seeking missiles using component lookups
  • Destroying the character with entity command buffers
  • Dealing with errors in bursted code
  • Instantiating missiles with entity Prefabs
  • Making the camera follow our character
  • Exploring other DOTS features

Let’s start by discussing how to modify our movement code to respond to input.

Moving using input and tag components

Let’s start creating gameplay by using input to move the cube. We need to change our movement job to provide it with the input values. We will do this the same way as we provided delta time by passing the input values to the job. In the next image...

Summary

In this chapter, we learned how to start using DOTS by creating our own components and systems to add gameplay to a simple game. We saw how to use input, and the DOTS transform API to move and rotate objects, and we also saw how to spawn and destroy objects.

DOTS is a new engine on its own, running inside Unity. The way it works is vastly different, and except for a few systems like the rendering pipelines, almost all DOTS features work very differently from their GameObject counterparts. Also, DOTS is still quite new, meaning it still has missing features and could be subjected to substantial changes in the next versions.

This makes it a possible risk for creating new projects, but on the other hand, the performance it can achieve could be decisive for big and ambitious projects. It is an interesting piece of technology that can revolutionize game development, and every day, it is getting more powerful and stable. It’s a good time to start learning about it...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Unity Game Development - Fourth Edition
Published in: Jan 2024Publisher: PacktISBN-13: 9781835085714
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo

author image
Juan Gabriel Gomila Salas

Juan graduated in mathematics with a master's degree in Teacher Training, specializing in Game Design for social casino video games and as a video game developer using Unity and Unreal Engine for both PC and mobile. He was an online teacher on Udemy from 2015 to 2022 and subsequently on his own platform Frogames Training with over 150 online courses and over 500,000 students across 130 countries.
Read more about Juan Gabriel Gomila Salas