Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity Game Optimization - Third Edition

You're reading from  Unity Game Optimization - Third Edition

Product type Book
Published in Nov 2019
Publisher Packt
ISBN-13 9781838556518
Pages 404 pages
Edition 3rd Edition
Languages
Authors (2):
Dr. Davide Aversa Dr. Davide Aversa
Profile icon Dr. Davide Aversa
Chris Dickinson Chris Dickinson
Profile icon Chris Dickinson
View More author details

Table of Contents (15) Chapters

Preface 1. Section 1: Base Scripting Optimization
2. Evaluating Performance Problems 3. Scripting Strategies 4. Section 2: Graphical Optimizations
5. The Benefits of Batching 6. Optimizing Your Art Assets 7. Faster Physics 8. Dynamic Graphics 9. Section 3: Advance Optimizations
10. Optimizations for Virtual and Augmented Reality 11. Masterful Memory Management 12. The Data-Oriented Technology Stack 13. Tactical Tips and Tricks 14. Other Books You May Enjoy

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 2019 Publisher: Packt ISBN-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.
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}