Book Image

Unity Android Game Development by Example Beginner's Guide

By : Thomas James Finnegan
Book Image

Unity Android Game Development by Example Beginner's Guide

By: Thomas James Finnegan

Overview of this book

Powerful and continuing to grow, the mobile market has never been bigger and more demanding of great games. Android continues to prove itself as a strong contender in this challenging market. With Unity 3D, great games can be made for Android quickly and easily. With its great deployment system, the Android platform is now only one click away. Unity Android Game Development by Example Beginner's Guide dives straight into making real, fully-functional games, with hands-on examples and step-by-step instructions to give you a firm grounding in Unity 3D and Android. Everything necessary for creating a complete gaming experience is covered and detailed throughout the course of this book. Using clear and practical examples that progressively build upon each other, this book guides you through the process of creating games in Unity for Android. Start by learning about all the great features that Unity and Android have to offer. Next, create a Tic-Tac-Toe game while learning all about interfaces. After that, learn about meshes, materials, and animations with the creation of a tank battle game. You will then learn how to expand your game's environment with the addition of shadows and a skybox. Adding on this, you will also learn how to expand the tank battle by creating enemies and using path finding to chase the player. Next, explore touch and tilt controls with the creation of a space fighter game. Then, learn about physics while recreating the most popular mobile game on the market. You will then expand the space fighter game with the addition of all the special effects that make a game great. Finally, complete your experience by learning the optimization techniques required to keep your games running smoothly. While Unity is available for both Mac and Windows, the book is presented working from a Windows environment. Programming in Unity is possible in C#, JavaScript, and Boo. This book will be working in C# and the final projects will be provided in C# and JavaScript. From nothing to a fully-featured mobile game, Unity Android Game Development by Example Beginner's Guide takes you through everything it takes to create your next game for the Android platform.
Table of Contents (17 chapters)
Unity Android Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Hello World


