Book Image

Unity Game Optimization - Third Edition

By : Dr. Davide Aversa, Chris Dickinson
Book Image

Unity Game Optimization - Third Edition

By: Dr. Davide Aversa, Chris Dickinson

Overview of this book

Unity engine comes with a great set of features to help you build high-performance games. This Unity book is your guide to optimizing various aspects of your game development, from game characters and scripts, right through to animations. You’ll explore techniques for writing better game scripts and learn how to optimize a game using Unity technologies such as ECS and the Burst compiler. The book will also help you manage third-party tooling used with the Unity ecosystem. You’ll also focus on the problems in the performance of large games and virtual reality (VR) projects in Unity, gaining insights into detecting performance issues and performing root cause analysis. As you progress, you’ll discover best practices for your Unity C# script code and get to grips with usage patterns. Later, you’ll be able to optimize audio resources and texture files, along with effectively storing and using resource files. You’ll then delve into the Rendering Pipeline and learn how to identify performance problems in the pipeline. In addition to this, you’ll learn how to optimize the memory and processing unit of Unity. Finally, you’ll cover tips and tricks used by Unity professionals to improve the project workflow. By the end of this book, you’ll have developed the skills you need to build interactive games using Unity and its components.
Table of Contents (15 chapters)
Free Chapter
1
Section 1: Base Scripting Optimization
4
Section 2: Graphical Optimizations
9
Section 3: Advance Optimizations

Preface

User experience is a critical component of any game. This not only includes our game's story and its gameplay but also how smoothly the graphics run, how reliably the game connects to multiplayer servers, how responsive it is to user input, and even how large the final application file size is due to the prevalence of mobile devices and cloud downloads. The barrier of entry into game development has been lowered considerably thanks to tools such as Unity, which offer an enormous array of useful development features while still being accessible to individual developers. However, due to the amount of competition in the gaming industry, the level of quality of the final product that our players expect us to provide is increasing with every passing day. We should expect that players and critics can and will scrutinize every facet of our game.

The goals of performance optimization are deeply entwined with user experience. Poorly optimized games can result in low frame rates, freezes, crashes, input lag, long loading times, inconsistent and jittery runtime behavior, physics engine breakdowns, and even excessively high battery power consumption (an often-neglected metric for mobile devices). Having just one of these issues can be a game developer's worst nightmare as reviews will tend to focus on the one thing that we did poorly, ignoring all the things that we did well.

One goal of performance optimization is to make the best use of the available resources, including CPU resources such as the number of cycles consumed, how much main memory space we're using (known as RAM), as well as Graphics Processing Unit (GPU) resources, which includes its own memory space (known as VRAM), Fill Rate, Memory Bandwidth, and so on. However, the most important goal of performance optimization is to ensure that no single resource causes a bottleneck at an inappropriate time and that the highest priority tasks get taken care of first. Even small, intermittent hiccups and sluggishness in performance can pull the player out of the experience, breaking the game immersion and limiting our potential to create the experience we intended. Another consideration is that the more resources we can save, the more activity we can afford to implement in our games, allowing us to generate more exciting and dynamic gameplay.

It is also vital to decide when to take a step back and stop making performance enhancements. In a world with infinite time and resources, there will always be another way to make it better, faster, and more efficient. There must be a point during development where we decide that the product has reached an acceptable level of quality. If not, we risk dooming ourselves to repeatedly implementing changes that result in little or no tangible benefit, while each change also risks the chance that we introduce more bugs.

The best way to decide whether a performance issue is worth fixing is to answer the question, will the user notice it?. If the answer to this question is no, then performance optimization will be a wasted effort. There is an old saying in software development:

Premature optimization is the root of all evil.

Premature optimization is the cardinal sin of reworking and refactoring code to enhance performance without any proof that it is necessary. This can mean either making changes without showing that a performance problem even exists, or making changes because we only believe a performance issue might stem from a particular area before it has been proven to be true.

Of course, the original version of this common saying by Donald Knuth goes on to say that we should still write our code to avoid the more straightforward and obvious performance problems. However, the real performance optimization work toward the end of a project can take a lot of time, and we should plan the time to polish the product properly while avoiding the desire to implement more costly and time-consuming changes without any valid proof. These kinds of mistakes have cost software developers, as a collective whole, a depressing number of work hours for nothing.

This book intends to give you the tools, knowledge, and skills you need to both detect and fix performance issues in a Unity application, no matter where they stem from. These bottlenecks can appear within hardware components such as the CPU, GPU, and RAM, or within software subsystems such as physics, rendering, and the Unity engine itself.

Optimizing the performance of our games will give them a much better chance of succeeding and standing out from the crowd in a marketplace that is inundated with new, high-quality games every single day.