Unity 5.x Cookbook

Book description

Over 100 recipes exploring the new and exciting features of Unity 5 to spice up your Unity skillset

About This Book

  • Built on the solid foundation of the popular Unity 4.x Cookbook, the recipes in this edition have been completely updated for Unity 5
  • Features recipes for both 2D and 3D games
  • Provides you with techniques for the new features of Unity 5, including the new UI system, 2D game development, new Standard Shaders, and the new Audio Mixer

In Detail

Unity 5 is a flexible and intuitive multiplatform game engine that is becoming the industry's de facto standard. Learn to craft your own 2D and 3D computer games by working through core concepts such as animation, audio, shaders, GUI, lights, cameras, and scripting to create your own games with Unity 5.

Completely re-written to cover the new features of Unity 5, this book is a great resource for all Unity game developers, from those who have recently started using Unity right up to Unity professionals.

The first half of the book focuses on core concepts of 2D game design while the second half focuses on developing 3D game development skills. In the first half, you will discover the new GUI system, the new Audio Mixer, external files, and animating 2D characters in 2D game development. As you progress further, you will familiarize yourself with the new Standard Shaders, the Mecanim system, Cameras, and the new Lighting features to hone your skills towards building 3D games to perfection. Finally, you will learn non-player character control and explore Unity 5's extra features to enhance your 3D game development skills.

What You Will Learn

  • Immerse players with great audio, utilizing Unity 5's audio features including the new Audio Mixer, ambient sound with Reverb Zones, dynamic soundtracks with Snapshots, and balanced audio via Ducking
  • Create better materials with Unity's new, physically-based, Standard Shader
  • Measure and control time, including pausing the game, displaying clocks and countdown timers, and even implementing ?bullet time? effects
  • Improve ambiance through the use of lights and effects such as reflection and light probes
  • Create stylish user interfaces with the new UI system, including power-bars, clock displays, and an extensible inventory system
  • Save and load text and media assets from local or remote sources, publish your game via Unity Cloud, and communicate with websites and their databases to create online scoreboards
  • Discover advanced techniques, including the publisher-subscriber and state patterns, performance bottleneck identification, and methods to maximize game performance and frame rates
  • Control 2D and 3D character movement, and use NavMeshAgents to write NPC and enemy behaviors such as seek, flee, flock, and waypoint path following

Who This Book Is For

From beginners to advanced users, from artists to coders, this book is for you and everyone in your team!

Programmers can explore multimedia features, and multimedia developers can try their hand at scripting. Basic knowledge and understanding of the Unity platform, game design principles, and programming knowledge in C# is essential.

Style and approach

Each chapter first introduces the topic area and explains how the techniques covered can enhance your games. Every recipe provides step-by-step instructions, followed by an explanation of how it all works, and useful additional refinements or alternative approaches. Every required resource and C# script (fully commented) is available to download, enabling you to follow each recipe yourself.

