© Kishan Takoordyal 2020
K. TakoordyalBeginning Unity Android Game Developmenthttps://doi.org/10.1007/978-1-4842-6002-9_2

2. Introduction to Unity

Kishan Takoordyal1 
(1)
Eau Coulee, Mauritius
 

As you already know, we will be using the Unity game engine in order to develop games. Unity has been written in the C++ programming language, but its scripting application programming interface (API; what we’ll use to actually code the games) is in C#. Unity can be used for much more than just making games. It can even be used to create visualizations for movies, architecture, or car manufacturing.

The benefit of using Unity rather than writing our games from scratch is that Unity already provides many ready-made tools to assist us in our game-making process, such as physics or lighting. Compared to other game engines, Unity is far more popular, which makes resolving bugs or learning how to do something easier, because it is very likely that something will already be present on the Internet. Any games we wish to work on in Unity is a project.

2.1 Creating a Unity Account

You must have an account set up at https://id.unity.com in order to use Unity. As well, you can create an account using your Google or Facebook account (Figure 2-1).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig1_HTML.jpg
Figure 2-1

Creating a Unity account

2.2 Downloading Unity and Add-ons

Unity has four licenses: Personal, Plus, Pro, and Enterprise. Personal has most of the features offered in the other business licenses and is free (Figure 2-2). Plus can be purchased by individuals who want more features than Personal offers. Pro or Enterprise licenses must be purchased if income derived from your game-developing business or organization exceeds a particular threshold. The greatest advantages that you’ll be forfeiting by using a Personal license are the ability to customize the splash screen of your game (when the game starts), the use of a dark editor UI, greater support, availability of premium resources (although not absolutely required), and more diagnostics/analytics. More information can be found at https://store.unity.com/.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig2_HTML.jpg
Figure 2-2

Unity plans

2.2.1 Unity Hub

Unity Hub is an application that can be used to download several versions of Unity, along with their respective modules, if required, and contains a list of the projects (cloud or local) you’re working on or have created (Figure 2-3).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig3_HTML.jpg
Figure 2-3

Unity Hub projects

For MacOS or Windows users, Unity Hub can be downloaded from https://unity3d.com/get-unity/download/, and for Linux users, an executable file can be obtained from the forum at https://forum.unity.com/threads/unity-hub-v2-0-0-release.677485/. Just install or run it after it finishes downloading.

Next, you must download a version of the Unity Editor (the actual game engine). You’ll have to use any 2019.3.x version for the examples in this book. Sign in to Unity Hub first, with the credentials you previously used to create an account. Then, proceed to click your profile picture or initials (top-right corner). Go to Manage License and activate a new Unity Personal license. Finally, go to Installs, click Add, select a 2019.3.x version of Unity, and choose the modules you wish to install. As we will be working on a mobile game, choose at least Android and/or iOS Build Support modules. Note that you will not be able to make iOS games if you don’t have an Apple computer. For Android build support, check Android SDK & NDK Tools and OpenJDK, too (Figure 2-4). The documentation and everything else is optional.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig4_HTML.jpg
Figure 2-4

Downloading the Unity Editor from the Hub

You will also require an integrated development environment (IDE) to write scripts. Visual Studio Code is a lightweight and excellent choice. You can download it from https://code.visualstudio.com/. We will be linking it to the Unity Editor later. For now, just install it.

2.2.2 Creating an Empty Project

In Unity Hub, click the blue New button, name the project you’re about to create, and choose a location in which to store it. Choose 3D as the template. Click Create, wait a while, and the project should open (Figure 2-5).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig5_HTML.jpg
Figure 2-5

Creating a new project

First, go to Edit ➤ Preferences ➤ External Tools and make sure you’re using the built-in JDK, SDK, and NDK. The appropriate check boxes should be ticked. You will also have to assign the vscode option in the External Script Editor tab, to write and modify scripts in a proper IDE. This option is just under the External Tools header (Figure 2-6).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig6_HTML.jpg
Figure 2-6

Checking preferences

2.3 Essential Windows

Unity provides several windows with very specific features to help developers in their game-developing process. You already know what a project means in Unity. Game projects contain scenes. Think of scenes as levels of a game, for example. It would be a bad idea to load everything when a lot of the things are currently unnecessary, for example. In scenes, you can design your levels and make them playable.

Now, everything that is present in a scene is called a GameObject (more on these in Chapter 3 of this book). When you make games, you will probably require images, sound, 3D models, etc. All of what you import in your project is an asset, whether you ever use it in a scene.

