Unity 2017 Game Optimization - Second Edition

Book description

Master a series of performance-enhancing coding techniques and methods that help them improve the performance of their Unity3D applications

About This Book

  • Discover features and techniques to optimize Unity Engine's CPU cycles, memory usage, and the GPU throughput of any application
  • Explore multiple techniques to solve performance issues with your VR projects
  • Learn the best practices for project organization to save time through an improved workflow

Who This Book Is For

This book is intended for intermediate and advanced Unity developers who have experience with most of Unity's feature-set, and who want to maximize the performance of their game. Familiarity with the C# language will be needed.

What You Will Learn

  • Use the Unity Profiler to find bottlenecks anywhere in your application, and discover how to resolve them
  • Implement best practices for C# scripting to avoid common pitfalls
  • Develop a solid understanding of the rendering pipeline, and maximize its performance by reducing draw calls and avoiding fill rate bottlenecks
  • Enhance shaders in a way that is accessible to most developers, optimizing them through subtle yet effective performance tweaks
  • Keep your scenes as dynamic as possible by making the most of the Physics engine
  • Organize, filter, and compress your art assets to maximize performance while maintaining high quality
  • Discover different kinds of performance problems that are critical for VR projects and how to tackle them
  • Use the Mono Framework and C# to implement low-level enhancements that maximize memory usage and avoid garbage collection
  • Get to know the best practices for project organization to save time through an improved workflow

In Detail

Unity is an awesome game development engine. Through its massive feature-set and ease-of-use, Unity helps put some of the best processing and rendering technology in the hands of hobbyists and professionals alike.

This book shows you how to make your games fly with the recent version of Unity 2017, and demonstrates that high performance does not need to be limited to games with the biggest teams and budgets.

Since nothing turns gamers away from a game faster than a poor user-experience, the book starts by explaining how to use the Unity Profiler to detect problems. You will learn how to use stopwatches, timers and logging methods to diagnose the problem. You will then explore techniques to improve performance through better programming practices.

Moving on, you will then learn about Unity's built-in batching processes; when they can be used to improve performance, and their limitations. Next, you will import your art assets using minimal space, CPU and memory at runtime, and discover some underused features and approaches for managing asset data. You will also improve graphics, particle system and shader performance with a series of tips and tricks to make the most of GPU parallel processing.

You will then delve into the fundamental layers of the Unity3D engine to discuss some issues that may be difficult to understand without a strong knowledge of its inner-workings. The book also introduces you to the critical performance problems for VR projects and how to tackle them.

By the end of the book, you will have learned to improve the development workflow by properly organizing assets and ways to instantiate assets as quickly and waste-free as possible via object pooling.

Style and approach

