Main
Beginning iOS AR Game Development: Developing Augmented Reality Apps with Unity and C#
Beginning iOS AR Game Development: Developing Augmented Reality Apps with Unity and C#
Allan Fowler
0 /
0
How much do you like this book?
What’s the quality of the file?
Download the book for quality assessment
What’s the quality of the downloaded files?
Create a fully featured application that’s both sophisticated and engaging. This book provides a detailed guide in developing augmented reality games that can take advantage of the advanced capabilities of new iOS devices and code while also offering compatibility with still supported legacy devices.
No programming experience is necessary as this book begins on the ground floor with basic programming concepts in Unity and builds to incorporating input from the real world to create interactive realities. You’ll learn to program with the Unity 2017 development platform using C#.
Recent announcements of increased AR capabilities on the latest iPhones and iPads show a clear dedication on Apple’s part to this emerging market of immersive games and apps. Unity 2017 is the latest version of this industry leading development platform and C# is a ubiquitous programming language perfect for any programmer to begin with.
Using the latest development technologies, Beginning iOS AR Game Development will show you how to program games that interact directly with the real world environment around the user for creative fantastic augmented reality experiences.
What You'll Learn
• Download assets from the Unity store
• Create a scene in Unity 2017
• Use physics and controls on mobile devices
Who This Book Is For
Beginner programmers and/or people new to developing games using Unity. It also serves as a great introduction to developing AR games and educators teaching the subject at high school or higher levels.
No programming experience is necessary as this book begins on the ground floor with basic programming concepts in Unity and builds to incorporating input from the real world to create interactive realities. You’ll learn to program with the Unity 2017 development platform using C#.
Recent announcements of increased AR capabilities on the latest iPhones and iPads show a clear dedication on Apple’s part to this emerging market of immersive games and apps. Unity 2017 is the latest version of this industry leading development platform and C# is a ubiquitous programming language perfect for any programmer to begin with.
Using the latest development technologies, Beginning iOS AR Game Development will show you how to program games that interact directly with the real world environment around the user for creative fantastic augmented reality experiences.
What You'll Learn
• Download assets from the Unity store
• Create a scene in Unity 2017
• Use physics and controls on mobile devices
Who This Book Is For
Beginner programmers and/or people new to developing games using Unity. It also serves as a great introduction to developing AR games and educators teaching the subject at high school or higher levels.
Categories:
Year:
2019
Edition:
1
Publisher:
Apress
Language:
english
Pages:
244 / 252
ISBN 10:
1484236173
ISBN 13:
9781484236178
File:
PDF, 11.90 MB
Your tags:
The file will be sent to your email address. It may take up to 1-5 minutes before you receive it.
The file will be sent to your Kindle account. It may takes up to 1-5 minutes before you received it.
Please note: you need to verify every book you want to send to your Kindle. Check your mailbox for the verification email from Amazon Kindle.
Please note: you need to verify every book you want to send to your Kindle. Check your mailbox for the verification email from Amazon Kindle.
Conversion to is in progress
Conversion to is failed
You may be interested in Powered by Rec2Me
Most frequently terms
unity464
select225
gameobject203
camera163
script119
menu114
arkit108
folder100
settings90
player84
asset80
assets76
ios73
lighting67
unity ar59
click58
app51
tab50
prefab48
layout47
cube41
bowling40
xcode39
collider34
planes29
mesh28
https26
fowler26
console26
mouse24
download24
video22
joystick22
rename21
layouts21
Related Booklists
0 comments
You can write a book review and share your experiences. Other readers will always be interested in your opinion of the books you've read. Whether you've loved the book or not, if you give your honest and detailed thoughts then people will find new books that are right for them.
1
|
2
|
Beginning iOS AR Game Development Developing Augmented Reality Apps with Unity and C# — Allan Fowler Beginning iOS AR Game Development Developing Augmented Reality Apps with Unity and C# Allan Fowler Beginning iOS AR Game Development: Developing Augmented Reality Apps with Unity and C# Allan Fowler Marietta, GA, USA ISBN-13 (pbk): 978-1-4842-3617-8 https://doi.org/10.1007/978-1-4842-3618-5 ISBN-13 (electronic): 978-1-4842-3618-5 Library of Congress Control Number: 2018964038 Copyright © 2019 by Allan Fowler This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aaron Black Development Editor: James Markham Coordinating Editor: Jessica Vakili; Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail rights@apress.com, or visit http://www.apress. com/rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/978-1-4842-3617-8. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper For Hao, Ciaran, & Annah - kia kaha, kia aroha, kia mana Table of Contents About the Author���������������������������������������������������������������������������������xi About the Technical Reviewer�����������������������������������������������������������xiii Chapter 1: Introduction������������������������������������������������������������������������1 Unity3D�����������������������������������������������������������������������������������������������������������������2 Unity Requirements�����������������������������������������������������������������������������������������������3 Preparing Your Mac�����������������������������������������������������������������������������������������3 Getting Registered�������������������������������������������������������������������������������������������4 Download Xcode����������������������������������������������������������������������������������������������4 Download Unity�����������������������������������������������������������������������������������������������4 Install Unity������������������������������������������������������������������������������������������������������5 Welcome����������������������������������������������������������������������������������������������������������7 Setting Up Unity�����������������������������������������������������������������������������������������������9 iOS Development Requirements��������������������������������������������������������������������13 The Unity Website������������������������������������������������������������������������������������������13 The Unity Community������������������������������������������������������������������������������������13 Chapter 2: Getting Started������������������������������������������������������������������15 Installing Unity ARKit�������������������������������������������������������������������������������������������19 The Editor Layout������������������������������������������������������������������������������������������������21 Preset Layouts�����������������������������������������������������������������������������������������������23 Custom Layouts���������������������������������������������������������������������������������������������26 The Inspector View����������������������������������������������������������������������������������������������30 v Table of Contents The Project View�������������������������������������������������������������������������������������������������34 Switch Between One Column and Two Columns�������������������������������������������35 Scale Icons����������������������������������������������������������������������������������������������������35 Inspect Assets�����������������������������������������������������������������������������������������������36 Search for Assets������������������������������������������������������������������������������������������36 Operate on Assets�����������������������������������������������������������������������������������������38 The Hierarchy View���������������������������������������������������������������������������������������������39 Inspect Game Objects������������������������������������������������������������������������������������40 Parent and Child GameObjects����������������������������������������������������������������������41 The Scene View���������������������������������������������������������������������������������������������������41 Navigate the Scene���������������������������������������������������������������������������������������41 Scene View Options���������������������������������������������������������������������������������������46 Scene View Gizmos���������������������������������������������������������������������������������������48 The Game View���������������������������������������������������������������������������������������������������49 Maximize on Play������������������������������������������������������������������������������������������50 Stats��������������������������������������������������������������������������������������������������������������51 Game View Gizmos����������������������������������������������������������������������������������������52 The Console View������������������������������������������������������������������������������������������������53 Explore Further���������������������������������������������������������������������������������������������������54 Unity Manual��������������������������������������������������������������������������������������������������55 Tutorials���������������������������������������������������������������������������������������������������������55 Version Control����������������������������������������������������������������������������������������������55 Chapter 3: The Unity ARKit�����������������������������������������������������������������57 Creating a Scene�������������������������������������������������������������������������������������������������57 Cameras��������������������������������������������������������������������������������������������������������������62 Transformation����������������������������������������������������������������������������������������������������64 Testing�����������������������������������������������������������������������������������������������������������70 vi Table of Contents ARKit Remote������������������������������������������������������������������������������������������������������71 Setting Up the Main Camera�������������������������������������������������������������������������������72 Adding a Component�������������������������������������������������������������������������������������������72 Tracking the Phone Movement����������������������������������������������������������������������76 Build and Run������������������������������������������������������������������������������������������������78 Saving the Scene�������������������������������������������������������������������������������������������84 Understanding Scenes����������������������������������������������������������������������������������85 Introducing Visual Inertial Odometry�������������������������������������������������������������������86 Feature Points�����������������������������������������������������������������������������������������������86 Point Clouds���������������������������������������������������������������������������������������������������87 Testing�����������������������������������������������������������������������������������������������������������89 Unity ARKitRemote�����������������������������������������������������������������������������������������90 Using ARKit Remote Connection��������������������������������������������������������������������95 Plane Visualization�����������������������������������������������������������������������������������������99 Testing���������������������������������������������������������������������������������������������������������101 Summary�����������������������������������������������������������������������������������������������������102 Chapter 4: Hit Testing and Lighting��������������������������������������������������103 Hit Testing���������������������������������������������������������������������������������������������������������103 Scale������������������������������������������������������������������������������������������������������������106 Transformation��������������������������������������������������������������������������������������������107 The Editor Hit Test Script�����������������������������������������������������������������������������109 Lighting��������������������������������������������������������������������������������������������������������121 Positioning the Camera�������������������������������������������������������������������������������128 Summary�����������������������������������������������������������������������������������������������������130 vii Table of Contents Chapter 5: Making AR Games�����������������������������������������������������������131 Fugu BowlAR Game������������������������������������������������������������������������������������������131 Creating a New Scene���������������������������������������������������������������������������������������132 Creating AR Assets��������������������������������������������������������������������������������������134 Transform the BowlingBall��������������������������������������������������������������������������137 Adding a Rigidbody��������������������������������������������������������������������������������������137 Opening the Asset Store������������������������������������������������������������������������������141 PhysicsMaterials�����������������������������������������������������������������������������������������145 Creating a (Temporary) Plane����������������������������������������������������������������������147 Importing an OBJ file to Unity����������������������������������������������������������������������150 Creating a new Project Folder���������������������������������������������������������������������151 Adding the Bowling_Pin to the Scene���������������������������������������������������������153 Transform Settings for the Bowling_Pin and BowlingBall���������������������������154 Adding a RigidBody to the Bowling_Pin������������������������������������������������������154 Adding a Collider to the Bowling_Pin����������������������������������������������������������155 Adding a PhysicsMaterial to the Bowling_Pin���������������������������������������������158 Making the Bowling_Ball Roll����������������������������������������������������������������������160 Editing the PlayerController Script��������������������������������������������������������������163 Creating Our Own Collider���������������������������������������������������������������������������164 Remembering the Parents���������������������������������������������������������������������������169 Making a Parent������������������������������������������������������������������������������������������169 Making the Bowling_Pin fall Over���������������������������������������������������������������170 Summary����������������������������������������������������������������������������������������������������������171 Chapter 6: Introducing Touch�����������������������������������������������������������173 Testing the Game in AR�������������������������������������������������������������������������������������173 Some Challenges�����������������������������������������������������������������������������������������178 Disabling the Plane(t)����������������������������������������������������������������������������������178 viii Table of Contents Testing���������������������������������������������������������������������������������������������������������178 Implementing Touch Controls����������������������������������������������������������������������180 Importing the Unity CrossPlatformInput Package����������������������������������������180 Adding Touch Controls���������������������������������������������������������������������������������182 Download the Joystick Asset Pack��������������������������������������������������������������182 Creating a Canvas����������������������������������������������������������������������������������������184 Adding the Joystick UI to the Canvas����������������������������������������������������������185 Adding a Script��������������������������������������������������������������������������������������������186 Testing���������������������������������������������������������������������������������������������������������189 On Device Testing����������������������������������������������������������������������������������������190 Success�������������������������������������������������������������������������������������������������������192 Chapter 7: Adding Plane Detection and Point Clouds�����������������������193 Creating the Generated Planes GameObject�����������������������������������������������������193 Creating the Point Cloud GameObject���������������������������������������������������������197 Setting Up the Main Camera������������������������������������������������������������������������200 Setting the Unity AR Video Script Clear Materials���������������������������������������203 Lighting��������������������������������������������������������������������������������������������������������216 Chapter 8: Final Steps����������������������������������������������������������������������223 Creating Prefabs�����������������������������������������������������������������������������������������������223 Adding More Bowling Pins���������������������������������������������������������������������������226 Instantiating GameObjects at Runtime��������������������������������������������������������228 Creating an Instantiate_GameObjects Script�����������������������������������������������229 Disabling a GameObject from the Scene�����������������������������������������������������232 Creating a Game Manager���������������������������������������������������������������������������233 FindWithTag�������������������������������������������������������������������������������������������������234 Testing���������������������������������������������������������������������������������������������������������235 Index�������������������������������������������������������������������������������������������������239 ix About the Author Dr. Allan Fowler is a Professor in Game Design at Kennesaw State University. Fowler lives in Atlanta, GA. Fowler is a published author, game designer, and dedicated educator. Apart from looking after his two children, in his spare time he makes games and practices martial arts and is a keen amateur photographer. Dr. Fowler holds a fifth degree black belt in Shorin-Ryu karate and has competed in State and International karate tournaments. xi About the Technical Reviewer Felipe Laso is a Senior Systems Engineer working at Lextech Global Services. He’s also an aspiring game designer/programmer. You can follow him on Twitter as @iFeliLM or on his blog. xiii CHAPTER 1 Introduction In this book, we are going to learn how to create an Augmented Reality (or AR) Game using the game development software from Unity (Unity3D 2018 or, more commonly, Unity). In this chapter, we will go through the process of downloading and installing Unity and learn about some of the tools that Unity provides. We will also install an existing AR project from the Unity Asset Store and explore some of the features of this game. In Chapter 2, we will install the ARKit and provide an overview of the Unity user interface. In Chapter 3, we will start using the Unity ARKit and use some of the key functions. I will also provide a basic overview of visual inertial odometry and what this means for creating AR projects. We will make a basic scene to use and test Unity ARKit. In Chapter 4, we will use some of the more advanced functions in Unity ARKit such as hit testing and lighting an AR scene. Finally, in Chapter 5, we will put this all together and make an AR game using the Unity ARKit. This book has been written for a beginner that has no prior experience using Unity or making games. The chapters have been prepared in a sequence to help learn each step. However, if you are reading this book and already know about Unity or making games, then feel free to skip whatever chapters you feel you already know. © Allan Fowler 2019 A. Fowler, Beginning iOS AR Game Development, https://doi.org/10.1007/978-1-4842-3618-5_1 1 Chapter 1 Introduction Unity3D The Unity game engine is a cross-platform game development tool for creating both 2D and 3D games. The term cross-platform can mean different things to different people. As Unity can be used on macOS, Windows, or Linux, this could be considered a cross-platform development tool. However, as Unity can be used to develop games for a games console, personal computer, web browser, mobile devices, VR systems, etc., this could also be why Unity is considered a cross-platform development tool. Unity can be used to create 3D games, that is, the game looks like it operates in a 3D space (it has an X, Y, and a Z). Unity can also be used to create 2D games. More recently, Unity has been used to create VR and AR games or simulations. In this book, we will use the latest version of Unity, which is currently 2018.1. However, like most software (and a lot of hardware), Unity is constantly introducing new features, functionality, and by the time this book has been printed, there may be a more current version of Unity available. When Unity makes a minor change to their software, they will typically add a number (like 2018.1.1). When the update is a bit more substantial, then the version number will change (like 2018.2). When Unity typically make a major change, then the version number will change completely (Unity 1, 2, 3, 4, 5). In July 2017, Unity changed the version numbering system to the year of release (2017 and now 2018). 2 Chapter 1 Introduction Unity Requirements Before you start learning to make games, you will need to download Unity and install it on your Mac. Although it is possible to make games for iOS devices with Unity installed on a Windows Personal Computer, you will need to use a piece of software called Xcode to port the Unity code so it can run on a Mac or iOS device. Currently, Xcode is only available on a Mac. So, if you have a Windows PC, then at some stage you will need to use a Mac to port the game. Throughout this book, I will use a Mac; if you are using a Windows PC, then many of the instructions or directions may not apply to you. Preparing Your Mac For iOS development using Unity, you will need a Mac running the Lion or Mountain Lion of Mac OS X 10.9 or higher and Xcode 7.0 or higher. Unity 2018 may still run on some older systems, but you will need the latest version of Xcode, which, as noted, is required for iOS development. The latest version of Xcode typically supports the more recent versions of iOS. At the time of writing, the current version of Xcode is version 9, which is what I’ll be using throughout this book. 3 Chapter 1 Introduction G etting Registered I highly recommend checking out the Apple Developer website (https:// developer.apple.com/) and registering as an iOS developer. Although it is not an absolute requirement of this book, if you want to publish games on the App Store, then you will need to be a registered Apple Developer. The process of registering as an Apple Developer can take a while, especially if you are registering a company. The first step is registering as an Apple Developer (which is currently free), then once you are registered, the next step is registering as an iOS developer (which is currently $99 per year). D ownload Xcode You won’t need Xcode until much later in the book, but it would be worth downloading and installing Xcode. You can find the latest version of Xcode on the Apple Developer website (https://developer.apple.com/). D ownload Unity Now would be a great time to install Unity. Go to the Unity website at https://unity3d.com and then select Get Unity or type in https:// store.unity.com/. On this page, you will find the latest release of Unity (at the time of writing, 2018.1). You can also find previous releases of Unity on the Unity website. While there is only a single Unity application, you can subscribe to different licensing options, depending on your needs and the size of your company (if you have one). The three licensing options are currently Personal, Plus, and Pro. To start the download process, click on the button of the subscription option that suits your needs (at the time of writing, this will be either Try Personal, Get Plus, or Go Pro). The file is about 1GB, so it may take a while to download. While you’re waiting, and you are on the 4 Chapter 1 Introduction Unity website, take some time to check out some of the games and demos that have been published, the community site, and the user forum. These will be very useful throughout the development of games using Unity. I nstall Unity The file you downloaded from Unity is a Download Installer, which at the time of writing is named UnityDownloadAssistant. Running the Download Assistant When the UnityDownloadAssistant file has been downloaded, double- click the file to run the Unity Download Assistant. Double-click the Unity Download Assistant icon to start installing Unity (Figure 1-1). Figure 1-1. The Unity Download Assistant 5 Chapter 1 Introduction The installer will process through the installation cycle, and when this is complete, a Unity folder will be placed in the Applications folder (unless you selected a different location). If you have previous versions of Unity installed, an installation of a new version may replace the previous version. I recommend renaming the folder of the previous version before installing the new version (for example, Unity2017). This way you can still use both versions of Unity. The Unity installation folder contains the Unity application and several support folders and applications (Figure 1-2). Figure 1-2. The Unity installation folder One of the most important files in the Unity folder is the Unity application, which will provide the tools used to create and test your games. This application is sometimes referred to as the Unity Editor, 6 Chapter 1 Introduction which is different from another application, the Unity Runtime Engine (also known as the Unity Player). The Unity Runtime Engine is integrated into the final builds, which will enable the game to be played on the target hardware. When I refer to Unity, I am usually referring to the Unity Editor. I sometimes will refer to the company Unity technologies as Unity. But hopefully, the context will be clear. The Documentation folder contains the User Manual, Component Reference, and the Script Reference documents. These are also available on the Unity website (select the Learn link). All of these files are HTML documents and can be opened in a web browser from the Unity Help menu system, or they can be opened directly by double-clicking the file. The Standard Assets folder contains several files with the .unityPackage file extension. These are Unity package files that contain collections of Unity assets, which can be imported into Unity. It is also possible to create your own Standard Assets and export these assets to a package file. There is also the Unity Bug Reporter Application. This application is typically run directly from within the Unity Editor using the Report a Bug function. However, this application run directly from the Unity installation folder. If you downloaded the Example Project with the Unity installation, be sure to open this in Unity. If you did not download this at installation, it still could be downloaded at any time. W elcome After Unity has finished installing (and be prepared for it to take a while), the Unity editor welcome screen will appear with the Unity Hello! Window (Figure 1-3). The Unity Hello! Window is where you sign into your Unity account (if you have one). If you don’t have a Unity account, select the create one link. If you are not currently connected to the Internet, you can work offline by selecting the Work offline button. 7 Chapter 1 Introduction Figure 1-3. The Unity Hello! screen The Unity Hello! window will appear when you start up Unity (Figure 1-3). I highly recommend creating a Unity account, if you haven’t already created one. After signing in for the first time, you will see the License management screen. If you have paid for the licensed version of Unity, enter your license serial number in the dialog box. If you want to use the free version of Unity, select the Unity Personal radio button (Figure 1-4). 8 Chapter 1 Introduction Figure 1-4. The Unity license management screen S etting Up Unity Before we get into making a game with Unity, this is a good time to review some of the options and administrative features of Unity. Changing Skins (Pro Version) If you have purchased a Pro license of Unity, you will be able to choose between a light or dark skin. If you are using the free version of Unity, you will only see the Light Skin. As most beginner game developers use the free version of Unity, I will use the light skin for screenshots. The light skin also produces better screenshots for the paperback version of this book. If you have the Pro version and want to change the skin, select Preferences in the Unity menu (Figure 1-5). 9 Chapter 1 Introduction Figure 1-5. The Unity Preferences menu With the Preferences menu open, you can change the skin from Dark to Light or Light to Dark (Figure 1-6). If you are using Unity Personal edition, you’re stuck with the Light skin. Figure 1-6. The General Preferences menu in the Unity Editor 10 Chapter 1 Introduction While you have the General Preferences menu open, I would recommend making sure the Load Previous Project on Startup option is deselected. This will ensure that Unity loads the project selection dialog at startup. This will make sure you avoid updating the wrong version of the project or update the version of Unity you are using before you are ready. R eporting Problems If you continue to use Unity for several years, you will encounter some bugs (both real and imagined). I have been using Unity since version 1.6 and have encountered several bugs with Unity. Software bugs are not unique to Unity. A game development engine is a complex piece of software and Unity certainly appreciates and values bug reports. If the bugs aren’t reported, then it’s difficult for Unity to fix them. The Unity Bug Reporter application provides this feature. As noted earlier, the Report a Bug Reporter is available in the Unity installation folder and is available from the Help menu in the Unity Editor (Figure 1-7). Figure 1-7. The Report a Bug option in the Help menu 11 Chapter 1 Introduction Selecting the Report, a Bug option on the help menu or double- clicking the Report a Bug application in the Unity installation folder opens the Unity Report a Bug application (Figure 1-8). The application provides menu options and dialog boxes for the user to specify what the problem is related to, how often the problem happens, the title of the bug, the details of the bug, and an option to attach any relevant files that will help fix the bug. The Unity Bug Reporter requires the user to specify an email address so that the team at Unity can respond to the bug report. Figure 1-8. The Unity Bug Reporter window 12 Chapter 1 Introduction iOS Development Requirements Earlier in this chapter, I suggested that it would be a good idea to download Xcode and register for the Apple Developer Program. If you have not down this already, now would be a good time to stop and do this. The hardware and software requirements for iOS development and details about the Apple Developer Program are listed on Apple’s Developer support page (https://developer.apple.com/). You can also find the requirements and download page for Xcode at https://developer. apple.com/Xcode/. The Unity Website As Unity Technologies has increased the features and functionally of Unity, so has the also increased in breadth and depth of content. There is a lot of information on this website, but I would recommend looking at the FAQ section (https://unity3d.com/unity/faq). There are also some great tutorials, documentation, and videos that will help you learn to create games in Unity (https://unity3d.com/unity/faq). The Unity Community In the Help menu, there are links to the official Unity community sites. This includes the official Unity Forum (https://forum.unity.com), which is moderated by Unity staff. This is a great resource for any game developer. Also on this menu is a link to Unity Answers, which uses a Stack Exchange format and includes some control (or moderation) of the questions and answers. The Unity feedback site (https://feedback.unity3d.com) enables developers to request and vote on possible future features. Welcome to Unity. 13 CHAPTER 2 Getting Started Hopefully, you might already have a good idea of what Augmented Reality (or AR) is. As there is some confusion about the difference between the terms Virtual Reality (or VR), Augmented Reality, and Mixed Reality, I thought it might be worth trying to clarify what I mean by AR in this book. Virtual Reality (VR) is a computer-generated environment that simulates experience through senses and perception. Unlike a traditional computer system, VR systems place the user inside an experience. Instead of viewing a screen in front of them, users are immersed and able to interact with 3D worlds. Now, let’s look at Augmented Reality (AR). The general consensus is that AR is defined as a direct live view of a real-world environment whose elements are “augmented” by computer-generated information. The key difference between VR and AR is that AR includes a live view of the real environment. VR systems typically do not include a live view of the real environment. The VR headsets are fully enclosed, and the display is fully computer generated. Mixed Reality (MR, although the acronym is rarely used), is a term that has mostly been used by Microsoft to differentiate their HoloLens. I feel that Mixed Reality is another form of AR. However, there is still some debate about this. When I asked my good friends at Microsoft, they felt that Mixed Reality is somewhere between AR and VR and fully integrates digital objects into your world, making it look as if they are really there. © Allan Fowler 2019 A. Fowler, Beginning iOS AR Game Development, https://doi.org/10.1007/978-1-4842-3618-5_2 15 Chapter 2 Getting Started It might surprise you that the terms VR and AR have been around for many years. While there is considerable debate about the first use of the term VR (mostly due to an agreed definition on the term), there is some general consensus that the term was first used around the 1950s where authors referred to fully immersive systems or environments. Throughout the U.S. Military and NASA, aircraft manufacturers used AR systems for training, research, and development. However, it was not until 2016 that we saw the first commercially available consumer AR system, the Oculus Rift. The Occulus Rift was manufactured by Oculus VR (which was eventually bought out by Facebook for $2,000,000,000). References to AR have also been made for many years. The difficulty with agreeing as to then the first reference to AR was made is also due to an agreed definition of what is AR. In 1989, George Douglas wrote about a computer-driven astronomical telescope guidance and control system with a superimposed star field and celestial coordinate graphics display that appears to be the first AR system. More recently, there have been several interesting developments in AR. Although there were some proprietary systems, the first most notable development was the introduction of Pokémon Go for Android and iOS phones. Using the geolocation capabilities and the integrated camera of the phone, users were able to see a virtual object appear on the screen to be in the real world. In this book, I am going to focus on developing an AR game for iOS. I have chosen iOS for a number of reasons. First, there are a lot of devices that use iOS. More importantly, Apple is investing in AR development in both hardware and software. With iOS 11, Apple included the ARKit. ARKit makes it easy for us to create AR games and simulations that put virtual objects into the user’s environment. Through combining information from the devices’ motion sensors with data from its cameras, ARKit can help an iPhone or iPad analyze the surroundings. Apple also beefed up the capability of the cameras in both the iPhone 8 and iPhone X. The cameras 16 Chapter 2 Getting Started in the iPhone 8 and iPhone X have been designed for shooting in low light and at 60-fps video. The dual optical image stabilization on iPhone X and improved optical image stabilization on the iPhone 8 also provide improved visual clarity. These hardware and software features help make the game appear more naturally in the user’s environment. Apple is also investing heavily in improving the AR capabilities of their future iPhones and iPads. Unity recently introduced the Unity ARKit in the Unity Store. This makes it a lot easier for us to make AR games for iOS. Therefore, throughout this book, I will be using the Unity ARKit. So now would be a good time to install the latest version of the Unity ARKit. The minimum requirements for the Unity ARKit are the following: iOS device that supports ARKit and has the latest version of iOS 11.3 or higher. Mac with macOS 10.13 (High Sierra) or higher. Unity version 2017.1 or higher. The latest version of XCode 9.3 (or higher) from the Apple Developer website (requires macOS 10.13). Now that you have Unity installed, it’s time to get acquainted with the various elements of the Unity ARKit that we will use to make and test our game. While we explore the different components of the Unity ARKit, I will discuss some of the technical principles of developing AR games. As this is an introductory book, I won’t go too deep into the technical details and try to keep this relatively high level. My editor is asking me for recommendations on additional AR books, so I might keep the more advanced or technical content for the second book on AR. Now let’s start Unity and create a new project. When you start Unity, you will see the Projects screen (Figure 2-1). 17 Chapter 2 Getting Started Figure 2-1. The Unity Projects Screen Select the New icon on the top right of the screen. This will open the Unity New Project screen (Figure 2-2). In the dialog boxes, type the Project name in, set the location of the Unity file, type in the name of your organization, and set the Template to 3D. Figure 2-2. The Unity New Project Screen 18 Chapter 2 Getting Started In the dialog boxes of the Unity New Project screen, enter in the name of the project, select where you want to save this file, and select the Create project icon (Figure 2-2). I have chosen AR project as the file name and selected the Users folder on the hard drive. Now Unity will open with a blank Unity project (Figure 2-3). Figure 2-3. A blank Unity projects screen Installing Unity ARKit Now would be a great time to install the Unity ARKit. To install the Unity ARKit from the Unity Store, you will need to access the Unity Asset Store. The Unity Asset Store can be accessed a number of ways. In the main window of the Unity screen, there is an Asset Store tab, and this will bring up the Asset Store Window. This window can also be accessed by using the Command button and 9 (⌘+9). To download resources from the Asset Store, you will need to register with Unity to create a Unity ID. When you have created a Unity ID, in the Unity Asset Store window, there is a search bar. Type in the search bar ARKit, and this will bring up a list of files that 19 Chapter 2 Getting Started meet this search criterion (Figure 2-4). The default window settings in Unity will show the Unity Store window minimized; to view the Asset Store in Full-Screen mode, there is a drop-down menu on the top-right side of the screen. Select this and click with the left mouse button. This will show the screen options, Reload, Maximize, Close Tab, and Add Tab. Select the maximize option (click the left mouse button). At the top of the screen, there are several filter options. Below the filter options, there will be the assets that meet your search criteria. Double-click the ARKit, and this will load the screen for this asset. Select the import button, and this Asset will be imported into Unity (Figure 2-5). Figure 2-4. The Unity Asset Store 20 Chapter 2 Getting Started Figure 2-5. Unity Project Folder with the ARKit in the scene folder The Editor Layout Now would be a good time to take a closer look at the layout of the Unity Editor. The main window is divided into panels. The default displayed view (factory Settings) for an area is selected by clicking the view’s tab. Views can be added, moved, removed, and resized, and the Editor supports switching among layouts, so a layout essentially is a specific arrangement of views. For example, the default layout of the main window (Figure 2-6) has an area containing a Scene View (Figure 2-7) and a Game View (Figure 2-8). 21 Chapter 2 Getting Started Figure 2-6. The default layout of the Unity Editor Figure 2-7. The Scene View selected in a multitabbed area 22 Chapter 2 Getting Started Figure 2-8. The Game View selected in a multitabbed area P reset Layouts The default layout is just one of several preset layouts. Alternate layouts can be selected from the menu in the top-right corner of the main window (Figure 2-9). Unity also enables us to create our own layout. In Figure 2-9, you will see that my menu has a Mobile Game Config. This is my custom Layout that I created for when I create Mobile Games. Check out the various Layouts on your menu. Figures 2-6 through 2-7 show the resulting layouts. 23 Chapter 2 Getting Started Figure 2-9. The Layout menu I’ll describe the individual types of views in more detail shortly, but for now, note that the 2-by-3 layout (Figure 2-10) is an example of a layout where the Scene View and Game View are in separate areas instead of sharing the same one. The 4-split layout (Figure 2-11) has four instances of the Scene View, demonstrating that a layout is not restricted to one of each type of view. The Tall Layout (Figure 2-12) provides a Portrait Scene View. The Wide Layout (Figure 2-13) provides a landscape Scene View. Figure 2-10. The 2-by-3 layout 24 Chapter 2 Getting Started Figure 2-11. The 4-split layout Figure 2-12. The Tall layout 25 Chapter 2 Getting Started Figure 2-13. The Wide layout C ustom Layouts The preset layouts provide a variety of workspaces, but fortunately, you’re not restricted to using them exactly as they are. Unity provides the flexibility to completely rearrange the Editor window as you like. R esize Areas For starters, you may notice while trying out the various preset layouts that some of the areas are too narrow, for example, in the left panel of the Wide layout (Figure 2-13). Fortunately, you can click the border of an area and drag it to resize the area. M ove Views Even cooler, you can move views around. Dragging the tab of a view into another tab region will move the view there. And dragging the tab into a “docking” area will create a new area. For example, start with the Default 26 Chapter 2 Getting Started layout, and drag the Inspector tab to the right of the Hierarchy tab. Now the Inspector View shares the same area as the Hierarchy View. The result should look like Figure 2-14. Figure 2-14. Workspace customized with views moved D etach Views You can even drag a view outside the Editor window so that it resides in its own “floating” window, which can be treated just like any other area. Drag the Scene tab outside the Editor, so it resides in a floating window, and then drag the Game tab into its tab region. The result should look like Figure 2-15. Likewise, dragging a tab into a docking region of the floating window will add another area to the window. Tip I like to detach the Game View into a floating window, since I normally don’t need to see it while I’m working in the Editor until I click Play, and this allows me to maximize the Game View to fill up to the entire screen. I also like working with more than one monitor. This way, I can maximize my screen space. 27 Chapter 2 Getting Started Figure 2-15. The list of new views Floating windows are often covered up by other windows, so the Windows menu on the menu bar has menu items for making each view visible. Notice there is a keyboard shortcut for each, and there is also a Layouts submenu that is identical to the layout menu inside the Editor. Add and Remove Views You can also add and remove views in each area using the menu at the top-right corner of the area (Figure 2-15). The Close Tab item removes the currently displayed view. The Add Tab item provides a list of new views for you to choose from. You may want to have different layouts for different target platforms, or different layouts for development vs. playtesting, or even different layouts for different games. For example, I have a custom layout specifically for my mobile games that pre-saves the Game View in a suitable portrait aspect ratio. It would be a hassle to manually reconfigure the Editor every time you start up Unity. Fortunately, you can name and save layouts by selecting the Save Layout option in the layout menu, which will prompt you for the new layout name (Figure 2-16). 28 Chapter 2 Getting Started Figure 2-16. Prompt for new layout After saving, the new layout will be listed in the layout menu and also in the list of layouts available for deletion if you select Delete Layout (Figure 2-17). Figure 2-17. Deletion menu for layouts If you’ve messed up or deleted the original layouts, you can select the Restore Factory Settings option in the area menu (Figure 2-18). This will also delete any custom layouts. 29 Chapter 2 Getting Started Figure 2-18. Restore original layout settings If you change a layout and haven’t saved the changes, you can always discard them by just reselecting that layout in the layout menu. The Inspector View The best view to describe in detail first is the Inspector View since its function is to display information about objects selected in other views. It’s really more than an inspector since it can typically be used to modify the selected item. The Inspector View is also used to display and adjust the various settings that can be brought up in the Edit menu (Figure 2-19). 30 Chapter 2 Getting Started Figure 2-19. Bringing up the Editor Settings The Inspector View displays the Editor Settings. If the project currently has metafiles, then the Version Control Mode is set to Meta Files (and if you’re using the Asset Server, this option is set to Asset Server). To hide the metafiles, set the Version Control Mode to Hidden (Figure 2-20). 31 Chapter 2 Getting Started Figure 2-20. Editor Settings in the Inspector View 32 Chapter 2 Getting Started With the Version Control Mode set to Disabled, Unity will remove the metafiles. The asset tracking is now handled within binary files inside the Library folder of the project. Note Unity users who are using metafiles for version control support also have the option of setting Asset Serialization Mode to Force Text. In that mode, Unity scene files are saved in a text-only YAML (YAML Ain’t Markup Language) format. Normally, the Inspector View displays the properties of the most recently selected object (when you bring up the Editor Settings, you really selected it). But sometimes you don’t want the Inspector View to change while you’re selecting other objects. In that case, you can pin the Inspector View to an object by selecting the Lock option in the menu at the top right of the view (Figure 2-21). 33 Chapter 2 Getting Started Figure 2-21. Locking the Inspector View The Project View While the Inspector View can be thought of as the lowest-level View in the Editor, since it displays the properties of just a single object, the Project View can be considered the highest-level view (Figure 2-22). The Project View displays all of the assets available for your game, ranging from individual models, textures, and scripts to the scene files that incorporate those assets. All of the project assets are files residing in the Assets folder of your project (so you might want to think of the Project View as the Assets View). 34 Chapter 2 Getting Started Figure 2-22. Top level of the Project view Switch Between One Column and Two Columns In several older versions of Unity, the Project View had only a one-column display. That option is still available in the menu for the Project View (click the little three-line icon at the top right of the view), so you can now switch between one and two columns. Scale Icons The slider on the bottom scales the view in the right panel—a larger scale is nice for textures, and smaller is better for items like scripts that don’t have interesting icons. This is a good reason to partition assets by asset type (i.e., put all textures in a Textures folder, scripts in a Script folder, and so on). Chances are, a single-scale slider setting won’t be good for a mixture of asset types. 35 Chapter 2 Getting Started I nspect Assets Selecting an asset on the right will display the properties of that asset in the Inspector View. For example, if you select an animation sample, the Inspector View displays information about the animation, some of which you can change, like the duration, and it even lets you play the animation in the Editor (Figure 2-23). We will look at changing asset properties in a later chapter, but for now feel free to select various types of assets in the Project View and see what shows up in the Inspector View. Figure 2-23. Inspecting a selected asset in the Project View S earch for Assets In a large and complex project, it’s difficult to manually search for a particular asset. Fortunately, just as in the Finder, there is a search box that can be used to filter the results showing in the right panel of the Project view. In Figure 2-24, the Project View displays the result of searching for assets with “add” in their names. 36 Chapter 2 Getting Started Figure 2-24. Searching for assets with “add” in the name The right panel displays the search results for everything under Assets (i.e., all of our assets). The search can be narrowed further by selecting one of the subfolders in the left panel. For example, if you know you’re looking for a scene, and you’ve arranged your assets into subfolders by the type of asset, you can select the folder to search. In Figure 2-25, I have searched the examples folder for any asset with add in the file name. Figure 2-25. Searching assets in a specific folder 37 Chapter 2 Getting Started Notice just below the search; there is a tab with the name of the folder that was selected. You can still click the Assets tab to the left to see the search results for all your assets, both locally and on the Unity Asset Store, which we’ll make numerous uses of in this book. You can also filter your search by asset type, using the menu immediately to the right of the search box. Instead of just searching in the Examples folder, you could have selected scenes as the asset type of interest (Figure 2-26). Notice how that resulted in being added to the search box. The t: prefix indicates the search should be filtered by the following asset type. You could have just typed that in without using the menu. Figure 2-26. Search filtered by asset type The button to the right of the asset type menu is for filtering by label (you can assign a label to each asset in the Inspector View), which is also pretty handy for searching the Asset Store. And the rightmost button, the star, will save the current search in the Favorites section of the left panel. O perate on Assets Assets in the Project View can be manipulated very much like their corresponding files in the Finder. 38 Chapter 2 Getting Started Double-clicking an asset will attempt to open a suitable program to view or edit the asset. This is equivalent to right-clicking the asset and selecting Open. Double-clicking a scene file will open the scene in this Unity Editor window, just as if you had selected Open Scene in the File menu. You can also rename, duplicate and delete, and drag files in and out of a folder just as you can in the Finder. Some of the operations are available in the Unity Edit menu and in a pop-up menu when you right-click on an asset. You’ll get some practice with that in the next few chapters. Likewise, in the next chapter, you will work on adding assets to a project. That involves importing a file or importing a Unity package, using the Assets menu on the menu bar or just dragging files into the Assets folder of the project using the Finder. The Hierarchy View Every game engine has a top-level object called a game object or entity to represent anything that has a position, potential behavior, and a name to identify it. Unity game objects are instances of the class GameObject. Note In general, when we refer to a type of Unity object, we’ll use its class name to be precise and make clear how that object would be referenced in a script. The Hierarchy View is another representation of the current scene. While the Scene View is a 3D representation of the scene that you can work in as you would with a content creation tool, and the Game View shows the scene as it looks when playing the game, the Hierarchy View lists all the GameObjects in the scene in an easily navigable tree structure. 39 Chapter 2 Getting Started Inspect Game Objects When you click a GameObject in the Hierarchy View, it becomes the current Editor selection, and its components are displayed in the Editor. Every GameObject has a Transform Component, which specifies its position, rotation, and scale, relative to its parent in the hierarchy (if you’re familiar with the math of 3D graphics, the Transform is essentially the transformation matrix of the object). Some components provide a function for the game object (e.g., a light is a GameObject with a Light Component attached). Other components reference assets such as meshes, textures, and scripts. Figure 2-27 shows the components of the Main Camera GameObject (in the Hierarchy view, the entire Player tree of GameObjects is displayed in blue because it’s linked to a prefab, a special type of asset that is used to clone a GameObject or group of GameObjects). Figure 2-27. Hierarchy View and Inspector View 40 Chapter 2 Getting Started Parent and Child GameObjects You will find that many of the GameObjects are arranged in a hierarchy, hence the name of this view. Parenting makes sense for game objects that are conceptually grouped together. For example, when you want to move a car, you want the wheels to automatically move along with the car. So, the wheels should be specified as children of the car, offset from the center of the car. When the wheels turn, they turn relative to the movement of the car. Parenting also allows us to activate or deactivate whole groups of game objects at one time. The Scene View Whereas the Hierarchy View allows us to create, inspect, and modify the GameObjects in the current scene, it doesn’t give us a way to visualize the scene. That’s where the Scene View comes in. The Scene View is similar to the interfaces of 3D modeling applications. It lets you examine and modify the scene from any 3D vantage point and gives you an idea how the final product will look. Navigate the Scene If you’re not familiar with working in 3D space, it’s a straightforward extension from working in 2D. Instead of just working in a space with x and y-axes and (x,y) coordinates, in 3D space, you have an additional z-axis and (x,y,z) coordinates. The x- and z-axes define the ground plane, and y- is pointing up (you can think of y as height). Note Some 3D applications and game engines use the z-axis for height and the x and y-axes for the ground plane, so when importing assets, you might have to adjust (rotate) them. 41 Chapter 2 Getting Started The viewpoint in 3D space is usually called the camera. Clicking the x, y, and z arrows of the multicolored Scene Gizmo in the upper-right corner is a quick way of flipping the camera so that it faces along the respective axis. For example, clicking the y arrow gives you a top-down view of the scene (Figure 2-28), and the text under the Scene Gizmo says “Top.” Figure 2-28. A top view in the Scene View The camera here is not the same as the Camera GameObject in the scene that is used during the game, so you don’t have to worry about messing up the game while you’re looking around in the Scene View. To demonstrate how to use the Navigation tools, I have selected the Player GameObject from the Project Folder and dragged this to the Hierarchy View. 42 Chapter 2 Getting Started Clicking the box in the center of the Scene Gizmo toggles the camera projection between perspective, which renders objects smaller as they recede in the distance; and orthographic, which renders everything at their original size whether they are close or far. Perspective is more realistic, and what you normally use in games, but orthographic is often more convenient when designing (hence its ubiquity in computer-aided design applications). The little graphic preceding the text under the Scene Gizmo indicates the current projection. You can zoom in and out using the mouse scroll wheel or by selecting the Hand tool in the upper-right toolbar of the Editor window and click- dragging the mouse while holding the Control key down. When the Hand tool is selected, you can also move the camera by click-dragging the view, and you can rotate (orbit) the camera by dragging the mouse while holding the Option (or Alt) key down, so you’re not restricted to just the axis camera angles, like in Figure 2-29. Figure 2-29. A tilted perspective in the Scene View 43 Chapter 2 Getting Started Notice that when you’re looking from an arbitrary angle, the text under the Scene Gizmo says Persp or Iso, depending on whether you’re using perspective or orthographic projection (Iso is short for isometric, which is the tilted orthographic view common in games like StarCraft). The other buttons on the toolbar activate modes for moving, rotating, and scaling GameObjects. There’s no reason to change them at the moment, so those modes will be explained in more detail when you start creating new projects. Tip If you accidentally make a change to the scene, you can select Undo from the Edit menu. If you made a lot of changes you don’t want to keep, you could just decline to save this scene when you switch to another scene or exit Unity. In the meantime, note that you can still move the camera while in those modes, using alternate keyboard and mouse combinations. Table 2-1 lists all the possible options. Table 2-1. Available Scene View Camera Controls Action Hand tool 1-button mouse or trackpad Move Click-drag Hold Alt-Command Hold Alt-Control and click-drag and click-drag Hold Alt and middle click-drag Orbit Hold Alt and click-drag Hold Alt and click-drag Hold Alt and click-drag Hold Alt and click-drag Zoom Hold Control and click-drag Hold Control and click-drag or two-finger swipe Hold Alt and right-click drag Hold Alt and right-click drag or scroll wheel 44 2-button mouse 3-button mouse Chapter 2 Getting Started There are a couple of other handy keyboard-based scene navigation features. Pressing the Arrow keys will move the camera forward, back, left, and right along the x–z plane (the ground plane). And holding the right mouse button down allows navigation of the scene as in a first-person game. The AWSD keys move left, forward, right, and back, respectively, and moving the mouse controls where the camera (viewpoint) looks. When you want to look at a particular GameObject in the Scene View, sometimes the quickest way to do that is to select the GameObject in the Hierarchy view, then use the Frame Selected menu item in the Edit menu (note the handy shortcut key F). In Figure 2-28, I clicked on the x-axis of the Scene Gizmo to get a horizontal view, then selected the Player GameObject in the Hierarchy View, and pressed the F key (shortcut for Frame Selected in the Edit menu) to zoom in on and center the player in the Scene View. You can also select a GameObject directly in the Scene View, but you have to exit the Hand tool first. Just as selecting a GameObject in the Hierarchy View will result in that selection displaying in the Scene View and Inspector View, selecting a GameObject in the Scene View will likewise display that selection in the Inspector View and display it as the selected GameObject back in the Hierarchy view. In Figure 2-30, after I invoke Frame Selected on the Player, I clicked the Move tool (the button directly right of the Hand tool button in the top right corner of the Editor window) and then clicked a GameObject near the Player in the Scene View. The Hierarchy View automatically updates to show that GameObject is selected, and the GameObject is also displayed in the Inspector View. 45 Chapter 2 Getting Started Figure 2-30. Selecting a GameObject in the Scene View Scene View Options The buttons lining the top of the Scene View provide display options to assist in your game development. Each button configures a view mode. The leftmost button sets the Draw mode. Normally, this mode is set to Textured, but if you want to see all the polygons, you can set it to Wireframe (Figure 2-31). 46 Chapter 2 Getting Started Figure 2-31. Wireframe display in the Scene view The next button sets the Render Paths, which controls whether the scene is colored normally or for diagnostics. The three buttons to the right of the Render Paths mode button are simple toggle buttons. They each pop up some mouse-over documentation (otherwise known as tooltips) when you let the mouse hover over them. The first of those controls the Scene Lighting mode. This toggles between using a default lighting scheme in the Scene View or the actual lights you’ve placed in the game. The middle button toggles the Game Overlay mode, whether the sky, lens flare, and fog effects are visible. And finally, there is the Audition Mode, which toggles sound on and off. 47 Chapter 2 Getting Started Scene View Gizmos The Gizmos button on the right activates displays of diagnostic graphics associated with the Components. The Scene View in Figure 2-32 shows some gizmos. By clicking the Gizmos button and checking the list of available gizmos, you can see those icons representing a Camera and a Light. Figure 2-32. Gizmos in the Scene View 48 Chapter 2 Getting Started You can select and deselect the various check boxes in the Gizmos window to focus on the objects you’re interested in. The check box at the top left toggles between a 3D display of the gizmos or just 2D icons. The adjacent slider controls the scale of the gizmos (so a quick way to hide all gizmos is to drag the scale slider all the way to the left). The Game View Now let’s look at the Game View. Like the Hierarchy View and Scene View, the Game View depicts the current scene, but not for editing purposes. Instead, the Game View is intended for playing and debugging the game. The Game View appears automatically when you click the Play button at the top of the Unity Editor window. If there isn’t an existing Game View when you click Play, a new one is created. If the Game view is visible while the Editor is not in Play mode, it shows the game in its initial state (i.e., from the vantage of the initial Camera position). The Game View shows how the game will look and function when you actually deploy it, but there may be discrepancies from how it will look and behave on the final build target. One possible difference is the size and aspect ratio of the Game View. This can be changed using the menu at the top left of the view. Figure 2-33 shows what happens when you switch from the Free Aspect ratio, which adjusts to the dimensions of the view, to a 5:4 aspect ratio, which results in the scaling down the game display so that it fits within the area and maintains the chosen aspect ratio. 49 Chapter 2 Getting Started Figure 2-33. The Game view M aximize on Play Clicking the Maximize on Play button will result in the Game view expanding to fill the entire Editor window when it is in the Play mode (Figure 2-34). If the view is detached from the Editor window, the button has no effect. 50 Chapter 2 Getting Started Figure 2-34. Game view with Maximize on Play S tats The Stats button displays statistics about the scene (Figure 2-35) that update as the game runs. Figure 2-35. Game view with Stats 51 Chapter 2 Getting Started Game View Gizmos The Gizmos button activates displays of diagnostic graphics associated with the Components. The Game View in Figure 2-36 shows two icons that are gizmos for an Audio Source. The list to the right of the Gizmos button allows you to select which gizmos you want displayed. Figure 2-36. Game View with Gizmos Both the Game View and Scene View are both depictions of the current scene. A Unity project consists of one or more scenes, and the Unity Editor has one scene open at a time. Think of the project as a game and the scenes as levels (in fact, some Unity script functions that operate on scenes use “level” in their names). Unity GameObjects are made interesting by attaching Components, each of which provides some specific information 52 Chapter 2 Getting Started or behavior. That’s where the Inspector View comes in. If you select a game object in the Hierarchy View or Scene View, the Inspector View will display its attached components. The Console View The remaining view in all the preset layouts, the Console View, is easy to ignore but it’s pretty useful (Figure 2-37). Figure 2-37. The Console view Informational, warning, and error messages appear in the Console View. Errors are in red, warnings in yellow, and informational messages in white. Selecting a message from the list displays it with more detail in the lower area. Also, the single-line area at the bottom of the Unity Editor displays the most recent Console message, so you can always see that a message has been logged even if the Console view is not visible. Tip Warning messages are easy to ignore, but you can ignore them at your risk. They are there for a reason and usually indicate something has to be resolved. And if you let warnings accumulate, it’s difficult to notice when a really important warning shows up. 53 Chapter 2 Getting Started The Console can get cluttered pretty quickly. You can manage that clutter with the leftmost three buttons on top of the Console View. The Clear toggle button removes all the messages. The Collapse toggle button combines similar messages. The Clear on Play toggle will remove all messages each time the Editor enters Play mode. The Error Pause button will cause the Editor to halt on an error message, specifically when a script calls a Log.LogError. While operating in the Editor, log messages end up in the Editor log, while messages generated from a Unity-built executable are directed to the Player log. Selecting Open Player Log or Open Editor Log from the view menu (click the little icon at the top right of the Console View) will bring up those logs, either in a text file or in the Console app (Figure 2-38). Figure 2-38. The Unity logs in the Mac Console app E xplore Further We’ve come to the end of this Unity tour. In Chapter 3, you’ll start learning some of the ARKit features. This is the first chapter that really starts using Unity. You haven’t yet started building your own scene (that will begin in Chapter 3), but you’ve been able to get familiar with the Unity Editor. There are plenty of official Unity resources that expand on the topics I will be covering. 54 Chapter 2 Getting Started U nity Manual As you can see, there’s a lot of Unity user interface, and we’ve hardly covered it all. This is a good time to get serious about reading the Unity Manual, either from within the Unity Editor (the Welcome screen or the Help menu) or on the Unity website (http://unity3d.com/) under the Learn tab in the “Documentation” section. The web version is pretty handy when you want to look something up or just read about Unity without having a Unity Editor running nearby. Most of what was covered in this chapter match topics in the Unity Basics section of the Unity Manual, particular the sections on “Learning the Interface,” “Customizing Your Workspace,” “Publishing Builds,” and “Unity Hotkeys.” We did jump ahead into the Advanced section of the Unity Manual and touch on Unity’s support for version control. That’s covered more in-depth with the Unity Manual’s page on “Using External Version Control with Unity.” T utorials Besides the “Documentation” section, the Learn tab on the Unity website also includes a “Tutorials” section that features an extensive set of Beginning Editor videos. As the name implies, these videos introduce the Unity Editor, and in fact the set of videos covers much of what was discussed in this chapter, including descriptions of the most important views (the Game View, Scene View, Hierarchy View, Inspector View, and Project View) and even the process of publishing a build. V ersion Control Although I only discussed version control briefly, in the context of explaining how to remove metafiles, that topic is worth a little more discussion, since a version control system (or VCS) is so important to 55 Chapter 2 Getting Started software development (which you’ll realize the first time you lose your project or can’t remember what change you made that broke your game!). If you already have a favorite VCS, you may want to use it with Unity, and if you haven’t been using one, then you may want to consider it if only to keep old versions of your project around in case you need to roll back, with the ability to check differences between versions, Among version control systems, Perforce is a popular commercial tool used in game studios, and Subversion (svn) has a long history as an open source option. These days, distributed version control systems like Git and Mercurial are trending. I use Mercurial on Bitbucket (http://bitbucket. com/) for my internal projects and post public projects on GitHub, including the projects for this book. To say Unity VCS support is product agnostic is really another way of saying Unity doesn’t have any particular version control system integrated into the Unity Editor. The metafiles, and YAML scene files for Unity Pro users, simply provide better compatibility with text-oriented version control systems that are commonly used for source code. You still have to run the VCS operations yourself outside of Unity. You can find out more about YAML, by the way, on http://yaml.org/. I find it convenient to use the Mac GitHub app provided on the GitHub website and similarly SourceTree for BitBucket, also available on that website. 56 CHAPTER 3 The Unity ARKit Now that you have installed Unity and the Unity ARKit Plug-in, it’s time to get to learn a bit more about this tool and make our first AR experience. Note, I called this an experience and not a game, although it could possibly be used as a game in some way; from my point of view a game needs some key elements, which I will go into detail in Chapter 4. If you have not already opened the AR Project we created in the last chapter, now would be a good time to do this. If you look in the Project folder (Figure 3-1) and look in each folder, you will see that the Unity ARKit Plug-in comes with a number of resources that we will explore and use throughout this book. C reating a Scene The first task we are going to do is create GameObject and put it in our scene and look at it in AR. From the Menu, select GameObject ➤ 3D Object ➤ Cube (Figure 3-1). We could choose any of the 3D objects, but I want you to see the Cube in AR and move around it in 3D space. Although I prefer Spheres, the 3D Cube has six vertices (the angular point of a polygon), and these are going to be easier to see. We will, however, add different shaped game objects very soon. © Allan Fowler 2019 A. Fowler, Beginning iOS AR Game Development, https://doi.org/10.1007/978-1-4842-3618-5_3 57 Chapter 3 The Unity ARKit Figure 3-1. Creating a 3D GameObject Once you have completed this task, you will see our amazing Cube in the Scene panel (Figure 3-2). Figure 3-2. The Cube GameObject in the Scene 58 Chapter 3 The Unity ARKit Now let’s have a look in the Inspector (Figure 3-3). If your Transform positions are not the same as mine, don’t worry about this for now (we are going to move the GameObject anyway). Note the scale is 1,1,1. Unity uses the metric standard for measurement, and therefore 1 unit in Unity space is the equivalent of 1 meter in real-world terms. Now for most traditional games (that are not AR), this is not really something that beginner developers need to worry about (much). However, because we will be projecting the game assets into a real-world space, getting measurements and scale correctly become very important. So, if we leave the scale of our Cube at 1 meter wide, by 1 meter tall, by 1 meter deep, this is going to be a pretty big cube. The convention for describing coordinates in 3D graphics is X,Y,Z. I will use this standard throughout this book. I am going to scale our amazing Cube, so it is going to fit into my very small apartment. It might surprise a few readers that most authors of instructional books don’t live in multi-million-dollar mansions (well at least this author doesn’t). 59 Chapter 3 The Unity ARKit Figure 3-3. The Cube GameObject in the Inspector 60 Chapter 3 The Unity ARKit In the Inspector, set the scale to 0.25,0.25,0.25 (Figure 3-4). Figure 3-4. The Cube GameObject reduced Scale 61 Chapter 3 The Unity ARKit Now if you look at the Cube in the Scene, it will (or should) look smaller (Figure 3-5). If you want to want a closer look at the GameObject, you can move the camera in the Scene view closer to the Cube. There are two main ways of moving the Scene view camera. The easiest way is by selecting the GameObject in the Hierarchy panel and pressing the f key (I remember this as f for focus). The other way is to use the middle mouse wheel (if you have a 3-button mouse) to move the camera in or out or double tab the mouse (if you have a Magic Mouse). I’m (very) old school and prefer to use my 5-button mouse. Figure 3-5. The Scene view of the Cube GameObject at reduced Scale C ameras Now would be a good time to talk about cameras. We just moved the camera in the Scene view. This camera enables you, the developer, to see what is in the Scene. However, if you look in the Hierarchy panel, you will see a GameObject Main Camera. This is the camera that the player will be looking through. You will note in Figure 3-5, there is a camera icon in my Scene view. This is where the Player’s camera is positioned in the scene. 62 Chapter 3 The Unity ARKit I want you to select the Main Camera in the Hierarchy panel. You will see a small window in the lower right of the Scene view a small window (Figure 3-6). This window displays a view from the Player’s camera (Camera Preview). Figure 3-6. Camera Preview Although the Camera Preview window provides us with a good idea of what the scene will look like to the player, to get a better view, we can select the Game view tab (Figure 3-7). 63 Chapter 3 The Unity ARKit Figure 3-7. The Game view As we saw in the previous chapter, there are a number of settings that we can use (aspect ratio, scale, and so on). We can see here that our amazing Cube is a bit far away. We are going to first move the Game Object, and then we are going to move the Main Camera. T ransformation There are several ways to move (transform) a GameObject in Unity. The first way we will use (and is the main method I use) is to set the transform settings in the Inspector. First, select the GameObject we want to transform. Then, in the inspector set the transform positions to 0,0,0 (Figure 3-8). 64 Chapter 3 The Unity ARKit Figure 3-8. The GameObject with the updated Transform positions Now let’s have a look in the Main Camera to check what this looks will like to the player (Figure 3-9). Figure 3-9. Camera Preview of the GameObject with reset Transform positions 65 Chapter 3 The Unity ARKit You will note that the GameObject is still a bit too far away from the Main Camera. At the top left of the screen (just above the Hierarchy panel), there are six icons (Figure 3-10). Figure 3-10. Six transform icons The first icon is the Hand tool (Q shortcut), and it is used for transforming (or panning) the camera in the scene view. With this tool selected, select any GameObject in Hierarchy move mouse and note that the transform settings of the GameObject don’t change, but the view of the GameObject changes. While holding down the alt (option) key, the Hand tool enables you to orbit the camera around its pivot point. By holding down the control button, you can move (or dolly) the camera closer or farther away from the GameObject. The next icon is the Move tool (W shortcut). The move tool, as you can probably guess, is for moving a GameObject’s position in the scene. With the Main Camera selected, select the Move tool (by selecting the Move icon or pressing the W key), and you will note that in the Scene view the Main Camera icon now has the move Gizmo (the Red, Green Blue lines with arrow points – Figure 3-11). First, select the Green line (Y-axis) and move the Main Camera down. Note that when you select this line, the other arrow lines are grayed out and the line you selected changes to yellow. Now try to move the camera to 0 on the Y-axis. You will note that this might take some time. You might want to enter zero in the Y in the Inspector if you can’t get the placement correct (which is why I prefer to type the values in the Inspector). 66 Chapter 3 The Unity ARKit Figure 3-11. The move Gizmo The next tool is the Rotate tool (E shortcut). As you probably guessed, this tool is used for rotating the GameObject. Try selecting the Cube and then press the R key. You will see the Rotate Gizmo (Figure 3-12). With the Rotate Gizmo, you can change the rotation of the GameObject by clicking and dragging the axes of the wireframe Rotate Gizmo that appears around it. With the Rotate Gizmo, the red, green, and blue circles perform a rotation around the red, green, and blue axes (red is the x-axis, green is the y-axis, and blue is the z-axis). The outer circle is used to rotate the GameObject around the Scene view z-axis. 67 Chapter 3 The Unity ARKit Figure 3-12. The rotate Gizmo The next tool is the Scale toll (R shortcut). The scale tool is used to scale or rescale GameObjects on all axes by selecting the center of the Scale Gizmo and then dragging the mouse (Figure 3-13). You can also use this tool to scale on an individual axis by selecting any one of the individual axes. Figure 3-13. The scale Gizmo 68 Chapter 3 The Unity ARKit Scaling the Cube with the Cube GameObject selected sets the position of the cube, or it has been moved while we were using the transform tools; now would be a good time to set or reset the position to 0,0,0 (Figure 3-14). Figure 3-14. The Cube GameObject Position settings 69 Chapter 3 The Unity ARKit Now we need to position the Main Camera at the origin (0,0,0). If you look at the position of my cube, this would mean our camera will be right in the middle of our Cube. So, let’s first move the cube out of the way. Set the Cube transformation position settings to 0,0,1 and then move the Main Camera to 0,0,0 (Figure 3-15). Now the cube will look like it is positioned 1 meter away from our camera. Figure 3-15. The new Main Camera settings T esting Now it’s time to see how our amazing cube is going to look in the real world. When developing for iOS, we need to use Xcode before we can test or deploy the game. To publish an AR game on iTunes, we need to submit the game to Apple for approval, and then when it gets approved, we can download it from the iTunes store and see what it looks like on our device. With Xcode, we can preview what our game will look like on our device, but this still requires us to build and run the game and use Xcode to preview the game. As you might imagine, this was a very time-consuming (and a little bit frustrating) experience. Unity came to the rescue and provided us with Unity Remote. Unity Remote is an application (or app) available for iOS devices in the iTunes app store. This tool helps us test a game on an iOS device without the need for 70 Chapter 3 The Unity ARKit submitting the game to the iTunes store. However, at the time of writing, Unity Remote (version 5) does not support AR. The fine people at Unity have thought about this challenge and have included in the Unity ARKit a small program called UnityARKitRemote. Unity ARKitRemote provides us with the tools needed to test our AR project on an iOS device. A RKit Remote In the Project folder, type in a search text to find the ARKit Remote Prefab (Figure 3-16). Now drag this file to the Hierarchy tab (Figure 3-17). Figure 3-16. Searching for the ARKit Remote Figure 3-17. The ARKitRemote Prefab in the Hierarchy 71 Chapter 3 The Unity ARKit Setting Up the Main Camera Now in the Main Camera settings, set the clear flags to Depth only (Figure 3-18). Figure 3-18. Set the Camera’s Clear Flags to Depth only A dding a Component Now we are going to add a Component to our camera. In this example, we are going to add the Unity AR Video script. To add this script, with the Main Camera selected, in the Inspector select Add Component. 72 Chapter 3 The Unity ARKit In the Inspector, you will see a list of possible components that you can add (Figure 3-18). It might take you a while to find it, so I recommend using the search bar in the Add Component menu for the Script we are looking for. In Figure 3-19, in the search bar, I searched for video. Figure 3-19. Searching for the Unity AR Video Script component 73 Chapter 3 The Unity ARKit Once you have found the Unity AR Video script, select it (single mouse-click) and now you should see this component is now added to the Main Camera (Figure 3-20). Figure 3-20. The Unity AR Video Script component added to the main camera In the Unity AR Video script, there is a property called Clear Material. We are going to add a material. In the Unity AR Video script Clear Materials properties, on the right of the properties box, there is a small gear (Figure 3-19); if you select this gear, you will see a list of all the possible materials available in this project folder (Figure 3-21). Again, you 74 Chapter 3 The Unity ARKit can search for this manually, or use the search bar. Search for the YUV material. I will go into a bit more detail on what this material is doing and why we use it in a later chapter. Figure 3-21. Selecting the YUV material for the Clear Material 75 Chapter 3 The Unity ARKit Tracking the Phone Movement To make the AR project look real, we need to track the movement of the phone in the real world and project an accurate representation of the virtual object on the phone’s screen. The fine folk at Unity have (again) made our lives easier and in the Unity ARKit has a Script called the Unity AR Camera Manager. Following the steps that we did to add the Unity AR Video script, we are going to add the Unity AR Camera Manager. First select the Main Camera and in the Inspector, select Add Component (Figure 3-22); now search for AR Camera Manager (Figure 3-23), and add it to the Main Camera. Figure 3-22. Add Component 76 Chapter 3 The Unity ARKit Figure 3-23. Searching for the Unity AR Camera Manager It is good practice to add the Main Camera to the Tracked Camera Properties of the Unity AR Camera Manager; this will ensure that the AR Camera Manager uses the correct camera. However, if you don’t select this, the Unity AR Camera Manager will choose it for you. To add the Main Camera to the Camera Properties of the Unity AR Camera Manager, select the Main Camera from the Hierarchy and drag this to the Tracked Camera properties of the Unity AR Camera Manager (Figure 3-24). 77 Chapter 3 The Unity ARKit Figure 3-24. Setting the Main Camera as the Tracked Camera B uild and Run Now we are ready to build and run our app. Make sure you have connected your iOS device to your Mac and then select Build & Run from the Unity File menu (Figure 3-25). If you have not already downloaded the latest version of Xcode, you need to do this now (it’s going to take a while). 78 Chapter 3 The Unity ARKit Figure 3-25. Selecting Build & Run Figure 3-26. The Build Settings 79 Chapter 3 The Unity ARKit In the Build setting menu, first select the Platform you want to build this on, which in our example will be iOS. Also, select the Development Build check box. Finally, it is really important to select only the scene you want to build. If your current scene is not listed, click the Add Open scenes button (Figure 3-26) and uncheck any other scene. From the Build Setting screen, select the Player Settings icon. This will open the Inspector for the Player Settings where we will enter the Company Name and Product Name (Figure 3-27). Figure 3-27. The Inspector view of the PlayerSettings Scroll down the menu to find the Bundle Identifier (Figure 3-26). It is important to note that once you have registered a bundle identifier to a Personal Team in Xcode, the same bundle identifier cannot be registered to another Apple Developer Program team in the future. This means that when you are testing your game using a free Apple ID and a Personal Team, you should choose a bundle identifier that is for testing only - you won’t be able to use the same bundle identifier to release the game. The best solution to do this is to add “Test” to the end of the test bundle 80 Chapter 3 The Unity ARKit identifier - for example, com.yourCompanyName.yourAppNameTest. Also note that the bundle identifier is written in what is known as reverse-DNS style. The accepted characters are alphanumeric characters, periods, and hyphens. In my example (Figure 3-28), I have used com. RottenEggProductions.HelloWorldARTest as the bundle identifier (You will need your own name). If you have a signing team ID, you may want to include that as well. But for testing purposes, this is not needed. Figure 3-28. Setting the Bundle Identifier in the PlayerSettings 81 Chapter 3 The Unity ARKit Now select the Build and Run icon. Unity will Prompt you to Save the Project. The tradition when learning Introductory Programming courses is to name our first application Hello World (don’t ask me why). So, in this tradition, I am going to name my first AR App, Hello WorldAR. Note in Figure 3-29, I am saving this in the same folder as my Unity Project. Some people would argue that this is not good practice, but it’s good enough for now. Figure 3-29. Saving the Hello WorldAR Menu After saving the file, Unity will start compiling the application (Figure 3-28) and will eventually open Xcode (Figure 3-29). When Xcode opens (Figure 3-31), make sure you have selected the correct device (iPhone or iPad), select the play button to launch your game on your device (Figure 3-30). If Unity reports any errors, be sure to check what the errors are and address these issues before retrying. 82 Chapter 3 The Unity ARKit Figure 3-30. Unity Compiling our application Figure 3-31. Xcode 83 Chapter 3 The Unity ARKit You will be prompted to allow the Unity ARKit to access your camera and if everything works, you should see your amazing cube in the real world. In my example, you can see my amazing cube projected in front of my photo of Mount Aoraki (Figure 3-32). Figure 3-32. My Hello WorldAR app S aving the Scene Now would be a good time to save our scene. From the file menu, select File ➤ Save Scene As and name this scene (Figure 3-33). I have chosen Hello WorldAR as the name of this Scene (Figure 3-34). 84 Chapter 3 The Unity ARKit Figure 3-33. Save Scene as menu Figure 3-34. Saving the Scene and selecting its location Understanding Scenes Now might be a good time to discuss the difference between Unity scenes and projects. A Unity Project contains all the scenes and necessary code that might be used for the game or application. A Scene is an element (or component) of the project. Think of the project as an entire movie and the scenes as parts of that movie. In a game, those scenes could be the menus, levels, credits, and so on. 85 Chapter 3 The Unity ARKit Introducing Visual Inertial Odometry Now we are going to look at some more of the important tools used for creating an AR game. In our Hello WorldAR project, we created a cube that was positioned in front of the iPhone’s camera and stayed there while we moved the camera position. How does the camera know where it is positioned? As you may already know, the iPhone has some pretty cool ways of knowing where it is positioned. The one I use most frequently is the accelerometer. The accelerometer allows the iPhone to know its position in 3 Axes (X,Y,Z). This is very useful for switching between portrait and landscape mode. The other tool I use is the compass (or magnetometer), and as you probably already know, this is very useful for navigation. The last tool is the gyroscope. The gyroscope tracks the rotation or twist of the iPhone. Although these are great tools for navigation, they do not have the level of precision needed for tracking the movement of the phone in AR. To track the movement of the iPhone needed for AR, Apple recently included some technologies in the phone’s camera. Through combining the Visual information (from the camera) and the Inertial information (from the accelerometer and the gyroscope), it is possible to accurately measure the position of the iPhone. Feature Points So how does the camera track the position of the phone? Good question! The camera in the iPhone (currently iPhone 8 or higher) is smart enough to identify key points (or feature points) in the real world and when the camera is moved, track where these points are. This process requires some pretty impressive mathematics, but the more recent iPhones have enough processing power to do this. 86 Chapter 3 The Unity ARKit P oint Clouds The Unity ARKit includes a prefab for helping the phone identify feature points in the physical world. The first one we are going to use is the PointCloud Prefab. With your Hello WorldAR app open in Unity, we are going to create an empty GameObject. From the file menu, select GameObject ➤ Create Empty (Figure 3-35). Figure 3-35. Creating an empty GameObject With the Empty GameObject Selected in the Inspector, rename it Point Cloud. Now add a component. In the search bar, search for the Unity Point Cloud Example and add this to the Point Cloud GameObject (Figure 3-36). 87 Chapter 3 The Unity ARKit Figure 3-36. Searching for the Point Cloud Example With the Unity Point Cloud Example script added, now set the number of Max Points to Show at 120 (Figure 3-37). You can set as many Point Clouds as you like. Figure 3-37. Setting the Max Points to Show Now we need to add a Point Cloud Particle Prefab to Point Cloud. Select the small gear to the right of the Point Cloud Prefab box and then search for Prefab (Figure 3-38). Select and drag the PointCloudPrefab to the Point Cloud Particle Prefab box in the Point Cloud Particle Example script (Figure 3-39). 88 Chapter 3 The Unity ARKit Figure 3-38. Searching for the PointCloud Prefab Figure 3-39. The PointCloudPrefab set as the Point Cloud Prefab Testing Now we are going to test our Point Cloud. When we tested our Hello WorldAR app, we went through the long (and possibly tedious) task of building the App, launching it in Xcode. and then finally being able to see our App on our iOS device. The wonderful people at Unity have thought about this and have created a way for us to reduce the time of testing our development. In the Unity ARKit, there is a Scene that will enable us to preview the build in the Game tab of Unity. If you want to go 89 Chapter 3 The Unity ARKit through the process of using Xcode every time you want to preview the development, that’s fine. However, I will show you a more efficient way that you might value. U nity ARKitRemote Because Unity Remote Connection currently does not support AR, we need to build and deploy an App to our iOS device. The fine folk at Unity have included in the Unity ARKit, a scene called ARKit Remote. You can find this in the Project folder using the search bar. In Figure 3-40, I have used the search string remote. Figure 3-40. Searching for the UnityARKitRemote Scene Double-click the Scene to open it. If you have not saved any changes to the current scene, you will be prompted to save it before Unity will open another scene. You will see that this is a very simple Scene that consists of a Main Camera and a Directional Light (Figure 3-41). Figure 3-41. UnityARKitRemote 90 Chapter 3 The Unity ARKit If you select the Main Camera in the Hierarchy, you will see that the Main Camera has several scripts added (Figure 3-42). These scripts will enable the phone camera to track its position as well as enable us to see the camera view in the Unity Editor. Figure 3-42. Main Camera Scripts 91 Chapter 3 The Unity ARKit Just like we did with the Hello WorldAR app that we created, we need to deploy this App to our phone. From the file menu, select File ➤ Build Settings (Figure 3-43). Figure 3-43. Selecting the Build Settings In the Build Setting menu, select the Player Settings menu and make changes to the Product Name and the Bundle Identifier (Figure 3-44). 92 Chapter 3 The Unity ARKit Figure 3-44. The Inspector view of the PlayerSettings 93 Chapter 3 The Unity ARKit Note, I have changed the Product Name to ARKitRemote and the Bundle Identifier to com.RottenEggProductions.ARKitRemoteTest (Figure 3-45). Figure 3-45. Bundle Identifier settings 94 Chapter 3 The Unity ARKit Make sure you have the correct Scene to build and select Build and Run from the Build Menu. Now Unity will compile our App and run Xcode and Xcode will deploy this to our Device (Figure 3-46). Figure 3-46. ARKitRemote installed on my iPhone Now we need to add the ARKit Connection Prefab to our Hello WorldAR project. Open your Hello WorldAR Scene (double-click). Using ARKit Remote Connection In the Project Folder, search for a Prefab called ARKitRemoteConnection (Figure 3-47). 95 Chapter 3 The Unity ARKit Figure 3-47. Searching for the UnityARKitRemoteConnection Prefab Select this Prefab and add it to the Scene (drag it to the Hierarchy) (Figure 3-48). Figure 3-48. The UnityARKitRemoteConnection in the Scene Now we are going to open the ARKitRemote on our iOS device. In Unity, open up the Hello World AR scene and then select the Game tab and press the Play button. Unity will prompt you to connect to player in the console menu (Figure 3-49). 96 Chapter 3 The Unity ARKit Figure 3-49. Unity connect to player message In the Console table, select Editor and select your iOS device (Figure 3-50). Figure 3-50. Selecting the iOS device in the Console menu Then Unity will prompt you to Start Remote ARKit Session (Figure 3-51). Click the icon on the Game screen in Unity and your application can now run on your iOS device. 97 Chapter 3 The Unity ARKit Figure 3-51. Start Remote ARKit Session prompt In Figure 3-52, you can see that I have successfully been able to view the Hello WorldAR app on my iPhone and see the Scene in Unity. If you move your device, you should see the Camera move in the Unity Scene tab and the picture change in the Game tab. I would like to point out that it’s going to be a bit slow (lag), but the ARKitRemote is currently the best way to develop and test Unity AR development for iOS. Figure 3-52. The Cube and the Point Cloud in the Game View 98 Chapter 3 The Unity ARKit While the point cloud system is great for identifying a non-symmetric object (like a sofa). It will be more efficient and more effective if we use another feature provided in the UnityARKit that will help identify a flat plane (like a floor, wall, or table). P lane Visualization First, create another empty GameObject that will contain the scripts needed. To create an Empty GameObject, from the menu, select GameObject ➤ Create Empty (Figure 3-53). Figure 3-53. Creating an Empty GameObject With the GameObject selected, in the Inspector, name this Created Planes (Figure 3-54). 99 Chapter 3 The Unity ARKit Figure 3-54. Naming the new GameObject Created Planes Now we need to add a component. Select the Add Component button in the Created Planes Inspector and in the search bar, search for the Unity AR Generate Plane script (Figure 3-55). Figure 3-55. Searching for the Unity AR Generate Plane script 100 Chapter 3 The Unity ARKit With the Unity AR Generated Plane script included in the Created Planes GameObject, in the Plane Prefab setting of the Unity AR Generated Plane script. select the small gear to the right of the option box and search for and select the DebugPlanePrefab (Figure 3-56). Figure 3-56. Searching for the debugPlanePrefab T esting Now with your iOS device connected to your Mac, run the UnityARKitRemote App. Now in Unity. connect to your device in the Editor and click the Play button. With your iOS device, turn the camera to view a flat surface, and you should see your point cloud and a rectangle in the Unity Game view screen (Figure 3-57). 101 Chapter 3 The Unity ARKit Figure 3-57. The Point Clouds and Created Plane Both the Point Clouds and Created Planes will be useful in the game that we create for finding and tracking the reference points from the iOS camera. If you are feeling brave, you might want to try deploying the final build to your iOS device. However, at the moment, our Cube is not quite ready for really using the reference point tracking of the camera. Summary In this chapter, we have learned about using the Unity ARKit and configuring our iOS device to preview our applications and games in real time. We also learned some theory of how the Unity ARKit can track the position of the device. Finally, we used some tools to help us accurately track the fine movements of the device. 102 CHAPTER 4 Hit Testing and Lighting In our last chapter, we used both Cloud Points and Generated Planes to help the camera track the movement of the device. However, you may have noticed that our amazing cube continued to stay at the point of origin. While this may be okay for our first AR App, I’m sure in time you might want to create virtual objects that appear to be located on a physical object in the real world. To do this, we are going to use a feature of the Unity ARKit called hit testing. You may have also noted that the lighting of the GameObject on the screen of your iOS device is not consistent with the lighting in the real world; we are also going to address this issue in this chapter. H it Testing Hit testing will enable us to place something on the Generated Planes we created in Chapter 3. This will also enable the AR App to place an object to look like it is in a position in the real world that is relative to where the user taps on the screen of their device. Let’s open the Hello WorldAR scene we created in the last chapter. With the Hello World AR Scene open in Unity, now select the Created Planes GameObject in the Hierarchy (Figure 4-1). © Allan Fowler 2019 A. Fowler, Beginning iOS AR Game Development, https://doi.org/10.1007/978-1-4842-3618-5_4 103 Chapter 4 Hit Testing and Lighting Figure 4-1. Selecting the Created Planes GameObject in the Hierarchy With the Created Planes GameObject selected in the Hierarchy, select Add Component; and in the search bar, search for the Editor Hit test (Figure 4-2). Now select this to add it to our Created Planes Game Object. This script will enable us to test out the basic functionality of Hit Testing in the editor. Figure 4-2. Searching for the Editor Hit Test 104 Chapter 4 Hit Testing and Lighting By now you might be keen to include something in the AR App a bit more interesting than our cube. We could search the Asset Store for an interesting or relevant asset to include, or we could create our own one. As making our own game assets is very time consuming and outside of the scope of this book, for now, let’s use one of the existing assets in the Unity ARKit. In the Project folder search for the asset Player (Figure 4-3). If this GameObject is not included in the version of the Unity ARKit you download, it can be downloaded from the Unity Asset Store (https://assetstore.unity.com/packages/essentials/ tutorial-projects/survival-shooter-tutorial-40756). Figure 4-3. Searching for the Player game asset With this asset selected, drag it to the Hierarchy (Figure 4-4). Figure 4-4. The Player character added to the Hierarchy 105 Chapter 4 Hit Testing and Lighting Now it’s time to say goodbye to our amazing cube. Select the Cube in the Hierarchy and right-click and select delete (Figure 4-5). Figure 4-5. Deleting the Cube S cale You might recall that in Chapter 2, I discussed the importance of scale in AR. If you select the Player GameObject in the Hierarchy, you will see that this asset is 1 meter tall. Now, this may be fine, but I want to make this asset look a lot smaller in the real world. I suspect that this is the intention of the person that created this asset. With the Player GameObject selected, change the scale to .25,.25,.25 (Figure 4-6). 106 Chapter 4 Hit Testing and Lighting Figure 4-6. The Rescaled Player GameObject T ransformation If you select the Main Camera and look at the Camera Preview window, you will notice that our Player GameObject is not visible to the player. If you look at the Inspector or both the Main Camera and the Player GameObjects, you will see that the positions of both of these assets are 0,0,0. Let’s move the Player GameObject. With the Player, GameObject selected, in the Inspector change the position to 0,0,1 (Figure 4-7). 107 Chapter 4 Hit Testing and Lighting Figure 4-7. The Player GameObject Repositioned You will notice in Figure 4-7 that the Player GameObject is facing away from the camera. I think it would be better if the Player GameObject was facing the player. With the Player GameObject selected, using the rotate tool, and rotate the Player Game Object in the Y-axis until it faces the main Camera (Figure 4-8). Figure 4-8. Rotating the Player GameObject When rotating a GameObject, it is possible to also unintentionally change another axis. In Figure 4-8, notice how the Transform positions of the X-axis and the Y-axis have also changed. Let’s reset the positions