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

Tactical Tips and Tricks

Software engineers are an optimistic bunch and, as such, we often underestimate the amount of work it takes to fully implement new features or make changes to an existing code base. A common mistake is to only consider how long it will take to write the code that's required to create that feature. In such cases, we forget to include the time it takes for several important tasks. We often need to spend time refactoring other subsystems to support the changes we're making. This can happen either because we didn't think it would be necessary at the time, or because we thought of a better way to implement it halfway through, which can quickly turn into a rabbit hole dive of redesign and refactoring if we don't plan far ahead. We should also consider the time that's needed for testing and documentation. Even if a QA team does a testing...

Editor hotkey tips

The Editor is rife with hotkeys that can aid rapid development, and it's worth checking out the documentation. However, let's be honest—nobody reads the manual until they need something specific from it. In this section, we will go over some of the most useful, yet less well-known, hotkeys that are available when we're playing with the Unity Editor.

For each case we'll be looking at, the Windows hotkey is listed. If the macOS hotkey requires a different set of keystrokes, then it will be shown in parentheses.

Working with GameObjects

GameObjects can be duplicated by selecting them in the Hierarchy window and pressing Ctrl + D (cmd + D). New, empty GameObjects can be created by...

Editor UI tips

The Editor's default behavior is designed to be efficient and satisfy every user; however, each of us is different, just like beautiful snowflakes, and so are our working preferences. Fortunately, Unity allows us to customize many aspects of the Editor's workflow. Let's see how with the following collection of tips.

Script Execution Order

We can prioritize which scripts will have their Update() and FixedUpdate() callbacks called before others by navigating to Edit | Project Settings | Script Execution Order. If we find ourselves trying to solve complex problems using this feature (with the exception of time-sensitive systems, such as audio processing), it implies that we've got some very...

Scripting tips

If you are a developer, you will spend a lot of time editing code. While artists and designers are playing with colorful images and visual effects, you may find yourself stuck in the black and white code editor domain. Coding may be hard sometimes, but it doesn't need to be tedious. In the following tips, we will learn how to simplify some of the most boring parts of the job.

General

We can modify various templates of the new script, as well as shader and compute shader files. This can be helpful if we want to remove the empty Update stubs which, as we covered in Chapter 2, Scripting Strategies, can cause unnecessary runtime overhead. These files can be found in the following locations:

  • Windows: <Unity...

Custom Editor scripts and menu tips

While it's common knowledge that we can create an Editor menu item in an Editor script with the [MenuItem] attribute, a less well-known ability is being able to set custom hotkeys for menu items. For example, we can make the K key trigger our menu item method by defining that the [MenuItem] attribute ends with _k, as follows:

[MenuItem("My Menu/Menu Item _k")]

We can also include modifier keys such as Ctrl (cmd), Shift, and Alt using the %, #, and & characters, respectively.

[MenuItem] also has two overloads, which allow us to set two additional parameters: a Boolean that determines whether the menu item requires a validation method and an integer that determines the menu item's priority in the Hierarchy window.

Check out the documentation for [MenuItems] for a complete list of available hotkey modifiers, special keys...

External tips

The following tips and tricks are related to topics outside the Unity Editor itself but can help Unity development workflow enormously.

The Twitter hashtag #unitytips is a great resource for useful tips and tricks for Unity development and is, in fact, where many of the tips in this chapter originate from. However, hashtags are difficult to filter for tips you haven't seen before, and it tends to be abused for marketing. A great resource that pulls together a bundle of weekly tips from #unitytips can be found at http://devdog.io/blog.

Googling Unity-related problems or concerns can be made a lot faster if we start the search with site:unity3d.com, which will filter all the results so that only those under the unity3d.com domain will appear.

If the Unity Editor crashes, for whatever reason, then we can potentially restore our scene by renaming the following file...

Other tips

Finally, this section contains tips that didn't quite fit into the other categories.

It's always a good idea to organize our scenes using empty GameObjects and use them as parents for a group of objects while naming them something sensible for that group. The only drawback to this method is that the empty object's transform is included during position or rotation changes and is included during recalculations. As we know, reparenting GameObject to another transform has its own costs. Proper object referencing, transform change caching, and/or use of localPosition/localRotation can be used to solve some of these problems adequately. In almost all cases, the benefits of having a workflow from scene organization are significantly more valuable than such trivial performance losses.

Animator Override Controllers were introduced way back in Unity v4.3 but tend...

Summary

This brings us to the end of this book. Hopefully, you have enjoyed the ride. To reiterate, perhaps the most important tip in this book is to always make sure that you verify the source of the performance bottleneck via benchmarking before making a single change. The last thing we want to waste time on is chasing ghosts in the code base when 5 minutes of Profiler testing can save us an entire day of work. In a lot of cases, the solution requires a cost-benefit analysis to determine whether we're not sacrificing too much in any other area at the risk of adding further bottlenecks. Make sure that you have a reasonable understanding of the root cause of the bottleneck to avoid putting other performance metrics at risk. To also reiterate the second most important tip in this book, always profile and test after making changes to ensure they had the intended effect.

Performance...

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