To make sure everything is set up properly, we need a simple application to test with, and what better to do that with than a Hello World application?

  1. The first step is pretty straightforward and simple; start Unity.

  2. If you have been following along so far, once it is done you should see a screen resembling the next screenshot. As the tab might suggest, this is the screen through which we open our various projects. Right now, though, we are interested in creating one, so select Create New Project from the second tab on top, and we will do just that.

  3. Use the Browse button to select an empty folder to keep your project in. Be sure that the folder is empty, because Unity will delete everything in it before creating the new project. Ch1_HelloWorld_CS works well for a project name.

  4. For now we can ignore the packages. These are bits of assets and functionality provided by Unity. They are free for you to use in your projects.

  5. Hit the Create button, and Unity will create a brand new project for us.

  6. The default layout of Unity contains a decent spread of windows needed to create a game.

    • Starting from the left-hand side, Hierarchy contains a list of all the objects that currently exist in our scene. They are organized alphabetically and are grouped under parent objects, if any.

    • Next to that is the Scene view. This window allows us to edit and arrange objects in the 3D space. In the top-left hand side, there are two groups of buttons. These affect how you can interact with the Scene view.

    • The button on the far left that looks like a hand lets you pan around when clicking and dragging with the left-mouse button.

    • The next button over, the crossed arrows, lets you move objects around. Its behavior and the gizmo it provides will be familiar if you have made use of any modeling programs.

    • The third button changes the gizmo to rotation. It allows you to rotate objects.

    • The fourth button is for scale. It changes the gizmo as well.

    • The second to last button toggles between Pivot and Center. This will change the position of the gizmo used by the last three buttons to be either at the pivot point of the selected object, or at the average position point of all the selected objects.

    • The last button toggles between Local and Center. This changes whether the gizmo is orientated parallel with the world origin or rotated with the selected object.

    • Underneath the Scene view is the Game view. This is what is currently being rendered by any cameras in the scene. It is what the player will see when playing the game and is used for testing your game. There are three buttons that control the playback of the Game view in the upper-middle section of the window.

    • The first is the Play button. It toggles the playing of the game. If you want to test your game, press this button.

    • The second is the Pause button. While playing, pressing this button will pause the whole game, allowing you to take a look at the game's current state.

    • The third is the Step button. When paused, this button will let you progress through your game one frame at a time.

    • On the right-hand side is the Inspector window. This displays information about any object that is currently selected.

    • In the bottom left-hand side is the Project window. This displays all of the assets that are currently stored in the project.

    • Behind it is the Console. It will display debug messages and compile errors, warnings, and runtime errors.

  7. At the top, underneath Help is an option called Manage License.... By selecting this, we are given options to control the license. The button descriptions cover what they do pretty well, so we will not cover them in more detail at this point.

  8. The next thing we need to do is connect our optional code editor. At the top, go to Edit followed by Preferences..., which opens the following window:

  9. By selecting External Tools on the left, we can select other software to be used for managing asset editing.

  10. If you do not desire to use MonoDevelop, select the drop-down list to the right of External Script Editor and navigate to the executable of Notepad++, or the other code editor of your choice.

  11. Your Image application can also be changed here to Adobe Photoshop CS3 or any other image-editing program you prefer, in the same way as the script editor.

  12. If you installed the Android SDK to the default location, do not worry about it. Otherwise, click on Browse... and find the android-sdk folder.

  13. Now, for the actual creation of this application, right-click inside of your Project window.

  14. From the new window that pops up, select Create and C# Script from the menu.

  15. Type in a name for the new script, HelloWorld will work well, and hit Enter twice: once to confirm the name and once to open it.

    Tip

    Because this is the first chapter, it will be a simple Hello World application. Unity supports C#, JavaScript, and Boo as scripting languages. For consistency, this book will be using C#. If you, instead, wish to use JavaScript for your scripts, copies of all of the projects can be found with the other resources for this book and an _JS suffix for JavaScript.

  16. Every script that is going to attach to an object inherits from MonoBehaviour. JavaScript does this automatically, but C# scripts must define it explicitly. However, as you can see from the default code in the script, we do not have to worry about setting this up initially; it is done automatically. Inheriting from MonoBehaviour lets our scripts access various values of the game object, such as the position, and lets the system automatically call certain functions during specific events in the game, such as the Update cycle and the GUI rendering.

  17. For now we will delete the Start and Update functions that Unity insists on including in every new script. Replace them with a bit of code that simply renders the words Hello World in the top-left corner of the screen. You can now close the script and return to Unity.

    public void OnGUI() {
      GUILayout.Label("Hello World");
    }
  18. Drag the HelloWorld script from the Project window and drop it on the Main Camera object in the Hierarchy window. Congratulations, you have just added your first bit of functionality to an object in Unity.

  19. If you select the Main Camera in Hierarchy, then Inspector will display all of the components attached to it. At the bottom of the list is your brand new HelloWorld script.

  20. Before we can test it, we need to save the scene. To do this, go to File at the top, and select Save Scene. Give it a name of HelloWorld and hit Save.

  21. You are now free to hit the Play button in the upper-middle section of the editor and witness the magic of Hello World.

  22. We now get to build the application. At the top, select File followed by Build Settings....

  23. By default the target platform is PC. Under Platform, select Android and hit Switch Platform in the bottom-left corner of the Build Settings window.

  24. Underneath the Scenes In Build box, there is a button labeled Add Current. Click on it to add our currently opened scene to the build. Only scenes that are in this list and checked will be added to the final build of your game. The scene with the number zero next to it will be the first scene loaded when the game starts.

  25. There is one last thing to change before we can hit the Build button. Select Player Settings... at the bottom of the Build Settings window.

  26. The Inspector window will open Player Settings for the application. From here we can change the splash screen, icon, screen orientation, and a handful of other technical options.

  27. At the moment, there are only a few options that we care about. At the top, Company Name is the name that will appear under the information about the application. Product Name is the name that will appear underneath the icon on your Android device. You can largely set these to anything you want, but they do need to be set immediately.

  28. The important setting is Bundle Identifier underneath Other Settings. This is the unique identifier that singles out your application from all other applications on the device. The format is com.CompanyName.ProductName, and it is good practice to use the same company name across all of your products. For this book, we will be using com.TomPacktAndBegin.Ch1.HelloWorld for Bundle Identifier, opting to use an extra dot for organization.

  29. Go up to File and click on Save again.

  30. Now you can hit the Build button in the Build Settings window.

  31. Pick a save location and a file name; Ch1_HelloWorld.apk works well. Be sure to remember where it is, and hit Save.

  32. If, during the build process, Unity complains about where the Android SDK is, select the android-sdk folder inside the location where it was installed. The default would be C:\Program Files\Android\android-sdk for 32-bit Windows and C:\Program Files (x86)\Android\android-sdk for 64-bit Windows.

  33. Once the loading bar finishes, which should not be very long, your apk has been made and we are ready to continue.

  34. We are finished with Unity for this chapter. You can close it down and start a command prompt.

  35. Just as we did when we were connecting our devices, we need to navigate to the platform tools folder in order to connect to our device. If you installed it to the default location, use:

    1. For 32-bit Windows:

      cd c:\program files\android\android-sdk\platform-tools
      
    2. For 64-bit Windows:

      cd c:\program files (x86)\android\android-sdk\platform-tools
      
  36. Double-check to make sure that the device is connected and recognized using:

    adb devices
    
  37. Now we install the application. This command tells the system to install an application on the connected device. The -r indicates that it should override if an application is found with the same Bundle Identifier as the application we are trying to install. That way you can just update your game as you develop, rather than uninstalling before installing the new version each time you need to make an update. The path to the .apk file that you wish to install is shown in quotes as follows:

    adb install -r "c:\users\tom\desktop\packt\book\ch1_helloworld.apk"
    
  38. Replace it with the path to your apk file; capital letters do not matter, but be sure to have all the correct spacing and punctuations.

  39. If all goes well, the console will display an upload speed when it has finished pushing your application to the device and a success message when it is finished installing. The most common causes for errors at this stage are not being in the platform-tools folder when issuing commands and not having the correct path to your .apk file surrounded by quotes.

  40. Once you have received your success message, find the application on your phone and start it up.

  41. Now, gaze in wonder at your ability to create Android applications with the power of Unity.

What just happened?

We created our very first Unity and Android application. Admittedly, it was just a simple Hello World application, but that is how it always starts. Also, it served very well for double-checking the device connection and for learning about the build process without all of the clutter from a game.

Have a go hero – working ahead

Try changing the icon for the application. It is a fairly simple procedure that you will undoubtedly want to perform as your game develops. How to do this was mentioned earlier in this section. But, as a reminder, take a look at Player Settings. Also, you will need to import an image. Take a look under Assets, in the menu bar, to know how to do this.