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

Using distance-squared over distance

It is safe to say that CPUs are relatively good at multiplying floating-point numbers together, but relatively dreadful at calculating square roots from them. Every time we ask Vector3 to calculate a distance with the magnitude property or with the Distance() method, we're asking it to perform a square root calculation (as per Pythagorean theorem), which can cost a lot of CPU overhead compared to many other types of vector math calculations.

However, the Vector3 class also offers a sqrMagnitude property, which provides the same result as distance, only the value is squared. This means that if we also square the value we wish to compare distance against, then we can perform essentially the same comparison without the cost of an expensive square-root calculation.

For example, consider the following code:

float distance = (transform.position...