Chapter 11. Improving Games with Extra Features and Optimization

In this chapter, we will cover the following topics:

  • Pausing the game
  • Implementing slow motion
  • Preventing your game from running on unknown servers
  • State-driven behavior Do-It-Yourself states
  • State-driven behavior using the State Design pattern
  • Reducing the number of objects by destroying objects at a death time
  • Reducing the number of enabled objects by disabling objects whenever possible
  • Reducing the number of active objects by making objects inactive whenever possible
  • Improving efficiency with delegates and events and avoiding SendMessage!
  • Executing methods regularly but independent of frame rate with coroutines
  • Spreading long computations over several frames with coroutines
  • Evaluating performance by measuring max and min frame rates (FPS)
  • Identifying performance bottlenecks with the Unity performance Profiler
  • Identifying performance bottlenecks with Do-It-Yourself performance profiling
  • Cache GameObject and component references to avoid expensive lookups
  • Improving performance with LOD groups
  • Improving performance through reduced draw calls by designing for draw call batching

Introduction

The first three recipes in this chapter provide some ideas for adding some extra features to your game (pausing, slow motion, and securing online games). The next two recipes then present ways to manage complexity in your games through managing states and their transitions.

The rest of the recipes in this chapter provide examples of how to investigate and improve the efficiency and performance of your game. Each of these optimization recipes begins by stating an optimization principle that it embodies.

The big picture

Before getting on with the recipes, let's step back and think about the different parts of Unity games and how their construction and runtime behavior can impact on game performance.

Games are made up of several different kinds of components:

  • Audio assets
  • 2D and 3D graphical assets
  • Text and other file assets
  • Scripts

When a game is running, there are many competing processing requirements for your CPU and GPU, including:

  • Audio processing
  • Script processing
  • 2D physics processing
  • 3D physics processing
  • Graphical rendering
  • GPU processing

One way to reduce the complexity of graphical computations and to improve frame rates is to use simpler models whenever possible—this is the reduction of the Level Of Detail (LOD). The general strategy is to identify situations where a simpler model will not degrade the user's experience. Typically, situations include where a model is only taking up a small part of the screen (so less detail in the model will not change what the user sees), when objects are moving very fast across the screen (so the user is unlikely to have time to notice less detail), or where we are sure the users' visual focus is elsewhere (for example, in a car racing game, the user is not looking at the quality of the trees but on the road ahead). We provide a LOD recipe, Improving performance with LOD groups, in this chapter.

Unity's draw call batching may actually be more efficient than you or your team's 3D modelers are at reducing the triangle/vertex geometry. So, it may be that by manually simplifying a 3D model, you have removed Unity's opportunity to apply its highly effective vertex reduction algorithms; then, the geometric complexity may be larger for a small model than for a larger model, and so a smaller model may lead to a lower game performance! One recipe presents advice collected from several sources and the location of tools to assist in different strategies to try to reduce draw calls and improve graphical performance.

We will present several recipes allowing you to analyze actual processing times and frame rates, so that you can collect data to confirm whether your design decisions are having the desired efficiency improvements.

 

"You have a limited CPU budget and you have to live with it"

 
 --Joachim Ante, Unite-07

At the end of the day, the best balance of heuristic strategies for your particular game project can only be discovered by an investment of time and hard work, and some form of profiling investigation. Certain strategies (such as caching to reduce component reflection lookups) should perhaps be standard practice in all projects, while other strategies may require tweaking for each unique game and level, to find which approaches work effectively to improve efficiency, frame rates, and, most importantly, the user experience when playing the game.

"Premature Optimization is the root of all evil"

Donald Knuth, "Structured Programming With Go To Statements". Computing Surveys, Vol 6, No 4, December 1974

Perhaps, the core strategy to take away from this chapter is that there are many parts of a game that are candidates for possible optimization, and you should drive the actual optimizations you finally implement for a particular game based on the evidence you gain by profiling its performance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.183