
Time for action – build and run
With this alternate build method, we can quickly and easily test games on our devices as follows:
- Start by opening the Build Settings window. Remember, it can be found under File at the top of the Unity Editor.
- Click on the Add Current button to add our current scene, also the only scene, to the list of Scenes In Build. If this list is empty, there is no game.
- Be sure to change your Platform to Android, if you haven't already done so. It is after all still the point of this book.
- Do not forget to set the Player Settings. Click on the Player Settings button to open them up in the Inspector window. Do you remember this from Chapter 1, Saying Hello to Unity and Android.
- At the top, set the Company Name and Product Name fields. Values of
TomPacktAndBegin
andCh2 TicTacToe
respectively will match the included completed project. Remember, these are seen by the people playing your game. - The Bundle Identifier field under Other Settings needs to be set as well. The format is still
com.CompanyName.ProductName
, socom.TomPactAndBegin.Ch2.TicTacToe
will work well. - So that we can see our cool dynamic GUI in action on a device, there is one other setting that should be changed. Click on Resolution and Presentation to expand the options.
- We are interested in Default Orientation. The default is Portrait, but this option means the game will be fixed in the portrait display mode. Click on the drop-down menu and select Auto Rotation. This option tells Unity to automatically adjust the game to be upright in whichever orientation the device is being held.
- The new set of options that popped up when Auto Rotation was selected allow for the limiting of the orientations that are supported. Perhaps you are making a game that needs to be wider and held in landscape orientation. By unchecking Portrait and Portrait Upside Down, Unity will still adjust (but only for the remaining orientations). On your Android device, along one of the shorter sides, are the controls of some sort usually a home, menu, back, and search set of buttons. This side is generally recognized as the bottom of the device and it is the position of these buttons that dictates what each orientation is. Portrait is when those buttons are down relative to the screen. Landscape Right is when they are to the right. The pattern begins to come clear, does it not?
- For now, leave all of the orientation options checked and we will go back to Build Settings.
- The next step (and this is very important) is to connect your device to your computer and give it a moment to be recognized. If your device is not first connected to your computer, this shorter build path will fail.
- In the bottom-right corner of the Build Settings window, click on the Build And Run button. You will be asked to give the application file, the APK, a relevant name and save it to an appropriate location. A name
Ch2_TicTacToe.apk
will be fine and it is suitable enough to save the file to the desktop. - Click on Save and sit back to watch the wonderful loading bar that is provided. If you paid attention to the loading bar when we built the Hello World project in Chapter 1, Saying Hello to Unity and Android, you will notice that there is an extra step taken this time around. After the application is built, there is a pushing to device step. This means the build was successful and Unity is now putting the application on your device and installing it. Once done, the game will be started on the device and the loading bar will be finished.
What just happened?
We just learned about the Build And Run button provided by the Build Settings window. Quick, easy, and free from command prompt pain; isn't the short build path wonderful? But if the build process fails for any reason including being unable to find the device, the application file will not be saved. You will have to sit through the entire build process again, if you want to try installing again. This isn't so bad for our simple Tic-tac-toe game, but might consume a lot of time for a larger project. Also, you can only have one Android device connected to your computer while building. Any more and the build process is a guaranteed failure. Unity also doesn't check for multiple devices until after it has gone through the rest of the potentially long build process.
Other than those words of caution, the Build And Run option is really quite nice. Let Unity handle the hard part of getting the game to your device. This gives us much more time to focus on testing and making a great game.
Have a go hero – single player mode
This is a tough one. Create a single player mode. You will have to start by adding an extra game state. Is the choice of MultiPlayer
for a game state starting to make sense? The opening screen is going to need an extra button for selecting the second game mode. Also, any logic for the computer player should go in the Update
function that is provided by the MonoBehaviour
class. The computer needs to take its turn before we check for victory in LateUpdate
. The Update
function is just the place to do it. Also, take a look at Random.Range
for randomly selecting a square to take control of. Or, you could do a little more work and make the computer search for a square where it can win or create a line of two matches.