Data Loading...

9781838642006 Flipbook PDF

No description


113 Views
41 Downloads
FLIP PDF 1.48MB

DOWNLOAD FLIP

REPORT DMCA

Hands-On

Unity 2020 Game Development Build, customize, and optimize professional games using Unity 2020 and C#

Nicolas Alejandro Borromeo FOR SALE IN INDIA ONLY

www.packt.com

Hands-On Unity 2020 Game Development Build, customize, and optimize professional games using Unity 2020 and C#

Nicolas Alejandro Borromeo

BIRMINGHAM—MUMBAI

Hands-On Unity 2020 Game Development Copyright © 2020 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 author, 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. Commissioning Editor: Pavan Ramchandani Acquisition Editor: Ashitosh Gupta Senior Editor: Hayden Edwards Content Development Editor: Akhil Nair Technical Editor: Deepesh Patel Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexer: Priyanka Dhadke Production Designer: Shankar Kalbhor First published: July 2020 Production reference: 1270720 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83864-200-6 www.packt.com

To Dad, who spoiled me with computers to help me keep learning – I miss you. Also, to my wife, for reminding me what I am capable of.

Packt.com

Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Improve your learning with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors About the author Nicolas Alejandro Borromeo is working as a Unity senior developer at Product Madness in London. He was a game development career coordinator at Universidad Argentina de la Empresa (UADE) and has been a game development teacher in many other Argentine universities, such as UADE, UTN, UAI, and USAL, and in institutions such as Image Campus and DaVinci, since 2012. Nicolas has been a Unity Certified Instructor since 2019, teaching for high-profile Unity clients all around the globe. He was an MMO client-side developer at Band of Coders in Argentina and has been a Unity freelance developer since 2012.

About the reviewer Sungkuk Park is a game engineer based in Berlin. He has participated in multiple game jams around the world and has created indie games to polish his skills. He is currently interested in the area of technical art, mainly CG, animation, gameplay, and VFX. In his spare time, he spends most of his time learning new skills, including drawing, animation, and VFX, that might come in handy when he pursues his next goal: being a game director for the next gaming generation!

Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors. packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents Preface

1

Designing a Game from Scratch Game concept

14

The difficulty balance

22

Input controls Winning and losing

15 16

Difficulty balance questions Implementation plan

23 24

Game characters

17

Documentation

25

Hero Enemies

17 18

Gameplay

18

Game-world layout Starting condition Ending condition Point system HUD

19 20 20 21 21

Game Design Document (GDD) GDD formats GDD creation tools Elevator pitch A high concept Tips for creating GDDs

25 26 26 28 29 30

Summary

33

Installing Unity

38

Unity versions Installing Unity with Unity Hub

39 39

Creating projects

45

Creating a project Project structure

45 48

Summary

50

2

Setting Up Unity Why use a game engine such as Unity? 36 Past and present industry insight Game engines Positives of Unity

36 37 37

ii Table of Contents

3

Working with Scenes and GameObjects Manipulating scenes

52

Possible uses

69

The purpose of a scene The Scene View Our first GameObject Navigating the Scene View Manipulating GameObjects

52 53 54 55 56

Prefabs

70

Creating Prefabs Prefab-instance relationship Prefab variants

71 72 75

GameObjects and components

60

Saving scenes and projects

77

Components Manipulating components

61 62

Saving our changes Project structure

77 78

Object hierarchies

68

Summary

80

Parenting of objects

68

4

Grayboxing with Terrain and ProBuilder Creating a Landscape with Terrain

82

Discussing Height Maps Creating and configuring Height Maps Authoring Height Maps Adding Height Map details

82 84 87 90

Installing ProBuilder Creating a Shape Manipulating the mesh Adding details

Summary

93 95 96 102

106

Creating Shapes with ProBuilder 93

5

Importing and Integrating Assets Importing assets Importing assets from the internet Importing assets from the Asset Store

107 108 110

Integrating assets Integrating terrain textures Integrating meshes Integrating textures

116 116 119 121

Table of Contents iii

Configuring assets Configuring meshes Configuring textures

123 124 126

