Unity in Action, Second Edition
Multiplatform game development in C#
Joseph Hocking
Foreword by Jesse Schell
  • March 2018
  • ISBN 9781617294969
  • 400 pages
  • printed in black & white

Joe Hocking wastes none of your time and gets you coding fast.

From the Foreword by Jesse Schell, author of The Art of Game Design

GET MORE WITH MANNING

An eBook copy of the previous edition, Unity in Action (First Edition), is included at no additional cost. It will be automatically added to your Manning Bookshelf within 24 hours of purchase.


Manning's bestselling and highly recommended Unity book has been fully revised! Unity in Action, Second Edition teaches you to write and deploy games with the Unity game development platform. You'll master the Unity toolset from the ground up, adding the skills you need to go from application coder to game developer.

Table of Contents detailed table of contents

Getting to know Unity

Why is Unity so great?

Unity's strengths and advantages

Downsides to be aware of

Example games built with Unity

How to use Unity

Scene view, Game view, and the Toolbar

Using the mouse and keyboard

The Hierarchy tab and the Inspector

The Project and Console tabs

Getting up and running with Unity programming

How code runs in Unity: script components

Using MonoDevelop, the cross-platform IDE

Printing to the console: Hello World!

Summary

Building a demo that puts you in 3D space

Before you start?

Planning the project

Understanding 3D coordinate space

Begin the project: place objects in the scene

The scenery: floor, outer walls, inner walls

Lights and cameras

The player?s collider and viewpoint

Making things move: a script that applies transforms

Diagramming how movement is programmed

Writing code to implement the diagram

Local vs. global coordinate space

Script component for looking around: MouseLook

Horizontal rotation that tracks mouse movement

Vertical rotation with limits

Horizontal and vertical rotation at the same time

Keyboard input component: first-person controls

Responding to key presses

Setting a rate of movement independent of the computer?s speed

Moving the CharacterController for collision detection

Adjusting components for walking instead of flying

Summary

Adding enemies and projectiles to the 3D game

Shooting via raycasts

What is raycasting?

Using the command ScreenPointToRay for shooting

Adding visual indicators for aiming and hits

Scripting reactive targets

Determining what was hit

Alert the target that it was hit

Basic wandering AI

Diagramming how basic AI works

?Seeing? obstacles with a raycast

Tracking the character?s state

Spawning enemy prefabs

What is a prefab?

Creating the enemy prefab

Instantiating from an invisible SceneController

Shooting via instantiating objects

Creating the projectile prefab

Shooting the projectile and colliding with a target

Damaging the player

Summary

Developing graphics for your game

Understanding art assets

Building basic 3D scenery: whiteboxing

Whiteboxing explained

Drawing a floor plan for the level

Laying out primitives according to the plan

Texture the scene with 2D images

Choosing a file format

Importing an image file

Applying the image

Generating sky visuals using texture images

What is a skybox?

Creating a new skybox material

Working with custom 3D models

Which file format to choose?

Exporting and importing the model

Creating effects using particle systems

Adjusting parameters on the default effect

Applying a new texture for fire

Attaching particle effects to 3D objects

Summary

Part 2: Getting comfortable

Building a Memory game using Unity?s 2D functionality

Setting everything up for 2D graphics

Preparing the project

Displaying 2D images (aka sprites)

Switching the camera to 2D mode

Building a card object and making it react to clicks

Building the object out of sprites

Mouse input code

Revealing the card on click

Displaying the various card images

Loading images programmatically

Setting the image from an invisible SceneController

Instantiating a grid of cards

Shuffling the cards

Making and scoring matches

Storing and comparing revealed cards

Hiding mismatched cards

Text display for the score

Restart button

Programming a UIButton component using SendMessage

Calling LoadScene from SceneController

Summary

Creating a basic 2D Platformer

Setting Up the Graphics

Placing Walls and Floor

Importing Sprite Sheets

Moving the Player Left and Right

Writing Keyboard Controls

Colliding with the Walls

Playing the Sprite?s Animation

Explaining the Mecanim Animation System

Triggering Animations from Code

Adding the Ability to Jump

Falling from Gravity

Applying an Upward Impulse

Detecting the Ground

Additional Features for a Platform Game

Unusual Floors: Slopes and One-Way Platforms

Implementing Moving Platforms

Camera Control

Summary

Putting a GUI onto a game

Before you start writing code?

Immediate mode GUI or advanced 2D interface?

Planning the layout

Importing UI images

Setting up the GUI display

Creating a canvas for the interface

Buttons, images, and text labels

Controlling the position of UI elements

Programming interactivity in the UI

Programming an invisible UIController

Creating a pop-up window

Setting values using sliders and input fields

Updating the game by responding to events

Integrating an event system

Broadcasting and listening for events from the scene

Broadcasting and listening for events from the HUD

Summary

Creating a third-person 3D game: player movement and animation

