Reader small image

You're reading from  Unity Game Optimization - Third Edition

Product typeBook
Published inNov 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781838556518
Edition3rd Edition
Languages
Tools
Right arrow
Authors (2):
Dr. Davide Aversa
Dr. Davide Aversa
author image
Dr. Davide Aversa

Dr. Davide Aversa holds a PhD in Artificial Intelligence (AI) and an MSc in AI and robotics from the University of Rome La Sapienza in Italy. He has a strong interest in AI for the development of interactive virtual agents and procedural content generation. He has served as a program committee member for video game-related conferences such as the IEEE conference on computational intelligence and games, and he also regularly participates in game-jam contests. He also writes a blog on game design and game development.
Read more about Dr. Davide Aversa

Chris Dickinson
Chris Dickinson
author image
Chris Dickinson

Chris Dickinson grew up in a quiet little corner of England with a strong passion for mathematics, science and, in particular, video games. He loved playing them, dissecting their gameplay, and trying to figure out how they worked. Watching his dad hack the hex code of a PC game to get around the early days of copy protection completely blew his mind! His passion for science won the battle at the time; however, after completing a master's degree in physics with electronics, he flew out to California to work in the field of scientific research in the heart of Silicon Valley. Shortly afterward, he had to admit to himself that research work was an unsuitable career path for his temperament. After firing resumes in all directions, he landed a job that finally set him on the correct course in the field of software engineering (this is not uncommon for physics grads, I hear). His time working as an automated tools developer for IPBX phone systems fit his temperament much better. Now he was figuring out complex chains of devices, helping its developers fix and improve them, and building tools of his own. Chris learned a lot about how to work with big, complex, real-time, event-based, user-input driven state machines (sounds familiar?). Being mostly self-taught at this point, Chris's passion for video games was flaring up again, pushing him to really figure out how video games were built. Once he felt confident enough, he returned to school for a bachelor's degree in game and simulation programming. By the time he was done, he was already hacking together his own (albeit rudimentary) game engines in C++ and regularly making use of those skills during his day job. However, if you want to build games, you should just build games, and not game engines. So, Chris picked his favorite publically available game engine at the time--an excellent little tool called Unity 3D--and started hammering out some games. After a brief stint of indie game development, Chris regretfully decided that the demands of that particular career path weren't for him, but the amount of knowledge he had accumulated in just a few short years was impressive by most standards, and he loved to make use of it in ways that enabled other developers with their creations. Since then, Chris has authored a tutorial book on game physics (Learning Game Physics with Bullet Physics and OpenGL, Packt Publishing) and two editions of a Unity performance optimization book (which you are currently reading). He has married the love of his life, Jamie, and works with some of the coolest modern technology as a software development engineer in Test (SDET) at Jaunt Inc. in San Mateo, CA, a Virtual Reality/Augmented Reality startup that focuses on delivering VR and AR experiences, such as 360 videos (and more!). Outside of work, Chris continues to fight an addiction to board games (particularly Battlestar: Galactica and Blood Rage), an obsession with Blizzard's Overwatch and Starcraft II, cater to the ever-growing list of demands from a pair of grumpy yet adorable cats, and gazing forlornly at the latest versions of Unity with a bunch of game ideas floating around on paper. Someday soon, when the time is right (and when he stops slacking off), his plans may come to fruition
Read more about Chris Dickinson

View More author details
Right arrow

The Data-Oriented Technology Stack

In recent years, we have seen a big push toward multithreading programming. The reason is obvious: while we have reached a technological limit on how fast a single core can go, we have discovered how to efficiently put thousands of cores into our hardware and run each piece of code in parallel to obtain a massive performance boost.

However, moving from single-thread programming to multithreading programming is not straightforward. Not every algorithm can easily be split into pieces and, even if it can, there are several details you need to take into account so as to avoid strange and unpredictable behaviors.

When the first version of Unity was released, back in 2005, massive multithreading was almost a futuristic scenario. However, fourteen years are the equivalent of a geological era in game development, and a game engine needs to adapt itself...

The problem of multithreading

Video games have great multithreading potential. In theory, every GameObject can be seen as a separate entity with its own life cycle and its own computation path. This would instantaneously increase your game performance with a lot of GameObject instances. Suppose that processing all the updates in the GameObject takes 1 ms. If you could have one thousand similar GameObject instances, that would take a full second but, if you can assign each update to a separate core, all the updates could run in parallel, and your total computation time would be exactly 1 ms. That represents a 100,000% speed boost!

Unfortunately, it is not so easy. As we said before, you cannot just assign a piece of code to a core and expect that everything keeps working. A big problem with writing multithreaded code is the risk of race conditions, deadlock, and bugs that are notoriously...

The Unity Job System

The big block in the DOTS that can provide us with a huge performance-enhancing feature is the C# Job System. Like all the other DOTS components, the feature is still in active development, but has been made public since Unity 2019.1, so it would be wise to start becoming familiar with it sooner rather than later, as it will introduce considerable changes to how Unity developers will be writing high-performance code:

As we will see, the difference in the quality of a game that uses this system versus one that doesn't might become very noticeable, which may cause some fragmentation within the Unity development community. It is in our best interests to understand and exploit the benefits of the new Job System so that our application will have the greatest potential for success.

The idea of the C# Job System is to be able to create simple tasks that run...

The new ECS