Assembling the scene

Summary

128

131

6

Materials and Effects with URP and Shader Graph Introducing Shaders

134

Shader Pipeline Render Pipelines and URP URP Built-in Shaders

134 137 139

Creating Shaders with Shader Graph

143

Creating our first Shader Graph asset

Using Textures Combining Textures Applying transparency Summary

144

148 157 161 163

7

Visual Effects with Particle Systems and VFX Graph Introduction to particle systems 166 Creating a basic particle system Using advanced modules

167 172

Creating fluid simulations

173

Creating a waterfall effect Creating a bonfire effect

174 176

Creating complex simulations with VFX Graph

178

Installing VFX Graph Creating and analyzing a VFX Graph Creating a rain effect

179 181 185

Summary

189

8

Lighting Using the Universal Render Pipeline Applying lighting Discussing lighting methods Configuring ambient lighting with skyboxes Configuring lighting in URP

Applying shadows Understanding shadow calculations Configuring performant shadows

192 192 197 202

205 206 210

Optimizing lighting

213

Understanding static lighting 213 Baking lightmaps 214 Applying static lighting to static objects 221

Summary

224

iv Table of Contents

9

Fullscreen Effects with postprocessing Using postprocessing Setting up a profile Using basic effects

Using advanced effects

226 226 229

Advanced effects

Summary

232

242

232

10

Sound and Music Integration Importing audio Audio types Configuring import settings

Integrating and mixing audio

243 244 245

Using 2D and 3D AudioSources Using an Audio Mixer

Summary

250 254

259

250

11

User Interface Design Understanding Canvas and RectTransform

263

Creating a UI with Canvas Positioning elements with RectTransform

265

Canvas objects types

268

Integrating assets for the UI

263

Creating UI controls

Creating a responsive UI Adapting objects' positions Adapting objects' sizes

Summary

275

282 283 286

290

268

12

Creating Animations with Animator, Cinemachine, and Timeline Using skeletal animations with Animator 292 Understanding skinning Importing skeletal animations

292 295

Integration using Animation Controllers 299

Table of Contents v

Creating dynamic cameras with Cinemachine 305 Creating dolly tracks

309

Creating cutscenes with

Timeline Creating animation clips Sequencing our intro cutscene

Summary

313 313 317

322

13

Introduction to Unity Scripting with C# Creating C# scripts

324

Initial setup 324 Creating a MonoBehaviour-based class 327 Adding fields 330

Using events and instructions

Events and instructions Using fields in instructions Common beginner errors

Summary

333 337 338

341

333

14

Implementing Movement and Spawning Implementing movement Moving objects through Transform Using Input Understanding Delta Time

Implementing spawning

344 344 348 353

Spawning objects Timing actions Destroying objects

Summary

355 363 367

368

355

15

Physics Collisions and Health System Configuring Physics Setting shapes Physics Object types Filtering collisions

Detecting collisions Detecting Trigger events Modifying the other Object

370 370 373 377

380 381 383

Moving with Physics Applying forces Tweaking Physics

Summary

386 386 388

391

vi Table of Contents

16

Win and Lose Conditions Creating Object Managers Implementing the Singleton design pattern Creating Managers with Singleton

394 394 398

Creating Game Modes 402 Improving our code with events 406 Summary 413

17

Scripting the UI, Sounds, and Graphics Scripting the UI Showing information in the UI Programming the Pause menu

Scripting feedback Scripting visual feedback

416 416 423

Scripting audio feedback Scripting animations

Summary

432 434

437

428 428

18

Implementing Game AI for Building Enemies Gathering information with sensors Creating Three-Filters sensors Debugging with Gizmos

Making decisions with FSMs Creating the FSM

440 440 446

450 450

Creating transitions Executing FSM actions Calculating our scene Pathfinding Using Pathfinding

Adding final details Summary

452 456 457 459

462 467

19

Scene Performance Optimization Optimizing graphics Introduction to graphic engines Using Frame Debugger Using batching Other optimizations

470 470 472 474 477

Optimizing processing Detecting CPU- and GPU-bound Using the CPU Usage profiler General CPU optimization techniques

