You are previewing Unity 5 Game Optimization.
O'Reilly logo
Unity 5 Game Optimization

Book Description

Master performance optimization for Unity3D applications with tips and techniques that cover every aspect of the Unity3D Engine

About This Book

  • Optimize CPU cycles, memory usage, and GPU throughput for any Unity3D application

  • Master optimization techniques across all Unity Engine features including Scripting, Asset Management, Physics, Graphics Features, and Shaders

  • A practical guide to exploring Unity Engine's many performance-enhancing methods

  • 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 our 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 through 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 our scenes as dynamic as possible by making the most of the Physics engine

  • Organize, filter, and compress our art assets to maximize performance while maintaining high quality

  • Pull back the veil on the Mono Framework and the C# Language 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

    Competition within the gaming industry has become significantly fiercer in recent years with the adoption of game development frameworks such as Unity3D. 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 has led to an enormous explosion of talent, which has made it critical to ensure our games stand out from the crowd through a high level of quality. A good user experience is essential to create a solid product that our users will enjoy for many years to come.

    Nothing turns gamers away from a game faster than a poor user-experience. Input latency, slow rendering, broken physics, stutters, freezes, and crashes are among a gamer’s worst nightmares and it’s up to us as game developers to ensure this never happens. High performance does not need to be limited to games with the biggest teams and budgets.

    Initially, you will explore the major features of the Unity3D Engine from top to bottom, investigating a multitude of ways we can improve application performance starting with the detection and analysis of bottlenecks. You’ll then gain an understanding of possible solutions and how to implement them. You will then learn everything you need to know about where performance bottlenecks can be found, why they happen, and how to work around them.

    This book gathers a massive wealth of knowledge together in one place, saving many hours of research and can be used as a quick reference to solve specific issues that arise during product development.

    Style and approach

    This book is organized based on the major features of Unity engine and should be treated as a reference guide. It is written as a series of investigations into both common and unusual performance pitfalls, each including a study on why the bottleneck is causing us problems, and a list of enhancements or features that can be used to work around them. Differences in effectiveness, behaviors, or feature-sets between Unity 4.x and Unity 5.x will be highlighted.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Unity 5 Game Optimization
      1. Table of Contents
      2. Unity 5 Game Optimization
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. 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. Errata
          3. Piracy
          4. Questions
      9. 1. Detecting Performance Issues
        1. The Unity Profiler
          1. Launching the Profiler
            1. Editor or standalone instances
            2. Editor profiling
            3. The Unity Webplayer connection
            4. Remote connection to an iOS device
            5. Remote connection to an Android device
          2. The Profiler window
            1. Controls
              1. CPU Area
              2. The GPU Area
              3. The Rendering Area
              4. The Memory Area
              5. The Audio Area
              6. The Physics 3D/2D Area
        2. Best approaches to performance analysis
          1. Verifying script presence
          2. Verifying script count
          3. Minimizing ongoing code changes
          4. Minimizing internal distractions
          5. Minimizing external distractions
        3. Targeted profiling of code segments
          1. Profiler script control
          2. Custom CPU Profiling
        4. Saving and loading Profiler data
          1. Saving Profiler data
          2. Loading Profiler data
        5. Final thoughts on Profiling and Analysis
          1. Understanding the Profiler
          2. Reducing noise
          3. Focusing on the issue
        6. Summary
      10. 2. Scripting Strategies
        1. Cache Component references
        2. Obtaining Components using the fastest method
        3. Removing empty callback declarations
        4. Avoiding the Find() and SendMessage() methods at runtime
          1. Static classes
          2. Singleton Components
          3. Assigning references to pre-existing objects
          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 a custom message
            7. Message registration
            8. Message sending
            9. Message cleanup
            10. Wrapping up the messaging system
        5. Disabling unused scripts and objects
          1. Disabling objects by visibility
          2. Disabling objects by distance
        6. Consider using distance-squared over distance
        7. Avoid retrieving string properties from GameObjects
        8. Update, Coroutines, and InvokeRepeating
        9. Consider caching Transform changes
        10. Faster GameObject null reference checks
        11. Summary
      11. 3. The Benefits of Batching
        1. Draw Calls
        2. Materials and Shaders
        3. Dynamic Batching
          1. Vertex attributes
          2. Uniform scaling
          3. Dynamic Batching summary
        4. Static Batching
          1. The Static flag
          2. Memory requirements
          3. Material references
          4. Static Batching caveats
            1. Edit Mode debugging of Static Batching
            2. Avoiding instantiating static meshes at runtime
            3. Visibility and rendering
          5. Static Batching summary
        5. Summary
      12. 4. Kickstart Your Art
        1. Audio
          1. Loading audio files
            1. Profiling audio
            2. Additional loading options
          2. Encoding formats and quality levels
          3. Audio performance enhancements
            1. Minimize active Audio Source count
            2. Minimize Audio Clip references
            3. Enable Force to Mono for 3D sounds
            4. Resample to lower frequencies
            5. Consider all encoding formats
            6. Beware of streaming
            7. Apply Filter effects through Mixer groups to reduce duplication
            8. Use "WWW.audioClip" responsibly
            9. Consider Audio Module files for background music
        2. Texture files
          1. 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
        3. Mesh and animation files
          1. Reducing polygon count
            1. Tweaking Mesh Compression
            2. Use Read-Write Enabled appropriately
          2. Import/calculate only what's needed
          3. Consider baked animations
          4. Let Unity optimize meshes
          5. Combine meshes
        4. Summary
      13. 5. Faster Physics
        1. Physics Engine internals
          1. Physics and time
            1. The Fixed Update loop
            2. Maximum Allowed Timestep
            3. Physics updates and runtime changes
          2. Static and Dynamic Colliders
          3. Collision detection
          4. Collider types
          5. The Collision Matrix
          6. Rigidbody active and sleeping states
          7. Ray and object casting
        2. Physics performance optimizations
          1. Scene setup
            1. Scaling
            2. Positioning
            3. Mass
          2. Use Static Colliders appropriately
          3. Optimize the Collision Matrix
          4. Prefer discrete collision detection
          5. Modify the FixedUpdate frequency
          6. Adjust the Maximum Allowed Timestep
          7. Minimize cast and bounding-volume checks
          8. Avoid complex Mesh Colliders
            1. Use simpler primitives
            2. Use simpler Mesh Colliders
          9. Avoid complex physics components
          10. Let physics objects sleep
          11. Modify Solver Iteration Count
          12. Optimizing ragdolls
            1. Reduce Joints and Colliders
            2. Avoid inter-ragdoll collisions
            3. Disable or remove inactive ragdolls
          13. Know when to use physics
          14. Consider upgrading to Unity 5
        3. Summary
      14. 6. Dynamic Graphics
        1. Profiling rendering issues
          1. GPU profiling
          2. The Frame Debugger
          3. Brute force testing
          4. CPU-bound
            1. Multithreaded rendering
            2. GPU Skinning
        2. Front end bottlenecks
          1. Level Of Detail
          2. Disable GPU Skinning
          3. Reduce tessellation
        3. Back end bottlenecks
          1. Fill rate
            1. Overdraw
            2. Occlusion Culling
            3. Shader optimization
            4. Consider using Shaders intended for mobile platforms
              1. Use small data types
              2. Avoid changing precision while swizzling
              3. Use GPU-optimized helper functions
              4. Disable unnecessary features
              5. Remove unnecessary input data
              6. Only expose necessary variables
              7. Reduce mathematical complexity
              8. Reduce texture lookups
              9. Avoid conditional statements
              10. Reduce data dependencies
              11. Surface Shaders
              12. Use Shader-based LOD
          2. Memory bandwidth
            1. Use less texture data
            2. Test different GPU Texture Compression formats
            3. Minimize texture sampling
            4. Organize assets to reduce texture swaps
          3. VRAM limits
            1. Texture preloading
            2. Texture thrashing
        4. Lighting and Shadowing
          1. Forward Rendering
          2. Deferred Shading
          3. Vertex Lit Shading (legacy)
          4. Real-time Shadows
          5. Lighting optimization
            1. Use the appropriate Shading Mode
            2. Use Culling Masks
            3. Use Baked Lightmaps
            4. Optimize Shadows
        5. Optimizing graphics for mobile
          1. Minimize Draw Calls
          2. Minimize the Material count
          3. Minimize texture size and Material count
          4. Make textures square and power of 2
          5. Use the lowest possible precision formats in Shaders
          6. Avoid Alpha Testing
        6. Summary
      15. 7. Masterful Memory Management
        1. The Mono platform
          1. The compilation process
            1. Manual JIT compilation
        2. Memory usage optimization
          1. Unity memory domains
            1. Native memory
            2. Managed memory
              1. Garbage collection
                1. Memory fragmentation
                2. Garbage collection at runtime
                3. Threaded garbage collection
                4. Garbage collection tactics
          2. Value types and Reference types
            1. Pass by value and pass by reference
            2. Structs are Value types
            3. Arrays are Reference types
            4. Strings are immutable Reference types
              1. String concatenation
            5. Boxing
          3. The importance of data layout
          4. The Unity API
          5. The foreach loops
          6. Coroutines
          7. Closures
          8. .NET library functions
          9. Temporary work buffers
          10. Object pooling
        3. 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
        4. The future of Mono and Unity
        5. Summary
      16. 8. Tactical Tips and Tricks
        1. Editor hotkey tips
          1. GameObjects
          2. Scene View
          3. Arrays
          4. Interface
          5. Other
        2. Editor interface tips
          1. General
          2. The Inspector View
          3. The Project View
          4. The Hierarchy View
          5. The Scene and Game Views
          6. Play Mode
        3. Scripting tips
          1. General
          2. Attributes
            1. Variable attributes
            2. Class attributes
          3. Logging
          4. Useful links
        4. Custom editors/menus tips
        5. External tips
          1. Other tips
        6. Summary
      17. Index