Unity 2018 By Example(Second Edition)
上QQ阅读APP看书,第一时间看更新

Building

So, now it's time to build the game! That is, to compile and package the game into a standalone and self-executing form, which the gamer can run and play without needing to use the Unity Editor. Typically, when developing games, you'll reach a decision about your target platform (such as Windows, iOS, Android, and others) during the design phase and not at the end of development. It's often said that Unity is a 'develop once, deploy everywhere tool. This slogan can conjure up the unfortunate image that, after a game is made, it'll work just as effortlessly on every platform supported by Unity as it does on the desktop.

Unfortunately, things are not so simple; games that work well on desktop systems don't necessarily perform equally well on mobiles and vice versa. This is largely due to the great differences in target hardware and industry standards that hold between them. Due to these differences, I'll focus our attention here to the Windows and Mac desktop platforms, ignoring mobiles and consoles and other platforms. To create a build for desktop platform, select File | Build Settings from the File menu:

Building

Figure 2.36: Accessing the Build Settings for the project

The Build Settings dialog is displayed and its interface consists of three main areas. The Scenes In Build list is a complete list of all scenes to be included in the build, regardless of whether the gamer will actually visit them in the game. It represents the totality of all scenes that could ever be visited in the game.

In short, if you want or need a scene in your game, then it needs to be in this list. Initially, the list is empty. See Figure 2.37:

Building

Figure 2.37: The Build Settings dialog

You can easily add scenes to the list by simply dragging and dropping the scene asset from the Project panel to the Scenes In Build list. For the coin collection game, I'll drag and drop the Level_01 scene to the list. As scenes are added, Unity automatically assigns them a number, depending on their order in the list. 0 represents the topmost item, 1 the next item, and so on. This number is important insofar as the 0 item is concerned. The topmost scene (scene 0) will always be the starting scene. That is, when the build runs, Unity automatically begins execution from scene 0. Thus, scene 0 will typically be your splash or intro scene. See Figure 2.38, Adding a level to the Build Settings dialog:

Building

Figure 2.38: Adding a level to the Build Settings dialog

Next, be sure to select your target platform from the Platform list at the bottom left-hand side of the Build Settings dialog. For desktop platforms, choose PC, Mac & Linux Standalone, which should be selected by default. Then, from the options, set the Target Platform drop-down list to either Windows, Linux, or Mac OS X, depending on your system. See Figure 2.39:

Building

Figure 2.39: Choosing a target build platform

If you've previously been testing your game for multiple platforms or trying out other platforms such as Android and iOS, the Switch Platform button (at the bottom left of the Build Settings dialog) might become active when you select the Standalone option. If it does, click on the Switch Platform button to confirm to Unity that you intend building for the selected platform. On clicking this, Unity may spend a few minutes configuring your assets for the selected platform:

Building

Figure 2.40: Switching platforms

Before building for the first time, you'll probably want to view the Player Settings options to fine-tune important build parameters, such as game resolution, quality settings, executable icon, and information, among other settings. To access the Player Settings, you can simply click on the Player Settings button from the Build dialog. This displays the Player Settings in the Object Inspector. The same settings can also be accessed via the application menu by navigating to Edit | Project Settings | Player. See Figure 2.4:

Building

Figure 2.41: Accessing the Player Settings options

From the Player Settings options, set Company Name and Product Name as this information is baked and stored within the built executable. You can also specify an icon image for the executable as well as a default mouse cursor, if one is required. For the collection game, however, these latter two settings will be left empty. See Figure 2.42:

Building

Figure 2.42: Setting a publisher name and product name

The Resolution and Presentation tab is especially important as it specifies the game screen size and whether a default splash screen (Resolution dialog) should appear at the application startup. From this tab, ensure that the Default Is Full Screen option is enabled, meaning that the game will run at the complete size of the system's screen as opposed to a smaller and movable window. In addition, enable the Display Resolution Dialog drop-down list. See Figure 2.43. When this is enabled, your application will display an options screen at startup, allowing the user to select a target resolution and screen size and customize controls.

For a final build, you'll probably want to disable this option, presenting the same settings through your own customized options screen in-game instead. However, for test builds, the Resolution dialog can be a great help. It lets you test your build easily at different sizes:

Building

Figure 2.43: Enabling the Resolution dialog

Now you're ready to make your first compiled build. So, click on the Build button from the Build Settings dialog or else, choose File | Build & Run from the application menu. When you do this, Unity presents you with a Save dialog, asking you to specify a target location on your computer where the build should be made. Select a location and choose Save, and the build process will be completed. Occasionally, this process can generate errors, which are printed in red in the Console window. This can happen, for example, when you save to a read-only drive, have insufficient hard drive space, or don't have the necessary administration privileges on your computer. However, generally, the build process succeeds if your game runs properly in the editor. See Figure 2.44:

Building

Figure 2.44: Building and running a game

After the Build is completed, Unity generates new files at your destination location. For Windows, it generates an executable file and data folder. See Figure 2.45. Both are essential and interdependent. That is, if you want to distribute your game and have other people play it without needing to install Unity, then you'll need to send users both the executable file and associated data folder and all its contents.

Building

Figure 2.45: Unity builds several files

On running your game, the Resolution dialog will show, assuming that you Enabled the Display Resolution Dialog option from the Player Settings. From here, users can select the game resolution, quality, and output monitor and configure player controls:

Building

Figure 2.46: Preparing to run your game from the Resolution dialog

On clicking the play button, your game will run by default in fullscreen mode. Congratulations! Your game is now completed and built and you can send it to your friends and family for play testing! See Figure 2.47:

Building

Figure 2.47: Running the coin collection game in fullscreen mode

But wait! How do you exit your game when you're finished playing? There's no quit button or main menu option in the game. For Windows, you just need to press Alt + F4 on the keyboard. For Mac, you press cmd + Q and for Ubuntu, it's Ctrl + Q.