This practical book will help readers understand the essentials of the Unity3D engine and how to build games while improving the performance of their applications.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Pursuing Performance Problems
    1. The Unity Profiler
      1. Launching the Profiler
        1. Editor or standalone instances
        2. Connecting to a WebGL instance
        3. Remote connection to an iOS device
        4. Remote connection to an Android device
        5. Editor profiling
      2. The Profiler window
        1. Profiler controls
          1. Add Profiler
          2. Record
          3. Deep Profile
          4. Profile Editor
          5. Connected Player
          6. Clear
          7. Load
          8. Save
          9. Frame Selection
        2. Timeline View
        3. Breakdown View Controls
        4. Breakdown View
          1. The CPU Usage Area
          2. The GPU Usage Area
          3. The Rendering Area
          4. The Memory Area
          5. The Audio Area
          6. The Physics 3D and Physics 2D Areas
          7. The Network Messages and Network Operations Areas
          8. The Video Area
          9. The UI and UI Details Areas
          10. The Global Illumination Area
    2. Best approaches to performance analysis
      1. Verifying script presence
      2. Verifying script count
      3. Verifying the order of events
      4. Minimizing ongoing code changes
      5. Minimizing internal distractions
      6. Minimizing external distractions
      7. Targeted profiling of code segments
        1. Profiler script control
        2. Custom CPU Profiling
    3. Final thoughts on Profiling and Analysis
      1. Understanding the Profiler
      2. Reducing noise
      3. Focusing on the issue
    4. Summary
  3. Scripting Strategies
    1. Obtain Components using the fastest method
    2. Remove empty callback definitions
    3. Cache Component references
    4. Share calculation output
    5. Update, Coroutines, and InvokeRepeating
    6. Faster GameObject null reference checks
    7. Avoid retrieving string properties from GameObjects
    8. Use appropriate data structures
    9. Avoid re-parenting Transforms at runtime
    10. Consider caching Transform changes
    11. Avoid Find() and SendMessage() at runtime
      1. Assigning references to pre-existing objects
      2. Static Classes
      3. Singleton Components
      4. A global Messaging System
        1. A globally accessible object
        2. Registration
        3. Message processing
        4. Implementing the Messaging System
        5. Message queuing and processing
        6. Implementing custom messages
        7. Message sending
        8. Message registration
        9. Message cleanup
        10. Wrapping up the Messaging System
    12. Disable unused scripts and objects
      1. Disabling objects by visibility
      2. Disabling objects by distance
    13. Consider using distance-squared over distance
    14. Minimize Deserialization behavior
      1. Reduce serialized object size
      2. Load serialized objects asynchronously
      3. Keep previously loaded serialized objects in memory
      4. Move common data into ScriptableObjects
    15. Load scenes additively and asynchronously
    16. Create a custom Update() layer
    17. Summary
  4. The Benefits of Batching
    1. Draw Calls
    2. Materials and Shaders
    3. The Frame Debugger
    4. Dynamic Batching
      1. Vertex attributes
      2. Mesh scaling
      3. Dynamic Batching summary
    5. Static Batching
      1. The Static flag
      2. Memory requirements
      3. Material references
      4. Static Batching caveats
        1. Edit Mode debugging of Static Batching
        2. Instantiating static meshes at runtime
      5. Static Batching summary
    6. Summary
  5. Kickstart Your Art
    1. Audio
      1. Importing audio files
      2. Loading audio files
      3. Encoding formats and quality levels
      4. Audio performance enhancements
        1. Minimize active Audio Source count
        2. Enable Force to Mono for 3D sounds
        3. Resample to lower frequencies
        4. Consider all compression formats
        5. Beware of streaming
        6. Apply Filter Effects through Mixer Groups to reduce duplication
        7. Use remote content streaming responsibly
        8. Consider Audio Module files for background music
    2. Texture files
      1. Texture compression formats
      2. Texture performance enhancements
        1. Reduce texture file size
        2. Use Mip Maps wisely
        3. Manage resolution downscaling externally
        4. Adjust Anisotropic Filtering levels
        5. Consider Atlasing
        6. Adjust compression rates for non-square textures
        7. Sparse Textures
        8. Procedural Materials
        9. Asynchronous Texture Uploading
    3. Mesh and animation files
      1. Reduce polygon count
      2. Tweak Mesh Compression
      3. Use Read-Write Enabled appropriately
      4. Consider baked animations
      5. Combine meshes
    4. Asset Bundles and Resources
    5. Summary
  6. Faster Physics
    1. Physics Engine internals
      1. Physics and time
        1. Maximum Allowed Timestep
        2. Physics updates and runtime changes
      2. Static Colliders and Dynamic Colliders
      3. Collision detection
      4. Collider types
      5. The Collision Matrix
      6. Rigidbody active and sleeping states
      7. Ray and object casting
      8. Debugging Physics
    2. Physics performance optimizations
      1. Scene setup
        1. Scaling
        2. Positioning
        3. Mass
      2. Use Static Colliders appropriately
      3. Use Trigger Volumes responsibly
      4. Optimize the Collision Matrix
      5. Prefer Discrete collision detection
      6. Modify the Fixed Update frequency
      7. Adjust the Maximum Allowed Timestep
      8. Minimize Raycasting and bounding-volume checks
      9. Avoid complex Mesh Colliders
        1. Use simpler primitives
        2. Use simpler Mesh Colliders
      10. Avoid complex physics Components
      11. Let physics objects sleep
      12. Modify the Solver Iteration Count
      13. Optimize Ragdolls
        1. Reduce Joints and Colliders
        2. Avoid inter-Ragdoll collisions
        3. Replace, deactivate or remove inactive Ragdolls
      14. Know when to use physics
    3. Summary
  7. Dynamic Graphics
    1. The Rendering Pipeline
      1. The GPU Front End
      2. The GPU Back End
        1. Fill Rate
          1. Overdraw
        2. Memory Bandwidth
      3. Lighting and Shadowing
        1. Forward Rendering
        2. Deferred Rendering
        3. Vertex Lit Shading (legacy)
        4. Global Illumination
      4. Multithreaded Rendering
      5. Low-level rendering APIs
    2. Detecting performance issues
      1. Profiling rendering issues
      2. Brute-force testing
    3. Rendering performance enhancements
      1. Enable/Disable GPU Skinning
      2. Reduce geometric complexity
      3. Reduce Tessellation
      4. Employ GPU Instancing
      5. Use mesh-based Level Of Detail (LOD)
        1. Culling Groups
      6. Make use of Occlusion Culling
      7. Optimizing Particle Systems
        1. Make use of Particle System Culling
        2. Avoid recursive Particle System calls
      8. Optimizing Unity UI
        1. Use more Canvases
        2. Separate objects between static and dynamic canvases
        3. Disable Raycast Target for noninteractive elements
        4. Hide UI elements by disabling the parent Canvas Component
        5. Avoid Animator Components
        6. Explicitly define the Event Camera for World Space Canvases
        7. Don't use alpha to hide UI elements
        8. Optimizing ScrollRects
          1. Make sure to use a RectMask2D
          2. Disable Pixel Perfect for ScrollRects
          3. Manually stop ScrollRect motion
        9. Use empty UIText elements for full-screen interaction
        10. Check the Unity UI source code
        11. Check the documentation
      9. Shader optimization
        1. Consider using Shaders intended for mobile platforms
        2. Use small data types
        3. Avoid changing precision while swizzling
        4. Use GPU-optimized helper functions
        5. Disable unnecessary features
        6. Remove unnecessary input data
        7. Expose only necessary variables
        8. Reduce mathematical complexity
        9. Reduce texture sampling
        10. Avoid conditional statements
        11. Reduce data dependencies
        12. Surface Shaders
        13. Use Shader-based LOD
      10. Use less texture data
      11. Test different GPU Texture Compression formats
      12. Minimize texture swapping
      13. VRAM limits
        1. Preload textures with hidden GameObjects
        2. Avoid texture thrashing
      14. Lighting optimization
        1. Use real-time Shadows responsibly
        2. Use Culling Masks
        3. Use baked Lightmaps
      15. Optimizing rendering performance for mobile devices
        1. Avoid Alpha Testing
        2. Minimize Draw Calls
        3. Minimize Material count
        4. Minimize texture size
        5. Make textures square and power-of-two
        6. Use the lowest possible precision formats in Shaders
    4. Summary
  8. Virtual Velocity and Augmented Acceleration
    1. XR Development
      1. Emulation
      2. User comfort
    2. Performance enhancements
      1. The kitchen sink
      2. Single-Pass versus Multi-Pass Stereo Rendering
      3. Apply anti-aliasing
      4. Prefer Forward Rendering
      5. Image effects in VR
      6. Backface culling
      7. Spatialized audio
      8. Avoid camera physics collisions
      9. Avoid Euler angles
      10. Exercise restraint
      11. Keep up to date with the latest developments
    3. Summary
  9. Masterful Memory Management
    1. The Mono platform
      1. Memory Domains
        1. Garbage collection
        2. Memory Fragmentation
        3. Garbage collection at runtime
        4. Threaded garbage collection
    2. Code compilation
      1. IL2CPP
    3. Profiling memory
      1. Profiling memory consumption
      2. Profiling memory efficiency
    4. Memory management performance enhancements
      1. Garbage collection tactics
      2. Manual JIT compilation
      3. Value types and Reference types
        1. Pass by value and by reference
        2. Structs are Value types
        3. Arrays are Reference types
        4. Strings are immutable Reference types
      4. String concatenation
        1. StringBuilder
        2. String formatting
      5. Boxing
      6. The importance of data layout
      7. Arrays from the Unity API
      8. Using InstanceIDs for dictionary keys
      9. foreach loops
      10. Coroutines
      11. Closures
      12. The .NET library functions
      13. Temporary work buffers
      14. Object Pooling
      15. Prefab Pooling
        1. Poolable Components
        2. The Prefab Pooling System
        3. Prefab pools
        4. Object spawning
        5. Instance prespawning
        6. Object despawning
        7. Prefab pool testing
        8. Prefab Pooling and Scene loading
        9. Prefab Pooling summary
      16. IL2CPP optimizations
      17. WebGL optimizations
    5. The future of Unity, Mono, and IL2CPP
      1. The upcoming C# Job System
    6. Summary
  10. Tactical Tips and Tricks
    1. Editor hotkey tips
      1. GameObjects
      2. Scene window
      3. Arrays
      4. Interface
      5. In-editor documentation
    2. Editor UI tips
      1. Script Execution Order
      2. Editor files
      3. The Inspector window
      4. The Project window
      5. The Hierarchy window
      6. The Scene and Game windows
      7. Play Mode
    3. Scripting tips
      1. General
      2. Attributes
        1. Variable attributes
        2. Class attributes
      3. Logging
      4. Useful links
    4. Custom Editor scripts and menu tips
    5. External tips
      1. Other tips
    6. Summary

Product information

  • Title: Unity 2017 Game Optimization - Second Edition
  • Author(s): Chris Dickinson
  • Release date: November 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788392365