I’ll go through some of the most frequently used windows in Unity. Your empty project should look mostly like this (Figure 2-7):
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig7_HTML.jpg
Figure 2-7

An empty scene

You can change the layout of these windows by clicking the little layout button found at the top right of the Unity Editor (Figure 2-8). You can also resize them by dragging their edges.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig8_HTML.jpg
Figure 2-8

Layouts

2.3.1 The Project Window

If you’re using the default layout, it should normally be found at the bottom of the editor. The Project window (Figure 2-9) is a collection of assets and directories that you have created or imported into a Unity project. You can also search the entire project to find an asset by its name or type, by using the search bar.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig9_HTML.jpg
Figure 2-9

The Project window

2.3.2 The Hierarchy Window

The Hierarchy window (Figure 2-10) basically contains a list of all the game objects that are present in the scene that is currently opened inside the editor. By default, it contains a camera and a light source, which you’ll learn more about in Chapter 3. It is found at the top left of the editor, in the default layout.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig10_HTML.jpg
Figure 2-10

The Hierarchy window, as it is by default

Try to create some primitive and basic GameObjects right from the Hierarchy window, by left-clicking the little plus icon or right-clicking anywhere in the window, to bring up a menu with options to choose from. The newly created GameObject will instantly show up in the Hierarchy window (Figure 2-11).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig11_HTML.jpg
Figure 2-11

Creating a 3D GameObject

2.3.3 The Scene Window

The Scene window is mostly used in level design where GameObjects must be placed to create in-game areas. You can navigate around the scene by using your mouse and make changes directly to objects in the scene in the Scene window. Note that the red color designates the x axis, green designates the y axis, and blue the z axis (Figure 2-12).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig12_HTML.jpg
Figure 2-12

The scene window with a cube

You can select objects by left-clicking them in the Scene window. Try also right-clicking and dragging to rotate around, or use the scroll wheel to zoom in and out. You can finally click and hold the scroll wheel and move your mouse to pan around. If you left-click and select objects in the Inspector window, they will appear selected in the Scene window also.

There are seven tools (Figure 2-13) to help you perform operations in the Scene view. The first is the Hand tool. When it is selected, no object will be selected when you click them. Instead, the left mouse button will be used to pan around the scene in the same way that holding the scroll wheel does, as previously described.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig13_HTML.jpg
Figure 2-13

The seven scene tools

The second scene tool is the Move tool (Figure 2-14). When an object is selected in the Scene window, you can drag arrows to move it in the direction of a specific axis. The object may also be dragged by the two little squares, to move along two axes simultaneously.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig14_HTML.jpg
Figure 2-14

The Move tool

The third scene tool, the Rotate tool, works in the same way as the Move tool, except that it is used for rotating GameObjects. By dragging along the colored circles, you can rotate a GameObject on one of the three axes, or along the gray circles, to rotate on two axes simultaneously (Figure 2-15).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig15_HTML.jpg
Figure 2-15

The Rotate tool

The fourth tool is the Scale tool (Figure 2-16), which allows us to scale selected GameObjects down or up. By dragging along the colored squares, you’re scaling GameObjects up or down along the respective axis, and by dragging from the little square at the center of the GameObject, you’re scaling GameObjects up or down uniformly along all axes at a time.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig16_HTML.jpg
Figure 2-16

The Scale tool

The fifth scene tool is the Rect tool, which is useful for moving and scaling 2D objects. We’ll be using it afterward, when we’ll have to move or scale images, buttons, and 2D UI elements.

The sixth tool combines the features of the Move, Rotate, and Scale tools. We’ll skip what it does and use the seventh and final (Multi) tool for now (Figure 2-17).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig17_HTML.jpg
Figure 2-17

The Multi tool

In the Scene view, you can also click the little cones at the top-right corner of the window, to make the view angle perpendicular to an axis. For example, try clicking the red cone, to make the view angle perpendicular to the x axis, and, essentially, the content you see will now appear in two dimensions, bounded by the z and y axes (Figure 2-18).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig18_HTML.jpg
Figure 2-18

Viewing in the x axis direction

You can also click the text under the little cones, to switch between a perspective or orthographic view, depending on the type of game you’re making.

The first button at the top left of the Scene view allows you to see GameObjects in another form. A good example of moving to wireframe could be to adjust wheels for a car (Figure 2-19).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig19_HTML.jpg
Figure 2-19

Wireframe shading in the Scene window

The little 2D button at the top of the window makes everything appear in 2D, which is useful for working with 2D UI elements or on 2D games. The other buttons mainly serve to toggle on or off such things as lighting, audio, or effects.