Adjusting the camera view for third-person

Importing a character to look at

Adding shadows to the scene

Orbiting the camera around the player character

Programming camera-relative movement controls

Rotating the character to face movement direction

Moving forward in that direction

Implementing the jump action

Applying vertical speed and acceleration

Modifying the ground detection to handle edges and slopes

Setting up animations on the player character

Defining animation clips in the imported model

Creating the animator controller for these animations

Writing code that operates the animator

Summary

Adding interactive devices and items within the game

Creating doors and other devices

Doors that open and close on a keypress

Checking distance and facing before opening the door

Operating a color-changing monitor

Interacting with objects by bumping into them

Colliding with physics-enabled obstacles

Triggering the door with a pressure plate

Collecting items scattered around the level

Managing inventory data and game state

Setting up player and inventory managers

Programming the game managers

Storing inventory in a collection object: List vs. Dictionary

Inventory UI for using and equipping items

Displaying inventory items in the UI

Equipping a key to use on locked doors

Restoring the player?s health by consuming health packs

Summary

Part 3: Strong finish

Connecting your game to the internet

Creating an outdoor scene

Generating sky visuals using a skybox

Setting up an atmosphere that?s controlled by code

Downloading weather data from an internet service

Requesting HTTP data using coroutines

Parsing XML

Parsing JSON

Affecting the scene based on weather data

Adding a networked billboard

Loading images from the internet

Displaying images on the billboard

Caching the downloaded image for reuse

Posting data to a web server

Tracking current weather: sending post requests

Server-side code in PHP

Summary

Playing audio: sound effects and music

Importing sound effects

Supported file formats

Importing audio files

Playing sound effects

Explaining what?s involved: Audio Clip vs. Source vs. Listener

Assigning a looping sound

Triggering sound effects from code

Audio control interface

Setting up the central AudioManager

Volume control UI

Playing UI sounds

Background music

Playing music loops

Controlling music volume separately

Fading between songs

Summary

Putting the parts together into a complete game

Building an action RPG by repurposing projects

Assembling assets and code from multiple projects

Programming point-and-click controls: movement and devices

Replacing the old GUI with a new interface

Developing the overarching game structure

Controlling mission flow and multiple levels

Completing a level by reaching the exit

Losing the level when caught by enemies

Handling the player?s progression through the game

Saving and loading the player?s progress

Beating the game by completing three levels

Summary

Deploying your game to players? devices

Start by building for the desktop: Windows, Mac, and Linux

Building the application

Adjusting Player Settings: setting the game?s name and icon

Platform-dependent compilation

Building for the web

Unity Player vs. HTML5/WebGL

Building the game embedded in a web page

Communicating with JavaScript in the browser

Building for mobile: iOS and Android

Setting up the build tools

Texture compression

Developing plug-ins

Summary

Appendixes:

Appendix A: Scene navigation and keyboard shortcuts

Scene navigation using the mouse

Commonly used keyboard shortcuts

Appendix B: External tools used alongside Unity

Programming tools

Visual Studio

Xcode

Android SDK

SVN, Git, or Mercurial

3D art applications

Maya

3ds Max

Blender

SketchUp

2D image editors

Photoshop

GIMP

TexturePacker

Aseprite, Pyxel Edit

Audio software

Pro Tools

Audacity

Appendix C: Modeling a bench in Blender

Building the mesh geometry

Texture-mapping the model

Appendix D: Online learning resources

Additional tutorials

Code libraries

About the Technology

Build your next game without sweating the low-level details. The Unity game development platform handles the heavy lifting, so you can focus on game play, graphics, and user experience. With support for C# programming, a huge ecosystem of production-quality prebuilt assets, and a strong dev community, Unity can get your next great game idea off the drawing board and onto the screen!

About the book

Unity in Action, Second Edition teaches you to write and deploy games with Unity. As you explore the many interesting examples, you'll get hands-on practice with Unity's intuitive workflow tools and state-of-the-art rendering engine. This practical guide exposes every aspect of the game dev process, from the initial groundwork to creating custom AI scripts and building easy-to-read UIs. And because you asked for it, this totally revised Second Edition includes a new chapter on building 2D platformers with Unity's expanded 2D toolkit.

What's inside

  • Revised for new best practices, updates, and more!
  • 2D and 3D games
  • Characters that run, jump, and bump into things
  • Connect your games to the internet

About the reader

You need to know C# or a similar language. No game development knowledge is assumed.

About the author

Joe Hocking is a software engineer and Unity expert specializing in interactive media development.


FREE domestic shipping on three or more pBooks

Useful and to the point! Everything you need to know about Unity in a single resource.

Dan Kacenjar, Cornerstone Software

Increases the velocity with which you'll go from idea to finished game.

Christopher Haupt, Sanlam

I've wanted to program in Unity for a long time. The excellent examples in this book gave me the confidence to get started.

Robin Dewson, Schroders