Unity 2021 Cookbook: Over 160 recipes to take your Unity game development skills to the next level [4 ed.] 9781839217616

Unity is the most widely used game development platform worldwide. Available for Windows, macOS, and Linux, Unity is a p

723 242 106MB

English Pages 775 Year 2021

Report DMCA / Copyright

DOWNLOAD FILE

Unity 2021 Cookbook: Over 160 recipes to take your Unity game development skills to the next level [4 ed.]
 9781839217616

Table of contents :
Title Page
Copyright and Credits
Dedication
Foreword
Contributors
Table of Contents
Preface
Chapter 1: Displaying Data with Core UI Elements
Technical requirements
Displaying a "Hello World" UI text message
Getting ready
How to do it...
How it works...
There's more...
Styling substrings with rich text
Displaying a digital clock
Getting ready
How to do it...
How it works...
Displaying a digital countdown timer
Getting ready
How to do it...
How it works...
Creating a message that fades away
Getting ready
How to do it...
How it works...
Displaying a perspective 3D Text Mesh
Getting ready
How to do it...
How it works...
There's more...
Making the text crawl as it does in the movie
Where to learn more
Creating sophisticated text with TextMeshPro
Getting ready
How to do it...
How it works...
There's more...
Rich text substrings for colors, effects, and sprites
Displaying an image
Getting ready
How to do it...
How it works...
There's more...
Working with 2D sprites and UI Image components
See also
Creating UIs with the Fungus open source dialog system
How to do it...
How it works...
Creating a Fungus character dialog with images
How to do it...
How it works...
There's more...
Further reading
Chapter 2: Responding to User Events for Interactive UIs
Technical requirements
Creating UI Buttons to move between scenes
How to do it...
How it works...
There's more...
Animating button properties on mouseover
How to do it...
How it works...
Organizing image panels and changing panel depths via buttons
Getting ready
How to do it...
How it works...
There's more...
Moving up or down by just one position, using scripted methods
Displaying the value of an interactive UI Slider
How to do it...
How it works...
Displaying a countdown timer graphically with a UI Slider
Getting ready
How to do it...
How it works...
Setting custom mouse cursors for 2D and 3D GameObjects
Getting ready
How to do it...
How it works...
Setting custom mouse cursors for UI controls
Getting ready
How to do it...
How it works...
Interactive text entry with Input Field
How to do it...
How it works...
There's more...
Toggles and radio buttons via toggle groups
Getting ready
How to do it...
How it works...
There's more...
Creating text and image icon UI Drop-down menus
Getting ready
How to do it...
How it works...
There's more...
Adding images to a Dropdown control
Displaying a radar to indicate the relative locations of objects
Getting ready
How to do it...
How it works...
The Start() method
The Update() method
The FindAndDisplayBlipsForTag(...) method
The CalculateBlipPositionAndDrawBlip (...) method
The NormalizedPosition(...) method
The CalculateBlipPosition(...) method
The DrawBlip() method
There's more...
Chapter 3: Inventory and Advanced UIs
Technical requirements
Creating a simple 2D mini-game – SpaceGirl
Getting ready
How to do it...
How it works...
Displaying single object pickups with carrying and not-carrying text
Getting ready
How to do it...
How it works...
The PlayerInventory script class
The PlayerInventoryDisplay script class
There's more...
Collecting multiple items and display the total number carried
Alternative – combining all the responsibilities into a single script
Displaying single-object pickups with carrying and not-carrying icons
Getting ready
How to do it...
How it works...
Displaying multiple pickups of the same object with multiple status icons
Getting ready
How to do it...
How it works...
There's more...
Revealing icons for multiple object pickups by changing the size of a tiled image
Using panels to visually outline the inventory UI area and individual items
Getting ready
How to do it...
How it works...
Creating a C# inventory slot UI to display scripted components
Getting ready
How to do it...
How it works...
There's more...
Modifying the game for a second inventory panel for keys
Using UI Grid Layout Groups to automatically populate a panel
Getting ready
How to do it...
How it works...
There's more...
Automatically inferring the number of inventory slots based on the number of GameObjects tagged Star
Adding a horizontal scroll bar to the inventory slot display
Automatically changing the grid cell size based on the number of slots in the inventory
Displaying multiple pickups of different objects as a list of text via a dynamic list of scripted PickUp objects
Getting ready
How to do it...
How it works...
There's more...
Ordering items in the inventory list alphabetically
Using a Dictionary and Enums to display text totals for different objects
Getting ready
How to do it...
How it works...
Creating a runtime UI Toolkit interface
Getting ready
How to do it...
How it works...
Further reading
Chapter 4: Playing and Manipulating Sounds
Technical requirements
Playing different one-off sound effects with a single AudioSource component
Getting ready
How to do it...
How it works...
There's more...
Playing a sound at a static point in 3D world space
Playing and controlling different sounds each with its own AudioSource component
Getting ready
How to do it...
How it works...
Creating just-in-time AudioSource components at runtime through C# scripting
Getting ready
How to do it...
How it works...
There's more...
Adding the CreateAudioSource(...) method as an extension to the MonoBehavior class
Delaying before playing a sound
Getting ready
How to do it...
How it works...
Preventing an audio clip from restarting if it is already playing
Getting ready
How to do it...
How it works...
Waiting for the audio to finish playing before auto-destructing an object
Getting ready
How to do it...
How it works...
See also
Scheduling a sound to play at a certain time
Getting ready
How to do it...
How it works...
Audio visualization from sample spectral data
Getting ready
How to do it...
How it works...
There's more...
Adding visualizations to a second AudioSource component
Trying out different Fast Fourier Transform (FFT) window types
Synchronizing simultaneous and sequential music to create a simple 140 bpm music-loop manager
Getting ready
How to do it...
How it works...
There's more...
Adding visualizations to the four playing loops
Further reading
Chapter 5: Creating 3D Objects, Terrains, Textures, and Materials
Technical requirements
Creating 3D primitives and adding materials and textures
Getting ready
How to do it...
How it works...
There's more...
Enhancing the scene – adding a spotlight and wood material
Creating a new Material asset file in the Project window and then setting its Albedo to a texture
Converting and importing 3D models into a project
Getting ready
How to do it...
How it works...
Highlighting GameObject materials on mouseover
Getting ready
How to do it...
How it works...
There's more...
Collider needed for custom meshes
Changing the material's color in response to mouse events
Fading the transparency of a material
How to do it...
How it works...
There's more...
Destroying objects when fading is complete
Using the GameObject's alpha as our starting alpha value
Using a coroutine for our fading loop
Creating geometry with ProBuilder
How to do it...
How it works...
Creating a house with ProBuilder
How to do it...
How it works...
Creating and texture-painting terrains
How to do it...
How it works...
Height painting terrains
Getting ready
How to do it...
How it works...
Adding Terrain holes
Getting ready
How to do it...
How it works...
There's more...
Further reading
Chapter 6: 2D Animation and Physics
Technical requirements
Flipping a sprite horizontally – the DIY approach
Getting ready
How to do it...
How it works...
There's more...
Flipping a sprite horizontally – using Animator State Chart and transitions
Getting ready
How to do it...
How it works...
There's more...
Instantaneous swapping
Animating body parts for character movement events
Getting ready
How to do it...
How it works...
Creating a three-frame animation clip to make a platform continually animate
Getting ready
How to do it...
How it works...
There's more...
Copying the animation relative to a new parent GameObject
Making a platform start falling once stepped on using a Trigger to move the animation from one state to another
Getting ready
How to do it...
How it works...
Creating animation clips from sprite sheet sequences
Getting ready
How to do it...
How it works...
Creating a platform game with tiles and tilemaps
Getting ready
How to do it...
How it works...
There's more...
Tile palettes for objects and walls
Using sprite placeholders to create a simple physics scene
Getting ready
How to do it...
How it works...
There's more...
Editing polygon Colliders for more realistic 2D physics
Getting ready
How to do it...
How it works...
Creating an explosionForce method for 2D physics objects
Getting ready
How to do it...
How it works...
There's more...
Clipping via Sprite Masking
Getting ready
How to do it...
How it works...
There's more...
Further reading
Chapter 7: Characters, Game Kits, and Starter Assets
Technical requirements
Creating a game with the 3D Game Kit
How to do it...
How it works...
There's more...
Creating a game with the 2D Game Kit
Getting ready
How to do it...
How it works...
Importing third-party 3D models and animations from Mixamo
Getting ready
How to do it...
How it works...
There's more...
Looping the animation
Scripting events to control when animation clips are played
Swapping the Standard Assets Ethan for a different character
Getting ready
How to do it...
How it works...
Importing a 3D model and adding an Animation Controller
How to do it...
How it works...
Using scripts to control 3D animations
Getting ready
How to do it...
How it works...
There's more...
Importing and using a UMA free character
How to do it...
How it works...
There's more...
Getting started with the 2021 Starter Assets package
How to do it...
How it works...
Further reading
Chapter 8: Web Server Communication and Online Version Control
Technical requirements
Setting up a leaderboard using PHP and a database
Getting ready
How to do it...
How it works...
There's more...
SQLite, PHP, and database servers
phpLiteAdmin
See also
Unity game communication with a web server leaderboard
Getting ready
How to do it...
How it works...
There's more...
Extracting the full leaderboard data for display within Unity
Using secret game codes to secure your leaderboard scripts
See also
Creating and cloning a GitHub repository
Getting ready
How to do it...
How it works...
There's more...
Learn more about DVCS
Learn more about Git at the command line
Using Bitbucket and Sourcetree visual applications
Learning about Mercurial rather than Git
Adding a Unity project to a local Git repository, and pushing files up to GitHub
Getting ready
How to do it...
How it works...
Unity project version control using GitHub for Unity
Getting ready
How to do it...
How it works...
There's more...
Further reading on GitHub for Unity
Pulling down updates from other developers
Unity Collaborate from Unity Technologies
Further reading
Chapter 9: Controlling and Choosing Positions
Technical requirements
Using a rectangle to constrain 2D Player object movement
Getting ready
How to do it...
How it works...
There's more...
Drawing a gizmo yellow rectangle to visually show a bounding rectangle
Player control of a 3D GameObject (and limiting movement within a rectangle)
How to do it...
How it works...
There's more...
Drawing a gizmo yellow rectangle to visually show a bounding rectangle
Drawing thick gizmo lines
Choosing destinations – finding a random spawn point
Getting ready
How to do it...
How it works...
See also
Choosing destinations – finding the nearest spawn point
Getting ready
How to do it...
How it works...
There's more...
Avoiding errors due to an empty array
See also
Choosing destinations – respawning to the most recently passed checkpoint
Getting ready
How to do it...
How it works...
Moving objects by clicking on them
Getting ready
How to do it...
How it works...
Firing projectiles in the direction of movement
Getting ready
How to do it...
How it works...
Chapter 10: Navigation Meshes and Agents
Technical requirements
NPC to travel to destination while avoiding obstacles
Getting ready
How to do it...
How it works...
NPC to seek or flee from a moving object
Getting ready
How to do it...
How it works...
There's more...
Using a Debug Ray to show a source-to-destination line
Constantly updating the NavMeshAgent's destination to flee from the player's current location
Maintaining a constant distance from the target ("lurking" mode!)
Point-and-click move to object
Getting ready
How to do it...
How it works...
There's more...
Creating a mouseover yellow highlight
Point-and-click move to tile
Getting ready
How to do it...
How it works...
There's more...
Using a yellow debug ray to show the destination of the AI agent
Point-and-click raycast with user-defined, higher-cost navigation areas
Getting ready
How to do it...
How it works...
There's more...
More intelligent pathfinding by setting different costs for custom-defined navigation areas such as mud and water
Improving the UX by updating a "gaze" cursor each frame
NPC NavMeshAgent to follow waypoints in a sequence
Getting ready
How to do it...
How it works...
There's more...
Working with arrays of waypoints
Increased flexibility with the WayPoint class
Controlling object group movement through flocking
Getting ready
How to do it...
How it works...
Creating a movable NavMesh Obstacle
Getting ready
How to do it...
How it works...
Further reading
Chapter 11: Cameras and Rendering Pipelines
Technical requirements
Creating the basic scene for this chapter
Getting ready
How to do it...
How it works...
There's more...
Working with a fixed Main Camera
Getting ready
How to do it...
How it works...
Changing how much of the screen a camera renders to
Getting ready
How to do it...
How it works...
There's more...
Using Render Textures to send camera output to places other than the screen
Getting ready
How to do it...
How it works...
There's more...
Inverting our mirror camera horizontally
A simple snapshot of a scene at runtime
Working with Unity's multipurpose camera rig
How to do it...
How it works...
Using Cinemachine ClearShot to switch cameras to keep the player in shot
Getting ready
How to do it...
How it works...
There's more...
Unity Cinemachine tutorials
Will Goldstone's ClearShot tutorial
Adam Myhill's Cinemachine blog posts
Reading the installed Cinemachine documentation
Cinemachine and Timeline
Letting the player switch to a Cinemachine FreeLook camera
Getting ready
How to do it...
How it works...
Creating a project with the URP
Getting ready
How to do it...
How it works...
Adding a vignette effect
Getting ready
How to do it...
How it works...
There's more...
Creating an HDRP project with an HDRI skybox
Getting ready
How to do it...
How it works...
Further reading
Chapter 12: Shader Graphs and Video Players
Technical requirements
Playing videos by manually adding a VideoPlayer component to a GameObject
Getting ready
How to do it...
How it works...
There's more...
Using scripting to control video playback on scene textures
Getting ready
How to do it...
How it works...
There's more...
Downloading an online video (rather than a clip)
Ensuring a movie is prepared before playing
Getting ready
How to do it...
How it works...
There's more...
Ensuring that the movie has been prepared before playing it with coroutines
Outputting video playback to a RenderTexture asset
Getting ready
How to do it...
How it works...
Using scripting to play a sequence of videos back to back
Getting ready
How to do it...
How it works...
Creating and using a simple Shader Graph
How to do it...
How it works...
Creating a glow effect with Shader Graph
Getting ready
How to do it...
How it works...
Toggling a Shader Graph color glow effect through C# code
Getting ready
How to do it...
How it works...
There's more...
Further reading
Shader Graph online resources
Video player online resources
Chapter 13: Advanced Topics - Gizmos, Automated Testing, and More
Technical requirements
Using Gizmo to show the currently selected object in the Scene window
How to do it...
How it works...
See also
Creating an editor snap-to-grid drawn by a Gizmo
How to do it...
How it works...
There's more...
Saving and loading player data – using static properties
Getting ready
How to do it...
How it works...
There's more...
Hiding the score before the first attempt is completed
See also
Saving and loading player data – using PlayerPrefs
Getting ready
How to do it...
How it works...
See also
Loading game data from a text file map
Getting ready
How to do it...
How it works...
Saving data to a file while the game is running
How to do it...
How it works...
See also
Generating and running a default test script class
How to do it...
How it works...
There's more...
Creating a default test script from the Project window's Create menu
Edit mode minimum skeleton unit test script
A simple unit test
How to do it...
How it works...
There's more...
Shorter tests with values in the assertion
Expected value followed by the actual value
Parameterizing tests with a data provider
How to do it...
How it works...
Unit testing a simple health script class
How to do it...
How it works...
Health.cs
TestHealth.cs
Creating and executing a unit test in PlayMode
How to do it...
How it works...
PlayMode testing a door animation
Getting ready
How to do it...
How it works...
There's more...
PlayMode and unit testing a player health bar with events, logging, and exceptions
Getting ready
How to do it...
How it works...
PlayMode testing
Unit tests
See also
Reporting Code Coverage testing
Getting ready
How to do it...
How it works...
Running simple Python scripts inside Unity
How to do it...
How it works...
Further reading
Chapter 14: Particle Systems and Other Visual Effects
Technical requirements
Exploring Unity's Particle Pack and reusing samples for your own games
How to do it...
How it works...
Creating a simple particle system from scratch
How to do it...
How it works...
Using Texture Sheets to simulate fire with a particle system
Getting ready
How to do it...
How it works...
Making particles collide with scene objects
How to do it...
How it works...
There's more...
Simulating an explosion
Getting ready
How to do it...
How it works...
Using Invoke to delay the execution of an explosion
Getting ready
How to do it...
How it works...
There's more...
Further reading
Chapter 15: Virtual and Augmented Reality (VR/AR)
Technical requirements
Setting up Unity for VR
How to do it...
How it works...
Setting up an Oculus Quest/2 in Developer Mode
Getting ready
How to do it...
How it works...
Creating a VR project for the Oculus Quest/2
Getting ready
How to do it...
How it works...
There's more...
Build failure due to Android version
Build failure due to "namespace cannot be found" error
Sideloading APK files to an Android device (such as the Quest headset)
Creating a VR project using the Mock HMD device
Getting ready
How to do it...
How it works...
Working with the Mozilla demo WebXR project
Getting ready
How to do it...
How it works...
There's more...
Using GitHub Pages to publish your WebXR project for free
Learning more about the WebXR example project
Fixing pink (shader/material) problems
The community maintaining more up-to-date XR resources
Fixing obsolete code errors
Adding 360-degree videos to a VR project
Getting ready
How to do it...
How it works...
There's more...
Playing 360-degree videos on the surface of a 3D object
Exploring the Unity AR samples
Getting ready
How to do it...
How it works...
There's more...
Targeting iOS device builds
Allowing Android APK file downloads on your phone
Setting up Unity for AR
How to do it...
How it works...
There's more...
Build failure due to Android version
Build failure due to Vulkan API graphics settings
Creating a simple AR Foundation project
Getting ready
How to do it...
How it works...
Detecting and highlighting planes with AR Foundation
Getting ready
How to do it...
How it works...
Creating an AR furniture previewer by detecting horizontal planes
Getting ready
How to do it...
How it works...
Creating a floating 3D model over an image target
Getting ready
How to do it...
How it works...
Further reading
Index

Polecaj historie