Unity 2021 Cookbook
Fourth Edition
Over 140 recipes to take your Unity game development skills to the next level
Matt Smith
Shaun Ferns
BIRMINGHAM - MUMBAI
Unity 2021 Cookbook
Fourth Edition
Copyright 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Pavan Ramchandani
Publishing Product Manager: Pavan Ramchandani
Senior Editor: Sofi Rogers
Content Development Editor: Rakhi Patel
Technical Editor: Simran Udasi
Copy Editor: Safis Editing
Project Coordinator: Manthan Patel
Proofreader: Safis Editing
Indexer: Tejal Soni
Production Designer: Shankar Kalbhor
First published: June 2013
Second edition: October 2015
Third edition: August 2018
Fourth edition: September 2021
Production reference: 1030921
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-83921-761-6
www.packt.com
I dedicate this book to my daughter, Charlotte.
Matt Smith
I dedicate this book to Martina, Toms, and Snan.
Shaun Ferns
Foreword
Not so long ago, developing professional quality games meant licensing an expensive game engine or writing your own from scratch. Then, you needed to hire a small army of developers to use it. Today, game engines like Unity have democratized game development to the point where you can simply download the tools and start making the game of your dreams right away.
Well... kinda. Having a powerful game creation tool is not the same thing as having the technical knowledge and skills to use it effectively.
I started coding games as a kid on my trusty ZX Spectrum, Commodore 64 & later the Amiga. I've been working as a professional game developer since 2003. When I first took the plunge into learning Unity development to create the Fungus storytelling tool, I found a huge amount of online documentation, tutorials, and forum answers available for Unity developers. This makes getting started with Unity development relatively easy, but the information can also be quite fragmented. Often, the last piece of the puzzle you need is buried 40 minutes into an hour-long tutorial video or on the 15th page of a forum thread. The hours you spend looking for these nuggets of wisdom is time that would be better spent working on your game.
The beauty of the Unity Cookbooks is that Matt, Chico, and Shaun have distilled this knowledge into a neat collection of easy-to-follow recipes, and they have provided the scripts and complete working projects so that you can put it to use straight away.
In this latest edition for Unity 2021, Matt and Shaun have updated the recipes from the previous book and added new recipes to introduce many of the latest Unity features. These include topics such as Augmented Reality and XR web publishing, particle systems, 2D physics, the 2021 Unity Starter Assets packages, code coverage, and running Python scripts in Unity.
Getting started with Unity development is free and easy. When you're ready to take your skills to the next level, this book is an effective way to do just that. It covers a great deal in its hundreds of pages, and if you can master even half of what's here, you'll be well on the way to becoming a great Unity developer!
Chris Gregan
Chief Architect, Romero Games: www.romerogames.ie
Author of Fungus: fungusgames.com
Contributors
About the authors
Matt Smith is a computing academic at TU Dublin, the Technological University of Dublin, Ireland, where he leads the DRIVE Research Group (Digital Realities, Interaction, and Virtual Environments). He has been researching and teaching interactive game and web technologies since he moved to Ireland in 2002. Matt started computer programming on a brand new ZX80 and submitted two games for his computing O-level exam in 1985. After nearly 10 years as a full-time student on a succession of scholarships, he gained several degrees in computing, including a Ph.D. in computational musicology. Since 1994, he has been a full-time computer science lecturer at the University of Winchester (UK), then Middlesex University (UK), and now TU Dublin (Ireland).
In 1985, Matt wrote the lyrics and was in the band whose music appeared on the B-side of the audio cassette carrying the computer game Confuzion (look up the game's Wikipedia page!). Matt is one of the documentation authors for the free, open source Fungus Unity visual scripting and dialogue system. He enjoys sports, martial arts, and music, playing several instruments enthusiastically, if not very well. To get away from the computer completely, he has taken up carpentry and is currently planning to build an oak staircase (with a landing!), which will be his biggest project yet...
Thanks to my family for all their support. Thanks also to the editors, reviewers, and readers who provided feedback and suggestions. Thanks to my students, who continue to challenge and surprise me with their enthusiasm for multimedia and game development. Special thanks to Kris for help with the VR recipes and Nina for the AR recipes. Also thanks to Justin in Limerick for keeping me sane with snooker, golf breaks, and the newly installed full-size table tennis table and congratulations on getting that first-class degree summa cum laude in 2021! Many thanks to Shaun for coming on board to coauthor this edition; given all the additional challenges this year, I don't think this book would have been completed without him, and I look forward to future collaborations.
Shaun Ferns is an academic at TU Dublin, the Technological University of Dublin, Ireland, where he is a researcher in the DRIVE Research Group (Digital Realities, Interaction, and Virtual Environments) and an associate researcher at the Educational Informatics Lab (EILab) at OntarioTechU. Since 2016, he has been primarily researching and teaching multimedia development, and prior to that was involved in the delivery of several engineering programs. He is currently exploring the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts and serious games for the built environment.
Next page