Getting started with Unity
Unity is a great platform to start learning game development or even shipping a commercial game. It is the choice of game engine for many of the popular games in the Android or iOS app stores. So, what makes Unity such a great platform to develop games on? The following is a short list of things that make Unity such a compelling platform to develop games on:
- It is free to get started: There is a ton of free assets or code you can use to build games. We will look at a number of free assets in this book.
- Incredibly easy to use: You could likely build a complete game in Unity without writing even one line of code. Fortunately, you will learn some scripting and how to write code in this book.
- Entirely cross platform: With Unity, you can develop on or for any environment that suits you. Of course, certain platforms, such as mobile, still have limitations and we will cover that as well.
- Outstanding community: Unity has a hardcore base of developers who are eager to share experiences and help others. We will be sure to showcase many of the great community resources.
- Asset Store: The Unity Asset Store can be an invaluable tool to build your first game or even your seventh commercial title. We will show you the deals and what to avoid.
Creating the game project
Let's get started by creating our sample game project Foody GO; we will also take this opportunity to build and deploy this starting project to your mobile device:
- Launch Unity and start a new project named
FoodyGO
. Make sure that 3D is enabled and disable Unity analytics. Of course, you will want to save your project in an easy-to-find folder, such as Games, just as shown in the following sample screenshot: - Click on the Create project button and wait for Unity to open.
- In the Hierarchy window (top-left corner), you will see a scene named Untitled. Beneath that, there will be the Main Camera and Directional Light attached. Here is the screenshot of what you will see:
- First thing you will want to do is rename the scene and save it. So, go to File | Save Scene As...
- A save dialog will open, allowing you to choose where to save the scene. Just choose the default
Assets
folder and name your sceneSplash
. Then, click on Save. - The title of the scene should now read Splash. Notice that there is also a new Splash scene object in the project
Assets
folder. - Now let's adjust the Unity editor layout to match how our mobile game will run. From the menu, select Window | Layouts | Tall. Then, undock the Game tab from the main window by selecting and clicking the mouse while dragging the tab over. Then, resize the window so both the scene and game windows are roughly of the same width.
- Save the layout by opening the menu to Window | Layouts | Save Layout. Name your layout
Tall_SidebySide
and click on Save. This will allow you to quickly return to this layout later. - Select the Main Camera by double-clicking on it, in either the Hierarchy window or Scene window. Notice how the Scene window will focus on the Main Camera object, and the Inspector window will display all the properties. This is what the Unity editor window should look like now:
Editor layout for mobile development
- Before we go further, let's take a look at each of the main windows we will be working with in Unity:
- Scene window: This window will allow you to view and interact with the game objects in the scene.
- Game window: This is the players' view of the scene rendered by the main camera.
- Hierarchy window: This shows a tree view of the game objects in the scene or scenes. In most cases, you will select or add items to the scene in this window.
- Project window: It provides a view of and a quick way to access the assets in your project. Not much is in our project now, but we will quickly add some new assets in the coming chapters.
- Inspector window: This view allows you to inspect and alter settings on game objects.
- Click on the Play button located at the top middle of Unity editor. The game will start playing, but nothing will happen because we only have a camera and a light. So let's add a simple Splash screen.
- In the Hierarchy window, select the scene. From the menu, select Game Object | UI | Panel to add a Canvas and Panel to the scene.
- Double-click on the Panel object in the Hierarchy window. This will focus the Scene window on the panel. In the Scene window, switch the view to 2D by clicking the button at the top of the window. This is what you should see now:
Editor window with UI focused on in Scene window
- The panel in the scene window is a 2D UI element that allows us to render text or other content to the player. By default, the panel centers itself to the main view of the camera when it is added to the scene. This is why we see the semitransparent panel cover the entire Game window. Since we don't want a transparent background to the splash screen, let's change the color.
- Select the Panel in the Hierarchy window. Then, click on the white box beside the Color property in the Inspector window to open the Color settings. You will see the following dialog:
- Enter
FFFFFFFF
in the Hex Color field and then close the dialog. Notice how the background of the Game window is now opaque white. - From the menu, select Game Object | UI | Text. In the Inspector window, set the properties of the Text to match the screen extract, as follows:
- Run the game by clicking on the Play button. Not much happens, but we now have a splash screen for our game. Don't worry, later in the book, we will add some pizazz to this screen. For now though, let's get this game deployed to your device.
Building and deploying the game
Now that we have the foundations of our game and a simple splash screen let's deploy it to your device. Nothing confirms your progress as a game developer other than seeing your game run outside the Unity editor. Follow the section that is specific to your device to complete the build and deployment.
Building and deploying to Android
As long as you have followed the steps for installation in the previous sections of this chapter, deploying your game to Android should be simple. If you do encounter any deployment issues, refer to Chapter 10, Troubleshooting. Follow the next steps to build and deploy to your Android device:
- From the menu, select Edit | Preferences. This will open the preferences dialog.
- Select the External Tools tab. Change or set the location of your
Android SDK Path
andJava JDK Path
to the installation paths we made a note of when installing. Close the dialog when you are finished. The subsequent sample screenshot shows where you need to enter those paths: - Select Edit | Project Settings | Player from the menu. Select the Android Settings tab and then click on Other Settings at the bottom of the panel. Set the Bundle Identifier to
com.packt.FoodyGO
as shown in the following screenshot: - Make sure that your Android device is connected by the USB. Refer to the Connecting to your Android Device section earlier in this chapter if you are unsure.
- Open the Build Settings by selecting from the menu File | Build Settings. On the Build Settings dialog, click on the Add Open Scenes button to add the Splash scene. Ensure that you select the Android build from the list of build types. When you are all set and ready, click on the Build and Run button. This is how the Build Settings dialog should look like:
- A file save dialog will open the project's root folder. Create a folder called
Build
while you still have the dialog box opened. Open the new Build folder and save your build ascom.packt.FoodyGO
. The name should match the name we used for the build identifier earlier. Then, click on Save to start the build. - Since this is the first build of the project, Unity will reimport all the project assets and other modules, which may take a few minutes. Subsequent builds should take less time, but if you change platform outputs, everything will need to be reimported.
- After the build completes, open your device. You will likely see the Unity screen loading and then the splash screen. Congratulations, you have deployed the game to your device.
Building and deploying to iOS
Assuming you followed the previous section, Setting up for iOS development, you should be able to already build and deploy the game to your device. Just follow the build and deployment steps again in that page to deploy the game to your device. Download the sample project for the chapter and deploy that to your iOS device.