Unity 2020 Mobile Game Development Second Edition Discover practical techniques and examples to create and deliver engaging games for Android and iOS John P. Doran BIRMINGHAM - MUMBAI Unity 2020 Mobile Game Development Second Edition 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: Larissa Pinto Content Development Editor: Divya Vijayan Senior Editor: Mohammed Yusuf Imaratwale Technical Editor: Deepesh Patel Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexer: Priyanka Dhadke Production Designer: Joshua Misquitta First published: November 2017 Second edition: August 2020 Production reference: 1210820 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83898-733-6 www.packt.com To my wife, Hien, for being my loving partner throughout our joint life journey, and to my parents, who took me seriously when I said I wanted to make games for a living. – John P. Doran 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 www.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 John P. Doran is a passionate and seasoned technical game designer, software engineer, and author based in Peoria, Illinois. For over a decade, John has gained extensive hands-on expertise in game development, working in a variety of roles, ranging from game designer to lead UI programmer. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. John is currently an instructor in residence at Bradley University. Prior to his present ventures, he was an award-winning videographer. I want to thank everyone at Packt Publishing for all of their hard work, and thank you to my colleagues in the Interactive Media department at Bradley University, for all of their support over the course of working on this title. 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. 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 Chapter 1: Building Your Game 7 Technical requirements 8 Setting up the project 8 Creating the player 12 Moving the player through a C# script 15 Improving our scripts with attributes and XML comments 20 Using attributes 20 The Tooltip attribute 20 The Range attribute 21 The RequireComponent attribute 22 XML comments 23 Update versus FixedUpdate 25 Putting it all together 26 Having the camera following our player 27 Creating a basic tile 30 Making it endless 35 Creating obstacles 42 Summary 55 Chapter 2: Project Setup for Android and iOS Development 56 Technical requirements 56 An introduction to build settings 57 Building a project for PC 61 Exporting a project for Android 64 Installing Android Build Support for Unity 64 Updating build and player settings for Android projects 67 Putting the project on your Android device 72 Unity for iOS setup and Xcode installation 83 Building a project for iOS 88 Summary 96 Chapter 3: Mobile Input/Touch Controls 98 Technical requirements 99 Using mouse input 99 Moving using touch controls 104 Implementing a gesture 107 Scaling the player using pinches 112 Using the accelerometer 115 Table of Contents Detecting touch on game objects 117 Summary 126 Chapter 4: Resolution-Independent UI 127 Technical requirements 128 Creating a title screen 128 The Rect Transform component 133 Anchors 135 Pivots 137 Adjusting and resizing the title text 138 Selecting different aspect ratios 145 Working with buttons 149 Adding a pause menu 159 Pausing the game 174 Adapting GUI for notch devices 179 Summary 188 Chapter 5: Advertising Using Unity Ads 190 Technical requirements 191 Setting up Unity Ads 191 Displaying a simple ad 197 Utilizing ad callback methods 200 Opt-in advertisements with rewards 204 Adding in a cooldown timer 211 Summary 217 Chapter 6: Implementing In-App Purchases 218 Technical requirements 219 Setting up Unity IAP 219 Creating our first purchase 227 Adding a button to restore purchases 237 Configuring purchases for the stores of your choice 241 Summary 241 Further reading 242 Chapter 7: Getting Social 243 Technical requirements 244 Adding a scoring system 244 Sharing high scores via Twitter 251 Downloading and installing Facebook's SDK 256 Logging in to our game via Facebook 267 Displaying a Facebook name and profile picture 276 Summary 283 Chapter 8: Keeping Players Involved with Notifications 284 [ ii ] Table of Contents Technical requirements 284 Setting up notifications 285 Scheduling notifications ahead of time 290 Customizing notifications 294 Summary 299 Chapter 9: Using Unity Analytics 300 Technical requirements 301 Setting up Analytics 301 Tracking Custom Events 305 Sending basic CustomEvents 305 Sending Custom Events with properties 310 Working with Funnel Analyzer 317 Tweaking properties with Remote Settings 321 Summary 338 Chapter 10: Making Your Title Juicy 339 Technical requirements 340 Animation using LeanTween 340 LeanTween setup 340 Creating a simple tween 343 Adding tweens to the pause menu 349 Working with materials 352 Using post-processing effects 356 Adding particle effects 364 Summary 368 Chapter 11: Game Build and Submission 369 Technical requirements 369 Building a release copy of our game 370 Putting your game on the Google Play Store 375 Setting up the Google Play Console 375 Publishing an app on Google Play 377 Putting your game on the Apple iOS App Store 390 Apple Developer setup and the creation of a provisioning profile 390 Adding an app to iTunes Connect 406 Summary 415 Chapter 12: Augmented Reality 416 Technical requirements 416 Setting up a project for AR 417 Basic setup 419 Detecting surfaces 423 Interacting with the AR environment 426 Spawning objects in AR 431 [ iii ]