{"id":82980,"date":"2019-01-16T20:08:25","date_gmt":"2019-01-16T20:08:25","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=82980"},"modified":"2021-07-29T19:44:09","modified_gmt":"2021-07-29T19:44:09","slug":"introduction-to-mobile-development-with-unity","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/introduction-to-mobile-development-with-unity\/","title":{"rendered":"Introduction to Mobile Development with Unity"},"content":{"rendered":"<p>Two things were introduced in 2008 almost at the same time. The iOS and Google Play Store opened up the world of mobile gaming. This new world introduced a new way to play games, all on your smartphone. Since then, it has only gotten easier to create your own apps and release them on a store for all to download and play instantly. It should come as no surprise then that Unity can be used to easily create your own mobile projects. Whether you intend to make a game for yourself or for the world, for iOS or Android, you can make that vision come to life with Unity.<\/p>\n<p>But where do you get started? The same place any creator should begin\u2014with the basics. Follow along and you&#8217;ll create a basic app using Unity that does three simple tasks. The first objective is to tap on an object to make it change color. Next, a button will be created that the user may tap whenever they wish to lock the game camera in place. Why would you lock the camera? Well, for the final objective you will be rotating the in-game camera using your phone&#8217;s gyroscope. That&#8217;s right, by moving your phone in real life the camera will move in your game. All these tasks combine to create a simple app that introduces you to the basics of creating a mobile game in Unity.<\/p>\n<h2>Setup<\/h2>\n<p>Creating the project is like any other Unity project. Being a mobile project does not change this part of the process. After opening Unity, create a new project as shown in Figure 1.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"126\" class=\"wp-image-82981\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image.jpeg\" \/> Figure 1: Creating a new project<\/p>\n<p>Next, name this <em>MobileIntro<\/em>. Make sure the project is a <em>3D<\/em> project then choose the <em>Location<\/em> for the project. Once you&#8217;ve done all this, you&#8217;re ready to create the project. It should look similar to Figure 2.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"572\" class=\"wp-image-82982\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-1.jpeg\" \/> Figure 2: Naming and creating the project.<\/p>\n<p>You may wish to start by creating the objects that will be important to this project. Start by creating a cube. This will be the object the user can tap to change its color to a random color. To create this object, click the <em>Create <\/em>button in the <em>Hierarchy <\/em>window. Then navigate to <em>3D Object-&gt;Cube <\/em>to create the cube object as shown in Figure 3.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"416\" height=\"387\" class=\"wp-image-82983\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-2.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 3: Creating a cube.<\/p>\n<p>The cube is rather small at the moment, and anyone that&#8217;s done virtually anything with a mobile app knows how difficult it is to tap small objects. You&#8217;ll want to increase the cube&#8217;s size. Click on the <em>Cube <\/em>object in the <em>Hierarchy<\/em>. After this, navigate to the <em>Inspector <\/em>window and find the <em>Transform <\/em>component. This should be the first component you see at the very top of the <em>Inspector <\/em>window. Once found, change all variables in the <em>Scale <\/em>fields to four as shown in Figure 4.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"607\" height=\"176\" class=\"wp-image-82984\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-3.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 4: Naming the object and setting the scale.<\/p>\n<p>Next, you&#8217;ll create two more objects. The first is simply a manager that will hold the script that makes the project run. The second will be the user interface (UI) that the user can interact with to lock the game&#8217;s camera to its current position. Using the same <em>Create <\/em>menu in the <em>Hierarchy <\/em>window, create an empty game object and call it <em>GameManager<\/em>. Then, once again using the <em>Create <\/em>menu, navigate to <em>UI-&gt;Button <\/em>to create the button that the user will tap to perform the camera locking function. When finished, the <em>Hierarchy <\/em>should look similar to Figure 5 below.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"276\" class=\"wp-image-82985\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-4.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 5: The current hierarchy.<\/p>\n<p>You may need to adjust the position of the button you just created to get it in exactly the position you want. Select the <em>Button <\/em>object in the <em>Hierarchy <\/em>window and go to the <em>Transform <\/em>component in the <em>Inspector<\/em>. Set the anchor to the top left part of the screen. Then set <em>Pos X <\/em>to 50 and <em>Pos Y <\/em>to -40. Finally, change the <em>Width<\/em> and <em>Height<\/em> of the button to 100 as shown in Figure 6.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"230\" class=\"wp-image-82986\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-5.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 6: Setting the anchor, position, width, and height.<\/p>\n<p>Next, you will want to change the text of the button to say <em>LOCK. <\/em>In the <em>Hierarchy<\/em> window, click the arrow next to the <em>Button <\/em>object as shown in Figure 7.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"191\" height=\"160\" class=\"wp-image-82987\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-6.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 7: Opening the child objects.<\/p>\n<p>The button&#8217;s <em>Text <\/em>object will appear. Select it and navigate to the <em>Text <\/em>component in the <em>Inspector <\/em>window. You&#8217;ll then need to change the text to say <em>LOCK<\/em>, then adjust the size of the text using <em>Font Size<\/em>. When finished, the component should look like Figure 8 below.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"235\" class=\"wp-image-82988\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-7.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 8: Setting the text and font size.<\/p>\n<p>With this completed, all the objects needed for the project are in place, but it&#8217;s not quite time to write code yet. As this is intended to be a mobile project, you will need to perform a few actions to make the program run on your mobile device. From the top menu select <em>File-&gt;Build Settings<\/em> as shown in Figure 9.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"271\" height=\"296\" class=\"wp-image-82989\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-8.jpeg\" \/><\/p>\n<p><strong>Figure 9: Opening the build settings.<\/strong><\/p>\n<p>This opens up the Build Settings dialog which shows you the many platforms where you could run an app created in Unity. For each platform, however, different modules will need to be installed. Figure 10 shows the dialog and the message you\u2019ll see about installing the module for Android development.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"607\" class=\"wp-image-82990\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-132.png\" \/><\/p>\n<p><strong>Figure 10: The <em>Build Settings <\/em>without having the necessary modules installed.<\/strong><\/p>\n<p>This is where you would go to build your project to be run on whichever device you choose. In addition, you can navigate to the player settings and change some properties of your build such as the image your application will use. Depending on how you installed Unity you may also need to install the Android or iOS modules for your copy of Unity. You can use the same Unity installer you used to install Unity onto your machine to do this. Just be sure to only select the Android and\/or the iOS module for installation.<\/p>\n<p>You may also need to download an SDK for your project to work. This example will be looking at how to install the Android SDK in particular. This can be accomplished either through command line tools or the Android Studio application. Instructions for both methods can be found in Unity&#8217;s documentation, found <a href=\"https:\/\/docs.unity3d.com\/Manual\/android-sdksetup.html\">here<\/a>.<\/p>\n<p>If you already have the above completed, then you&#8217;ll need to let Unity know that this is a mobile project. Once again, the example figures will show selecting Android devices as the platform of choice, but you should just as easily be able to use this project on an iOS device. Select the appropriate mobile device for you, then near the bottom of the <em>Build Settings <\/em>window you will need to click <em>Switch Platform<\/em> as shown in Figure 11.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"604\" class=\"wp-image-82991\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-9.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 11: Switching platforms.<\/p>\n<p>Once this is done, Unity will rebuild the project and set the target platform to your choice. You&#8217;ll see the Unity logo will be next to the platform you selected once the process is complete. Now you&#8217;ll need to change a few of the player settings for your project. To the immediate right of the <em>Switch Platform <\/em>button (now grayed out), click <em>Player Settings<\/em> shown in Figure 12.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"604\" class=\"wp-image-82992\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-10.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 12: Platform selected. Now on to player settings.<\/p>\n<p>The <em>Inspector window <\/em>will change showing which player settings you can change for your project. You can do a lot here such as change the app icon or API settings. For this project you will start by changing the orientation of the app. Click <em>Resolution and Presentation, <\/em>then find the <em>Orientation <\/em>settings. This project will assume you set the orientation to <em>Landscape Right<\/em>. Figure 13 shows the settings.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"453\" class=\"wp-image-82993\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-11.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 13: Setting your app&#8217;s default orientation.<\/p>\n<p>After this, search for <em>Other Settings<\/em>. In order to test your project on your phone later, you will need to set up a package name for the project. Under <em>Other Settings <\/em>you will need to locate the <em>Package Name <\/em>field. The required format for this package name is <em>com.CompanyName.PackageName<\/em>. Leave the <em>com <\/em>part of the name and change the <em>CompanyName <\/em>and <em>PackageName<\/em>. It can be set to whatever you wish, but the example below in Figure 14 will simply use <em>com.mycompany.mobileintro<\/em> as the package name for the project.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"244\" class=\"wp-image-82994\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-12.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 14: Creating a package name for apk file.<\/p>\n<p>Your project&#8217;s mobile specific settings have now been configured, and it is time to write the project&#8217;s code. Right click in the <em>Assets <\/em>window and select <em>Create-&gt;C# Script<\/em> as shown in Figure 15.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"348\" class=\"wp-image-82995\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-13.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 15: Creating a C# script.<\/p>\n<p>Name this script <em>PlayScript<\/em>. Once you&#8217;re done, the <em>Assets <\/em>window should look similar to Figure 16.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"100\" class=\"wp-image-82996\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-14.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 16: The <em>Assets <\/em>window with <em>PlayScript<\/em><\/p>\n<p>Double click the script to open it up in Visual Studio.<\/p>\n<h2>The Code<\/h2>\n<p>Thankfully, no additional using statements are needed to make this project. The line <code>using UnityEngine<\/code> should already be in the code upon creation. In addition, you&#8217;ll need to add <code>using UnityEngine.UI<\/code> in order to change the text of the UI button. Finally, you&#8217;ll need to declare some variables that will relate to three objects in the project. These objects are the in-game camera, the text in the button, and the cube you created.<\/p>\n<pre class=\"lang:c# theme:vs2012\">public GameObject mainCamera;\r\npublic MeshRenderer cubeRender;\r\npublic Text buttonText;<\/pre>\n<p>After this, a few private variables will be declared. One of them is of the type <code>Gyroscope<\/code>. As you may have guessed, this has to do with your phone&#8217;s internal gyroscope that will be used to move the in-game camera. The next variable, called <code>rotMultiplier<\/code>, is used to increase how much the camera moves when you rotate your phone. By default, it&#8217;s not going to move very far. With this variable you can increase the amount the camera moves whenever the gyroscope detects rotation. In this example the value of <code>rotMultiplier<\/code> is fifty, but you may need to adjust this number in your own project. Finally, a <code>boolean<\/code> is created that will tell the project whether to lock the camera or not.<\/p>\n<pre class=\"lang:c# theme:vs2012\">private Gyroscope gyro;\r\nprivate float rotMultiplier = 50;\r\nprivate bool lockCam = false;<\/pre>\n<p>Now, you move on to the <code>Start<\/code> function. Whenever the game starts, there will be a quick check to see if the phone you&#8217;re using has a gyroscope. Most mobile devices these days have gyroscopes, so why even have the check? This is primarily done to enable the gyroscope functionality for Unity. If for some reason, a mobile device lacks a gyroscope, this could prevent the project from crashing. In that event, you would most likely desire to add an <code>else<\/code> statement after the <code>if<\/code> statement and perform anything you wish the project to do in the event that the gyroscope is unavailable. However, this example will assume that your device has a gyroscope, and that no further code is needed. In the interest of good coding habits, the if statement will be used in this example.<\/p>\n<pre class=\"lang:c# theme:vs2012\">if (SystemInfo.supportsGyroscope)\r\n{\r\n\tgyro = Input.gyro;\r\n\tgyro.enabled = true;\r\n}<\/pre>\n<p>The <code>Start<\/code> function is complete and the code should look similar to Figure 17.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"375\" height=\"302\" class=\"wp-image-82997\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-15.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 17: Variable declarations and <em>Start <\/em>function.<\/p>\n<p>Next, you&#8217;ll need to move on to the <code>Update<\/code> function. Here you will code in the functionality to detect when the cube has been tapped on as well as the camera rotation. The code that catches when the cube has been tapped is as follows:<\/p>\n<pre class=\"lang:c# theme:vs2012 \">if (Input.GetMouseButtonDown(0))\r\n{\r\n\tRay ray = mainCamera.GetComponent&lt;Camera&gt;().ScreenPointToRay(\r\n                 new Vector3(Input.GetTouch(0).position.x, \r\n                 Input.GetTouch(0).position.y, 0));\r\n\tRaycastHit raycastHit;\r\n\tif (Physics.Raycast(ray, out raycastHit))\r\n\t{\r\n\t\tfloat r, g, b;\r\n\t\tr = Random.Range(0.0f, 1.0f);\r\n\t\tg = Random.Range(0.0f, 1.0f);\r\n\t\tb = Random.Range(0.0f, 1.0f);\r\n\t\tcubeRender.material.color = new Color(r, g, b);\r\n\t}\r\n}<\/pre>\n<p>That&#8217;s curious. Why are you looking for mouse input? Here&#8217;s a handy little tip about Unity. You can use some of the same code that you would use for both games that run on a desktop computer as well as games that run on mobile devices. While you could specifically search for a tap of the screen, you may find it easier to simply look for a mouse click. Plus, if you aim to make a project that can work on both PC and mobile, that&#8217;s less code to change for each version. This can also be helpful when testing the program on a desktop computer before performing further testing on mobile.<\/p>\n<p>You check to see if a <em>mouse click<\/em> has occurred. Once you do that, a <code>raycast<\/code> is created at the point where the <em>mouse cursor<\/em> was located at the time. A <code>raycast<\/code> can be thought of as an arrow being shot forward. If it hits something, the program can then gain information on what was hit and even change the object in question upon impact. A good example of this lies in nearly every first-person shooter game. Whenever you fire a gun, you&#8217;re not really shooting a bullet out of a gun. Rather, the game is firing a <code>raycast<\/code> from the gun. Depending on what it hits, a certain action will occur whether that be damaging an enemy or leaving a hole in the wall.<\/p>\n<p>In this case, whenever a <code>raycast<\/code> hits your cube, the cube will get a new random color applied to it. After getting three random numbers representing red, green, and blue, you get the <em>color <\/em>variable from <code>cubeRender<\/code> and assign the new color. After these, you create the code that controls the camera movement. These next two lines of code are fairly simple. If the <code>lockCam<\/code> variable is false, then rotate the camera based on the inputs from the gyroscope.<\/p>\n<pre class=\"lang:c# theme:vs2012 \">if (lockCam == false)\r\n   mainCamera.transform.rotation = \r\n       Quaternion.Euler(\r\n            gyro.attitude.x * rotMultiplier, \r\n            gyro.attitude.y * rotMultiplier, 0);<\/pre>\n<p>With this code entered into the <code>Update<\/code> function, you can now move on. The code should look like Figure 18.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1114\" height=\"402\" class=\"wp-image-82998\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-16.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 18: The <em>Update <\/em>function.<\/p>\n<p>Before exiting Visual Studio, there&#8217;s one last function to be created. This function, called <code>LockCamera<\/code>, will be put to use in the UI button you made earlier. Whenever you tap that button, this function will be called. All it does is set <code>lockCam<\/code> to true or false and changes the text in your button. Placed underneath the <code>Update<\/code> function, the code looks like this:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void LockButton()\r\n{\r\n\tif (lockCam == false)\r\n\t{\r\n\t\tlockCam = true;\r\n\t\tbuttonText.text = \"UNLOCK\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tlockCam = false;\r\n\t\tbuttonText.text = \"LOCK\";\r\n\t} \r\n}<\/pre>\n<p>And with that out of the way, the code for this project is complete. Save your work and head back to the Unity editor to finish the project.<\/p>\n<h2>Completing the Project<\/h2>\n<p>Remember the <em>GameManager <\/em>object from before? You&#8217;ll attach your freshly made script to this object. With <em>GameManager <\/em>selected in the <em>Hierarchy<\/em>, click and drag <em>PlayScript <\/em>from the assets window into the <em>Inspector<\/em> as shown in Figure 19.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"573\" class=\"wp-image-82999\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-17.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 19: Attaching <em>PlayScript <\/em>to the <em>GameManager<\/em><\/p>\n<p>Next, you&#8217;ll need to fill in the <em>Main Camera <\/em>and <em>Cube Render <\/em>fields. In the <em>Hierarchy<\/em>, select the <em>Main Camera <\/em>object and drag it into the <em>Main Camera <\/em>field. Likewise, drag <em>Cube <\/em>into <em>Cube Render. <\/em>Figure 20 shows how this looks.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"329\" class=\"wp-image-83000\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-18.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 20: Setting the <em>Main Camera, Button Text, <\/em>and <em>Cube Render <\/em>variables.<\/p>\n<p>Next, locate the <em>Button <\/em>object in the <em>Hierarchy<\/em>. After selecting it, find the <em>On Click <\/em>event list in the <em>Inspector<\/em> as shown in Figure 21.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"255\" class=\"wp-image-83001\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-19.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 21: List of <em>On Click <\/em>events, currently empty.<\/p>\n<p>Click the <em>+ <\/em>icon to add a new <em>On Click <\/em>event to your button shown in Figure 22.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"209\" height=\"255\" class=\"wp-image-83002\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-20.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 22: Adding an <em>On Click <\/em>event.<\/p>\n<p>After this you will need to click and drag <em>GameManager <\/em>from the <em>Hierarchy <\/em>into the<em> Object <\/em>field. Figure 23 shows you what to do.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"924\" height=\"569\" class=\"wp-image-83003\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-21.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 23: Setting <em>GameManager <\/em>as the object to pull code from.<\/p>\n<p>Once this is complete, click the drop down menu that current says <em>No Function<\/em>. Navigate to <em>PlayScript-&gt;LockButton <\/em>to set the <em>LockButton <\/em>function to this button&#8217;s <em>On Click <\/em>event as shown in Figure 24.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"418\" height=\"476\" class=\"wp-image-83004\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-22.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 24: Setting <em>LockButton <\/em>as the function to call when your button is tapped.<\/p>\n<p>Two steps remain before you can test your project out properly. The necessary modules and SDK you need for the project have been installed. Unfortunately, the SDK doesn&#8217;t do much good if Unity doesn&#8217;t know where it is. Go to <em>Edit-&gt;Preferences <\/em>to take care of that as shown in Figure 25.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"322\" height=\"552\" class=\"wp-image-83005\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-23.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 25: Accessing Unity Preferences.<\/p>\n<p>This opens the <em>Unity Preferences<\/em> dialog where you adjust settings such as the colors Unity uses. For this project, you&#8217;ll want to navigate to <em>External Tools<\/em> shown in Figure 26.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"537\" height=\"397\" class=\"wp-image-83006\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-24.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 26: Navigating to <em>External Tools<\/em>.<\/p>\n<p>Scroll down until you find settings for Android. This is where you specify the file path for your SDK as well as the JDK (Java Development Kit) and Android NDK. You can also download the things you need using the convenient <em>Download <\/em>button. For mobile projects, you&#8217;ll at least want the SDK and JDK filled in. After installing everything you need, use the <em>Browse <\/em>button and find the path to your SDK and JDK as shown in Figure 27.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"395\" height=\"185\" class=\"wp-image-83007\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-25.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 27: Specifying a path for the Android SDK and JDK.<\/p>\n<p>There&#8217;s another step involved to make this project run on your phone, but it takes place outside of Unity. You&#8217;ll need to enable developer mode on your mobile device in order to build the project to your phone. On Android, you simply need to navigate to <em>Settings-&gt;About phone<\/em>-&gt;Software Information and then find the <em>Build Number<\/em>. Tap <em>Build Number <\/em>multiple times to enable developer mode on your phone. For iOS, the process is a little different. You&#8217;ll need <em>Xcode<\/em> running on your desktop, followed by plugging a USB cable from your phone into the computer. From there, navigate your settings until you find <em>Developer<\/em>. Finding this means you have developer mode enabled on your phone.<\/p>\n<p>After all the above steps have been completed, your project is now ready, but how do you test this out on your phone? After all, it&#8217;s hard to test gyroscope functionality on a desktop computer. You can get around this by navigating to <em>File-&gt;Build Settings<\/em>. Once there, you&#8217;ll need to click the <em>Build and Run <\/em>button near the bottom of the window shown in Figure 28.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"605\" class=\"wp-image-83008\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-26.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 28: Building your project and running it on your mobile device.<\/p>\n<p>This will build your game and then upload it to your phone. From there the program should begin running on your phone. Of course, you will need a USB cable connected from your computer to your mobile device for this method to work. Another window will pop up before the build begins saying you need to save the <em>apk<\/em> file somewhere on your computer. You may save this file wherever you wish, as it will not affect building the <em>apk<\/em> to your phone. In addition, it&#8217;s possible that a dialogue box with a message about JDK will pop up saying it found an up to date version. Go ahead and click <em>Yes <\/em>on this dialogue.<\/p>\n<p>Once the build is complete, you should see your project begin to play on your phone. If for some reason it doesn&#8217;t play automatically, try searching around your apps and see if <em>MobileIntro <\/em>is anywhere to be found. Once the game begins, go ahead and rotate the device and notice the in-game camera move with your phone&#8217;s movements. If there&#8217;s a certain angle you like, you can tap the UI button near the top right to lock the camera in place. Finally, try tapping the cube and watch it change to a random color. Remember when I said that you can look for mouse input when looking for taps on your phone screen? You can see that in action both by tapping the cube and tapping the UI button. The app will look something like Figure 29.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"873\" height=\"383\" class=\"wp-image-83009\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/01\/word-image-27.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 29: Mobile project in action.<\/p>\n<h2>Conclusion<\/h2>\n<p>As you can see, creating a mobile project is easier than you might think. If you ever wanted to create another mobile game in the future, some settings you edited here in Unity would carry over into other projects, namely the path to the SDK. In some cases, there isn&#8217;t much difference between code for a mobile project and code for a PC project, so controlling different versions of the same app becomes easier too. However, it would have been a little underwhelming if all you did was make code that works on both PC and mobile, so you took advantage of your mobile device&#8217;s gyroscope to move the camera in time with your own movements. From here you can build off this example project to create your own mobile experience, whatever that may be.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today\u2019s world, some people do not need computers because they can do everything on their smartphones. Playing games is no different and is one of the most popular uses for the mobile devices (who makes phone calls, anymore?). In this article, Lance Talbert shows you how to create a game that will run on an Android device.&hellip;<\/p>\n","protected":false},"author":317499,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538,53],"tags":[95509],"coauthors":[52549],"class_list":["post-82980","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","category-featured","tag-standardize"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/317499"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82980"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82980\/revisions"}],"predecessor-version":[{"id":83011,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82980\/revisions\/83011"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82980"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}