Finally, there are also two buttons by default, labeled Center and Global. When you click the former, either Center or Pivot will be displayed. When using transform tools such as Move or Rotate, the arrows will be placed either at the center or at the pivot point of the selected GameObject(s). Try this in the Editor now, to get more comfortable with working with these. Try moving, rotating, and scaling 3D objects, and practice everything you’ve learned until now.

For example, if there are two cubes in the scene and both have been selected, if the first button is set to Center, the current tool will be placed equidistant from the two cubes (Figure 2-20).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig20_HTML.jpg
Figure 2-20

Center point of two GameObjects

If Pivot is used instead, the tool will be placed at the center of one of the two cubes, therefore known as the pivot point. In this case, the pivot point will be determined by which of the two cubes is selected first. If the left one is selected first, its center will be the pivot point (Figure 2-21).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig21_HTML.jpg
Figure 2-21

Pivot point of two GameObjects

Now on to Global and Local modes. Basically, when the mode of the first button is set to Global, all the tools will have the same positioning relative to the world. The red arrow will always point right (x axis); the green arrow will always point up (y axis); and the blue arrow will always point forward (z axis). These won’t depend on the position, rotation, or scale of the selected GameObject(s).

However, for the Local mode, the tool will always depend on the position, rotation, or scale of the selected GameObject(s). In a way, it’s like saying the blue arrow “is the z axis of the GameObject,” which means that the blue arrow will point to the forward movement of the GameObject(s), rather than the norm, which is across the world space that Global defines. In the following example, the cube is slightly rotated, so that its “forward” points a bit upward (Figure 2-22).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig22_HTML.jpg
Figure 2-22

Local mode with a selected GameObject

There is also a little magnet icon next to the Global/Local button. This can be used to toggle on/off Grid Snapping. This can be used when tool positioning is set to Global. Grid Snapping allows you to move objects by units of 1 across all axes and will thus snap the position of GameObject(s) to the closest whole number position in the scene.

2.3.4 Game View

This can be found just beside the Scene window, if you’re using the default layout. It is mostly used for testing game projects before exporting, as it shows a representation of the actual exported game to a third-party user (Figure 2-23).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig23_HTML.jpg
Figure 2-23

The game view

Figure 2-23 shows what your empty game would look like if someone tried to play it right now. Display 1 allows you to toggle between different displays, to see how specific places will look.

The Free Aspect tab allows you to set a resolution/ratio, so that it is easier for you to see how the game will be rendered on a display with that particular resolution/ratio. Free Aspect takes the whole size of the game window. You can also create new resolutions or ratios (Figure 2-24).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig24_HTML.jpg
Figure 2-24

Setting a new resolution/ratio in the Game view

Using the slider for scale, you can zoom in or out of the Game window, although that is not available in the actual game if you don’t implement something similar. Maximize On Play can be set on or off. It will appear whiter if activated, and that allows you to test your game in full screen. Note that at this point, nothing will happen if you toggle it on or off.

Stats allow you to preview some info about the game, such as CPU usage or the number of vertices present (Figure 2-25).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig25_HTML.jpg
Figure 2-25

Showing stats in the Game view

Gizmos allows you to preview more things that are shown to the end user, such as components like colliders, which you’ll learn more about in the next chapters. Finally, on to Play mode.

When you want to test your game without exporting a build of it, you can do that within the editor. There are three icons at the top of the Game window. The first (leftmost) one is the Play button. When you click that button, the Editor goes darker, the button turns blue, and you’re in what is called Play mode. In Play mode, you’re playing the game as a normal user would if they had a build of that game. To exit Play mode, you simply click the Play button again.

When in Play mode (Figure 2-26), you can click the button beside it—the Pause button to temporarily pause the game. Clicking the Pause button again will resume it. The last (rightmost) button is the Step button, and whenever you click it, it will automatically play one frame and pause the game, if it isn’t paused already. You can still click the Pause button to resume the game. The Step button is useful if we want to know what’s happening with each frame that is leading to a bug.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig26_HTML.jpg
Figure 2-26

Entering Play mode

Please be aware that any changes you make while in Play mode are temporary. That is, if, for example, you move an object in Play mode, when you go back to the normal editing mode, the changes will revert to how they were before you entered Play mode.

2.3.5 The Inspector Window

If you’re using the default layout, the Inspector is the big vertical triangle at the right side of your screen. It contains much information on selected GameObject(s), based on the components that are attached to this (these) GameObject(s). By default, all GameObjects have a transform component (Figures 2-27 and 2-28). Most components have fields or properties with values that can be tweaked from this window.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig27_HTML.jpg
Figure 2-27