Table of contents

  1. Unity 5.x Cookbook
    1. Table of Contents
    2. Unity 5.x Cookbook
    3. Credits
    4. Foreword
    5. About the Authors
    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. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example codes and color images
        2. Errata
        3. Piracy
        4. Questions
    9. 1. Core UI – Messages, Menus, Scores, and Timers
      1. Introduction
        1. The big picture
      2. Displaying a "Hello World" UI text message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Styling substrings with Rich Text
      3. Displaying a digital clock
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The Unity tutorial for animating an analogue clock
      4. Displaying a digital countdown timer
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Creating a message that fades away
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Displaying a perspective 3D text message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. We have to make this text crawl like it does in the movie
          2. Where to learn more
      7. Displaying an image
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Working with Sprites and UI Image components
        5. See also
      8. Creating UI Buttons to move between scenes
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Visual animation for the button mouse-over
          2. Animating button properties on mouse over
      9. Organizing images inside panels and changing panel depths via buttons
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Moving up or down by just one position, using scripted methods
      10. Displaying the value of an interactive UI Slider
        1. How to do it...
        2. How it works...
      11. Displaying a countdown timer graphically with a UI Slider
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Displaying a radar to indicate the relative locations of objects
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. The Start() method
          2. The Update() method
          3. The FindAndDisplayBlipsForTag(…) method
          4. The NormalisedPosition(…) method
          5. The CalculateBlipPosition(…) method
          6. The DrawBlip() method
      13. Creating UIs with the Fungus open-source dialog system
        1. How to do it...
        2. How it works...
      14. Setting custom mouse cursor images
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Custom cursors for mouse over UI controls
      15. Input Fields component for text entry
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Executing a C# method to respond each time the user changes the input text content
      16. Toggles and radio buttons via Toggle Groups
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Adding more Toggles and a Toggle Group to implement mutually-exclusive radio buttons
      17. Conclusion
    10. 2. Inventory GUIs
      1. Introduction
        1. The big picture
      2. Creating a simple 2D mini-game – SpaceGirl
        1. Getting ready
        2. How to do it...
        3. How it works...
      3. Displaying single object pickups with carrying and not-carrying text
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. The separation of view logic
      4. Displaying single object pickups with carrying and not-carrying icons
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Displaying multiple pickups of the same object with text totals
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Displaying multiple pickups of the same object with multiple status icons
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Revealing icons for multiple object pickups by changing the size of a tiled image
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Displaying multiple pickups of different objects as a list of text via a dynamic List<> of PickUp objects
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Order items in the inventory list alphabetically
      9. Displaying multiple pickups of different objects as text totals via a dynamic Dictionary<> of PickUp objects and "enum" pickup types
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Generalizing multiple icon displays using UI Grid Layout Groups (with scrollbars!)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Add a horizontal scrollbar to the inventory slot display
          2. The automation of PlayerInventoryDisplay getting references to all the slots
          3. Automatically changing the grid cell size based on the number of slots in inventory
          4. Add some help methods to the Rect Transform script class
      11. Conclusion
    11. 3. 2D Animation
      1. Introduction
        1. The big picture
      2. Flipping a sprite horizontally
        1. Getting ready
        2. How to do it...
        3. How it works...
      3. Animating body parts for character movement events
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Creating a 3-frame animation clip to make a platform continually animate
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Making a platform start falling once stepped-on using a Trigger to move animation from one state to another
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Creating animation clips from sprite sheet sequences
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Conclusion
    12. 4. Creating Maps and Materials
      1. Introduction
        1. Creating and saving texture maps
        2. The big picture
          1. Specular workflow
          2. The metallic workflow
          3. Other material properties
          4. Unity samples and documentation
      2. Creating a basic material with Standard Shader (Specular setup)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Setting the texture type for an image file
          2. Combining the map with color
      3. Adapting a basic material from Specular setup to Metallic
        1. Getting ready
        2. How to do it...
        3. How it works...
      4. Applying Normal maps to a material
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Adding Transparency and Emission maps to a material
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using texture maps with Transparent Mode
          2. Avoiding issues with the semi-transparent objects
          3. Emitting light over other objects
      6. Highlighting materials at mouse over
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      7. Adding Detail maps to a material
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Fading the transparency of a material
        1. How to do it...
        2. How it works...
        3. There's more...
      9. Playing videos inside a scene
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      10. Conclusion
        1. Resources
          1. References
          2. Tools
    13. 5. Using Cameras
      1. Introduction
        1. The big picture
      2. Creating a picture-in-picture effect
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Making the picture-in-picture proportional to the screen's size
          2. Changing the position of the picture-in-picture
          3. Preventing the picture-in-picture from updating on every frame
        5. See also
      3. Switching between multiple cameras
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using a single-enabled camera
          2. Triggering the switch from other events
        5. See also
      4. Making textures from screen content
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Applying your texture to a material
          2. Using your texture as a screenshot
        5. See also
      5. Zooming a telescopic camera
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Displaying a mini-map
        1. Getting ready...
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Covering a wider or narrower area
          2. Changing the map's orientation
          3. Adapting your mini-map to other styles
      7. Creating an in-game surveillance camera
        1. Getting ready
        2. How to do it...
        3. How it works...
    14. 6. Lights and Effects
      1. Introduction
        1. The big picture
          1. Lights
          2. Environment Lighting
          3. Emissive materials
          4. Projector
          5. Lightmaps and Light Probes
          6. The Lighting window
      2. Using lights and cookie textures to simulate a cloudy day
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Creating Spot Light cookies
          2. Creating Point Light Cookies
      3. Adding a custom Reflection map to a scene
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Mapping coordinates
          2. Sharp reflections
          3. Maximum size
      4. Creating a laser aim with Projector and Line Renderer
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Reflecting surrounding objects with Reflection Probes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Setting up an environment with Procedural Skybox and Directional Light
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Lighting a simple scene with Lightmaps and Light Probes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Conclusion
    15. 7. Controlling 3D Animations
      1. Introduction
        1. The big picture
      2. Configuring a character's Avatar and idle animation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      3. Moving your character with root motion and Blend Trees
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Mixing animations with Layers and Masks
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Organizing States into Sub-state Machines
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Transforming the Character Controller via script
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Adding rigid props to animated characters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Using Animation Events to throw an object
        1. Getting ready
        2. How to do it...
        3. How it works...
      9. Applying Ragdoll physics to a character
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      10. Rotating the character's torso to aim a weapon
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    16. 8. Positions, Movement and Navigation for Character GameObjects
      1. Introduction
        1. The big picture
      2. Player control of a 2D GameObject (and limiting the movement within a rectangle)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Player control of a 3D GameObject (and limiting the movement within a rectangle)
        1. How to do it...
        2. How it works...
      4. Choosing destinations – find the nearest (or a random) spawn point
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Choosing the nearest spawn point
          2. Avoiding errors due to an empty array
        5. See also
      5. Choosing destinations – respawn to the most recently passed checkpoint
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. NPC NavMeshAgent to seek or flee destination while avoiding obstacles
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Constantly updating the NavMeshAgent destination to Player's character current location
          2. Constantly update NavMeshAgent destination to flee away from Player's character current location
          3. Create a mini point-and-click game
      7. NPC NavMeshAgent to follow the waypoints in a sequence
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. More efficient to avoid using NavMeshes for waypoints
          2. Working with arrays of waypoints
          3. Increased flexibility with a WayPoint class
      8. Controlling the object group movement through flocking
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Learn more about flocking Artificial Intelligence
      9. Conclusion
    17. 9. Playing and Manipulating Sounds
      1. Introduction
        1. The big picture
      2. Matching the audio pitch to the animation speed
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Changing the Animation/Sound Ratio
          2. Accessing the function from other scripts
      3. Simulating acoustic environments with Reverb Zones
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Attaching the Audio Reverb Zone component to Audio Sources
          2. Making your own Reverb settings
      4. Preventing an Audio Clip from restarting if it is already playing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Waiting for audio to finish playing before auto-destructing an object
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Adding volume control with Audio Mixers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Playing with Audio Production
        5. See also
      7. Making a dynamic soundtrack with Snapshots
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Reducing the need for multiple audio clips
          2. Dealing with audio file formats and compression rates
          3. Applying Snapshots to background noise
          4. Getting creative with effects
        5. See also
      8. Balancing in-game audio with Ducking
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    18. 10. Working with External Resource Files and Devices
      1. Introduction
        1. The big picture
      2. Loading external resource files – using Unity Default Resources
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Loading text files with this method
          2. Loading and playing audio files with this method
        5. See also
      3. Loading external resource files – by downloading files from the Internet
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Converting from Texture to Sprite
          2. Downloading a text file from the Web
          3. The WWW class and the resource contents
        5. See also
      4. Loading external resource files – by manually storing files in the Unity Resources folder
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Avoiding cross-platform problems with Path.Combine() rather than "/" or "\"
        5. See also
      5. Saving and loading player data – using static properties
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Hiding the score before the first attempt completed
        5. See also
      6. Saving and loading player data – using PlayerPrefs
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Saving screenshots from the game
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Setting up a leaderboard using PHP/MySQL
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Extracting the full leaderboard data as XML for display within Unity
          2. Using the secret game codes to secure your leaderboard scripts
        5. See also
      9. Loading game data from a text file map
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Managing Unity project code using Git version control and GitHub hosting
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Learn more about Distributed Version Control Systems (DVCS)
          2. Using Bitbucket and SourceTree
          3. Using the command line rather than Git-client application
        5. See also
      11. Publishing for multiple devices via Unity Cloud
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Learn more about Unity Cloud
        5. See also
    19. 11. Improving Games with Extra Features and Optimization
      1. Introduction
        1. The big picture
      2. Pausing the game
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Learning more about QualitySettings
        5. See also
      3. Implementing slow motion
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Customizing the slider
          2. Adding Motion Blur
          3. Creating sonic ambience
        5. See also
      4. Preventing your game from running on unknown servers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Improving security by using full URLs in your domain list
          2. Allowing redistribution with more domains
      5. State-driven behavior Do-It-Yourself states
        1. How to do it...
        2. How it works...
        3. See also
      6. State-driven behavior using the State Design pattern
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Reducing the number of objects by destroying objects at death a time
        1. How to do it...
        2. How it works...
        3. See also
      8. Reducing the number of enabled objects by disabling objects whenever possible
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Note – viewable in Scene panel still counts as visible!
          2. Another common case – only enable after OnTrigger()
        5. See also
      9. Reducing the number of active objects by making objects inactive whenever possible
        1. How to do it...
        2. How it works...
        3. See also
      10. Improving efficiency with delegates and events and avoiding SendMessage!
        1. How to do it...
        2. How it works...
        3. See also
      11. Executing methods regularly but independent of frame rate with coroutines
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Have different actions happening at different intervals
        4. See also
      12. Spreading long computations over several frames with coroutines
        1. How to do it...
        2. How it works...
        3. There's more...
          1. Retrieving the complete Unity log text files from your system
        4. See also
      13. Evaluating performance by measuring max and min frame rates (FPS)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Turn off runtime display to reduce FPS processing
        5. See also
      14. Identifying performance bottlenecks with the Unity performance Profiler
        1. How to do it...
        2. How it works...
        3. See also
      15. Identifying performance "bottlenecks" with Do-It-Yourself performance profiling
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      16. Cache GameObject and component references to avoid expensive lookups
        1. Getting ready
        2. How to do it...
          1. Method 1 – AverageDistance calculation
          2. Method 2 – Cache array of Respawn object transforms
          3. Method 3 – Cache reference to Player transform
          4. Method 4 – Cache Player's Vector3 position
          5. Method 5 – Cache reference to SimpleMath component
          6. Method 6 – Cache array of sphere Vector3 positions
        3. How it works...
        4. See also
      17. Improving performance with LOD groups
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Adding more LOD renderers
          2. Fading LOD transitions
        5. See also
      18. Improving performance through reduced draw calls by designing for draw call batching
        1. How to do it...
          1. Static batching
          2. Dynamic batching
        2. There's more...
          1. Reduce the need for textures by vertex painting
          2. Information sources about reducing textures and materials
        3. See also
      19. Conclusion
        1. There's more...
          1. Game audio optimization
          2. Physics engine optimization
          3. More tips for improving script efficiency
          4. Sources of more wisdom about optimization
          5. Published articles that discuss premature optimization
          6. Sources of more about Game Managers and the State Pattern
    20. 12. Editor Extensions
      1. Introduction
      2. An editor extension to allow pickup type (and parameters) to be changed at design time via a custom Inspector UI
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Offer the custom editing of pickup parameters via Inspector
          2. Offer a drop-down list of tags for key-pickup to fit via Inspector
          3. Logic to open doors with keys based on fitsLockTag
          4. The need to add [SerializeField] for private properties
          5. Learn more from the Unity documentation
      3. An editor extension to add 100 randomly located copies of a prefab with one menu click
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Child each new GameObject to a single parent, to avoid filling up the Hierarchy with 100s of new objects
      4. A progress bar to display proportion completed of Editor extension processing
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. An editor extension to have an object-creator GameObject, with buttons to instantiate different pickups at cross-hair object location in scene
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Conclusion
    21. Index

Product information

  • Title: Unity 5.x Cookbook
  • Author(s): Matt Smith, Chico Queiroz
  • Release date: October 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784391362