481 481 484 488

Table of Contents vii

Optimizing memory Memory allocation and the garbage collector

490 491

Using the Memory Profiler

Summary

495

499

20

Building the Project Building a project Debugging the build Debugging code

501 506 507

Profiling performance

510

Summary

512

Release Post-release

520 521

Summary

522

Creating a simple AR game

545

21

Finishing Touches Iterating your game Testing and feedback Interpreting feedback

Releasing your game Pre-release

513 514 516

518 518

22

Augmented Reality in Unity Using AR Foundation Creating an AR Foundation project Using tracking features

Building for mobile devices Building for Android Building for iOS

524 524 528

536

Spawning the Player and Enemies 546 Coding the Player and Enemy behavior 549

Summary

554

readers know what you think

557

536 542

Other Books You May Enjoy Leave a review - let other

Index

Preface I still remember that moment of my life when I was afraid of telling my parents that I was going to study game development. At that time and where I was, that was considered a childish desire by most parents, and a career with no future, but I was stubborn enough to not care and to follow my dream. Today, game development is one of the biggest industries, generating more revenue than even the film industry. Of course, following my dream was more difficult than I thought. Sooner or later, people chasing this particular dream have to face the fact that developing games is a difficult task that requires a deep level of knowledge in different areas. Sadly, most people give up due to this difficulty level, but I strongly believe that with proper guidance and tools, you can make your career path easier for you to follow. In my case, what helped me to flatten the learning curve was learning using Unity. Welcome to this book about Unity 2020. Here you will learn how to use the most recent Unity features to create your first video game in the simplest way possible. Unity is a tool that provides you with powerful but easy-to-use features to solve the most common problems in game development, such as rendering, animation, physics, sound, effects, and so on. We will be using all those features to create a simple but complete game, learning all the nuances needed to handle Unity. By the end of this book, you will be able to use Unity in a way that will allow you to start studying in depth that area of game development that you are interested in starting your career in, or that will simply allow you to create hobby games just for the joy of doing it. Unity is a versatile tool that can be used both in professional and amateur projects, and every day it is being used by more and more people. It is worth mentioning that Unity can be used to create not only games but any kind of interactive app, from simple mobile apps to complex training or education applications (known as serious gaming), using the latest technologies, such as augmented and virtual reality. So, even if we are creating a game here, you are starting a learning path that can end in lots of possible specializations.

Hands-On Unity 2020 Game Development Over the years, the Unity game engine has extended its scope from just being about creating video games to building AR/VR experiences, complex simulations, real-time realistic rendering, films, and serious games for training and education. Its features for implementing gameplay, graphics, and customization using C# programming make Unity a comprehensive platform for developing professional-level, rich experiences. With this book, you'll be able to build impressive Unity projects in a step-bystep manner and apply your knowledge of Unity concepts to create a real-world game. Complete with hands-on tutorials and projects, this easy-to-follow guide will show you how to develop your first complete game

using a variety of Unity tools. As you make progress, you'll learn how to make the most of the Unity Editor and create scripts using the C# programming language. This Unity game development book will then take you through integrating graphics, sound, and animations and manipulating physics to create impressive mechanics for your games. You'll also learn how to code a simple AI agent to challenge the user and use profiling tools to ensure that the code runs in a performant way. Finally, you'll get to grips with Unity's AR Foundation for creating AR experiences for 3D apps and games. By the end of this book, you'll have developed a complete game and will have built a solid foundation using Unity's tooling ecosystem to develop game projects of any scale.

Things you will learn: •

Write scripts for customizing various aspects of a game, such as physics, gameplay, and UI



Create rich particle systems for your Unity games from scratch using VFX Graph and Shuriken



Program rich shaders and effects using Unity's new Shader Graph and Universal Render Pipeline



Add animations to your game using the Animator, Cinemachine, and Timeline



Implement postprocessing to increase graphics quality with full-screen effects



Implement game artificial intelligence (AI) to control character behavior



Detect and fix optimization issues using profilers and batching

www.packt.com

FOR SALE IN INDIA ONLY