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

Masterful Memory Management

Memory efficiency is an important element of performance optimization. It's possible for games of limited scope, such as hobby projects and prototypes, to get away with ignoring memory management. These games will tend to waste a lot of resources and potentially leak memory, but this won't be a problem if we limit its exposure to friends and coworkers. However, anything we want to release professionally needs to take this subject seriously. Unnecessary memory allocations lead to poor user experience due to excessive garbage collection (costing precious CPU time) and memory leaks, which will lead to crashes. None of these situations are acceptable in modern game releases.

Using memory efficiently with Unity requires a solid understanding of the underlying Unity engine, the Mono platform, and the C# language. Also, if we're making use...

The Mono platform

Mono is a magical sauce mixed into the Unity recipe, which gives it a lot of its cross-platform capability. Mono is an open source project that built its own platform of libraries based on the API, specifications, and tools from Microsoft's .NET Framework. Essentially, it is an open source recreation of the .NET library, was accomplished with little-to-no access to the original source code, and is fully compatible with the original library from Microsoft.

The goal of the Mono project is to provide cross-platform development through a framework that allows code written in a common programming language to run against many different hardware platforms, including Linux, macOS, Windows, ARM, PowerPC, and more. Mono even supports many different programming languages. Any language that can be compiled into .NET's Common Intermediate Language (CIL) is sufficient...

Code compilation

When we make changes to our C# code, it is automatically compiled when we switch back from our favorite IDE (which is typically either MonoDevelop or the much more feature-rich Visual Studio) to the Unity Editor. However, the C# code is not converted directly into machine code, as we would expect static compilers to do if we are using languages such as C++.

Instead, the code is converted into an intermediate stage called Common Intermediate Language (CIL), which is an abstraction above the native code. This is how .NET can support multiple languages—each uses a different compiler, but they're all converted into CIL, so the output is effectively the same regardless of the language that we pick. CIL is similar to Java bytecode, upon which it is based, and the CIL code is entirely useless on its own, as CPUs have no idea how to run the instructions defined...

Profiling memory

There are two issues we are concerned about when it comes to memory management: how much we're consuming and how often we're allocating new blocks. Let's cover each of these topics separately.

Profiling memory consumption

We do not have direct control over what is going on in the native domain since we don't have the Unity engine source code and hence can't add any code that will interact with it directly. We can, however, control it indirectly using various script-level functions that serve as interaction points between managed and native code. There are technically a variety of memory allocators available, which are used internally for things such as GameObjects, graphics objects...

Memory management performance enhancements

In most game engines, we would have the luxury of being able to port inefficient managed code into faster native code if we were hitting performance issues. This is not an option unless we invest serious cash in obtaining the Unity source code, which is offered as a license separate from the Free/Personal/Pro licensing system, and on a per case, per-title basis. We could also purchase a license of Unity Pro with the hope of using native plugins, but doing so rarely leads to a performance benefit since we must still cross the native-managed bridge to invoke function calls inside of it. Native plugins are normally used to interface with systems and libraries that are not built specifically for C#. This forces the overwhelming majority of us into a position of needing to make our C# script-level code as performant as possible ourselves.

...

Summary

We've covered a humongous amount of theory and language concepts in this chapter, which have hopefully shed some light on how the internals of the Unity engine and C# language work. These tools try their best to spare us from the burden of complex memory management, but there is still a whole host of concerns we need to keep in mind as we develop our game. Between the compilation processes, multiple memory domains, the complexities of value types versus reference types, passing by value versus passing by reference, boxing, object pooling, and various quirks within the Unity API, you have a lot of things to worry about. However, with enough practice, you will learn to overcome them without needing to keep referring to giant tomes such as this!

With this chapter, we have covered all of the possible optimization areas in classic Unity. However, with the 2019.1 release...

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}