A typical default Inspector window for a cube: part 1

../images/491558_1_En_2_Chapter/491558_1_En_2_Fig28_HTML.jpg
Figure 2-28

A typical default Inspector window for a cube: part 2

The cube we’ve been taking as an example, or a new cube that we create, should normally have these components by default. The purpose of tags and layers will be explained in the last chapters, and the use of the components you’re seeing will be documented in Chapter 3. When the check box next to Static is ticked, our GameObject(s) won’t move or physically change at runtime, no matter what we do. This has performance gains. You are advised to try changing the properties of some components, to see the difference this makes.

2.3.6 The Unity Asset Store

If you remember what was mentioned about the definition of assets in the section where you learned about the Project window, there are several ways to import assets into Unity. One of the most common is by opening .unitypackage files. A Unity package, or a file ending with a .unitypackage extension, is a compressed file that includes several assets.

For this section, we’ll focus mostly on downloading and importing assets from the Asset Store, rather than from third-party unofficial sources. The Asset Store is kind of marketplace where you can browse and download assets for your games. Think of it as Unity’s Google Play for Assets.

To access the Asset Store, you must head to Window and click the Asset Store tab. Or simply hit Ctrl+9. You can resize the Asset Store window or place it somewhere. It should look something like Figure 2-29 in full screen.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig29_HTML.jpg
Figure 2-29

The Asset Store

You can search for assets using the search bar and use filters for things such as price and category. For the sake of this tutorial, we are going to download and import a package called Simple Input, which happens to be free. Use the pricing filter to target only free assets (Figure 2-30).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig30_HTML.jpg
Figure 2-30

Searching for assets

Then click the first one that pops up (it should look like the first result in the preceding screenshot). You can read the description, preview what files will be imported, and check out the screenshots or reviews while on the page of an asset. Hit download when you’re ready. When it finishes downloading, you should now see on that button that Import appears instead of Download (Figure 2-31). Click it.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig31_HTML.jpg
Figure 2-31

The Simple Input system asset

Unity will now decompress the package. Finally, hit Import. The assets will appear in your Project window once everything is imported (Figure 2-32).
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig32_HTML.jpg
Figure 2-32

Importing an asset—Simple Input System in our project

2.3.7 The Console Window

Console is a read-only window that can have logs, warnings, or errors (Figure 2-33). While testing your code for your game projects, you’ll often output values to the Console, to make sure everything is working as expected. This is called debugging. Unity may also warn you of things that may not really affect your project right now but can cause problems in the long term. As for errors, you must fix them; otherwise, you will not be able to run or build your game.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig33_HTML.jpg
Figure 2-33

Example of logging, warning, and error messages in the Console

When you click a log/warning/error in the Console window, more details will appear at the bottom of the screen, indicating mainly the statement and line number that are responsible for the message in question. If you double-click a message in the Console, the responsible script will be opened in the default text/code editor Unity has been assigned to use.

The Clear button will remove all the messages in the Console window, except those that must absolutely be fixed to continue development of your game project (Figure 2-34). The Clear on Play automatically performs the operations of the Clear button every time you enter the Play mode, as long as it is enabled. Similarly, Clear on Build will run each time Unity successfully builds an executable file of your game.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig34_HTML.jpg
Figure 2-34

Amount of messages by type and genre in the Console window

The Collapse tab will compile the same console messages together in a single message. The number of instances will be represented in a small circle at the far right of the Console window. Error Pause will pause the game in Play mode when an error is received, and the last drop-down button Editor allows you to choose where you want to have debug messages from, perhaps from a device connected to your computer, for example.

The Search bar allows you to search for messages in particular, and the last three small colored buttons allow you to choose what kind of debug messages you desire. For example, clicking the yellow button will stop all warning messages from popping in the Console window. However, the number of every type of logging message received since the last time you hit Clear will still continue to be displayed beside their respective buttons.

2.3.8 Build Settings

This can be accessed from File ➤ Build Settings. In the Build Settings window, you can switch to the platform with which you want to export your game and adjust some more options that will be covered more fully in future sections (Figure 2-35). The current platform you’re on will be represented by a small Unity logo before its label. For future projects, switch to the Android platform. You’ll see how to configure and make builds soon. Drag and drop the scenes you want in the Build Settings window. The first one will be the one the player will see as they open your game. Finally, it’s a good practice to save your scene and project. Do so from the File tab.
../images/491558_1_En_2_Chapter/491558_1_En_2_Fig35_HTML.jpg
Figure 2-35

The Build Settings window

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17