The ECS is a brave and ambitious attempt to redesign the core foundation of Unity's design: the GameObject-MonoBehaviour paradigm. As you can imagine, changing the base design pattern of every object in the game is not an easy task. So you may ask: Why?

There are several reasons for that. Let's look at some of them objectively:

  • First, as we said before, GameObject and MonoBehaviour are heavy objects; they carry a lot of internal code and data structures. The overhead introduced by GameObject instances and MonoBehaviour is large enough to limit the number of objects you can have on the screen more than the resources needed to render them. That's not a good thing for an abstraction model.
  • Second, MonoBehaviour instances are scattered in memory. This means that GameObject needs to look around in memory to retrieve all the MonoBehaviour instances it is...

The burst compiler

The last component of the DOTS is the burst compiler. The burst compiler is a compiler that can compile a subset of C# into optimized native code. The main goal of Burst is to compile jobs so that they can be as fast and lightweight as possible.

The cool thing is that using the burst compiler is extremely easy. First, you need to install the Burst package from Window | Package Manager. Then, the only thing you need to change is to add the [BurstCompile] decorator on top of the job definitions as follows:

[BurstCompile]
public struct RotatorJob : IJobForEach<Rotation, RotationSpeed>
        {

            [ReadOnly]
            public float deltaTime;

            public void Execute(ref Rotation rotation, [ReadOnly] ref RotationSpeed rotationSpeed)
            {
                rotation.Value = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle...

Summary

The DOTS is the peak of Unity's effort to push Unity into the future of gaming. I firmly believe that in the future, DOTS will be a core component of any optimization effort, and this chapter will definitely grow into several ones while DOTS becomes more stable and is supported by the community.

Unfortunately, at this stage, C# jobs and ECS are still very unstable, their APIs are changing rapidly and, therefore, I do not advise using them in big, important, commercial games. However, I believe it is important to start experimenting with them so as to be ready for when their time comes.

This chapter merely scratches the surface of the DOTS. There are many more details, configurations, and optimizations that can be implemented both in jobs and ECS. For more information, the main Unity Hub for DOTS (https://unity.com/dots) is your best friend.

This chapter effectively...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Game Optimization - Third Edition
Published in: Nov 2019Publisher: PacktISBN-13: 9781838556518
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 $15.99/month. Cancel anytime

Authors (2)

author image
Dr. Davide Aversa

Dr. Davide Aversa holds a PhD in Artificial Intelligence (AI) and an MSc in AI and robotics from the University of Rome La Sapienza in Italy. He has a strong interest in AI for the development of interactive virtual agents and procedural content generation. He has served as a program committee member for video game-related conferences such as the IEEE conference on computational intelligence and games, and he also regularly participates in game-jam contests. He also writes a blog on game design and game development.
Read more about Dr. Davide Aversa

author image
Chris Dickinson

Chris Dickinson grew up in a quiet little corner of England with a strong passion for mathematics, science and, in particular, video games. He loved playing them, dissecting their gameplay, and trying to figure out how they worked. Watching his dad hack the hex code of a PC game to get around the early days of copy protection completely blew his mind! His passion for science won the battle at the time; however, after completing a master's degree in physics with electronics, he flew out to California to work in the field of scientific research in the heart of Silicon Valley. Shortly afterward, he had to admit to himself that research work was an unsuitable career path for his temperament. After firing resumes in all directions, he landed a job that finally set him on the correct course in the field of software engineering (this is not uncommon for physics grads, I hear). His time working as an automated tools developer for IPBX phone systems fit his temperament much better. Now he was figuring out complex chains of devices, helping its developers fix and improve them, and building tools of his own. Chris learned a lot about how to work with big, complex, real-time, event-based, user-input driven state machines (sounds familiar?). Being mostly self-taught at this point, Chris's passion for video games was flaring up again, pushing him to really figure out how video games were built. Once he felt confident enough, he returned to school for a bachelor's degree in game and simulation programming. By the time he was done, he was already hacking together his own (albeit rudimentary) game engines in C++ and regularly making use of those skills during his day job. However, if you want to build games, you should just build games, and not game engines. So, Chris picked his favorite publically available game engine at the time--an excellent little tool called Unity 3D--and started hammering out some games. After a brief stint of indie game development, Chris regretfully decided that the demands of that particular career path weren't for him, but the amount of knowledge he had accumulated in just a few short years was impressive by most standards, and he loved to make use of it in ways that enabled other developers with their creations. Since then, Chris has authored a tutorial book on game physics (Learning Game Physics with Bullet Physics and OpenGL, Packt Publishing) and two editions of a Unity performance optimization book (which you are currently reading). He has married the love of his life, Jamie, and works with some of the coolest modern technology as a software development engineer in Test (SDET) at Jaunt Inc. in San Mateo, CA, a Virtual Reality/Augmented Reality startup that focuses on delivering VR and AR experiences, such as 360 videos (and more!). Outside of work, Chris continues to fight an addiction to board games (particularly Battlestar: Galactica and Blood Rage), an obsession with Blizzard's Overwatch and Starcraft II, cater to the ever-growing list of demands from a pair of grumpy yet adorable cats, and gazing forlornly at the latest versions of Unity with a bunch of game ideas floating around on paper. Someday soon, when the time is right (and when he stops slacking off), his plans may come to fruition
Read more about Chris Dickinson