On the Internet, there are many applications and software to support game development, among which Unity, Unreal Engine, and CryEngine are three of the most popular game development software. Among the three, Unity and Unreal Engine are more popular than CryEngine.
Check out the following Mytour article to learn how to create Android applications using Unity.
Creating Android Applications Using Unity
Follow the steps below to create an Android application using Unity.
Step 1: Setting up Unity and Android Studio
To get started, first download and install the latest version of Unity. Download and install Unity here:
Moreover, creating an application for a mobile platform requires installing the corresponding SDK for that platform. Specifically, the corresponding SDK for Android is Android Studio, and for iOS, it's Xcode.
To download and install Android Studio, visit the following link.
link: https://Mytour/download-android-studio-17597
After downloading and installing the application, the next step you need to do is to install drivers for your phone by opening Android Studio => Configure => SDK Manager => Launch Standalone SDK Manager.
At this point, a new window will appear. Here, scroll down and check Google USB and Web Drivers, then click Install Packages in the bottom right corner of the window and wait for the installation process to complete.
Next, open the Settings app on your phone => About / About Phone => Software Information. Find and tap on Build Number several times until a message 'Congratulations, you are now a developer' or similar appears on the screen.
Return to the Settings window, find and select Developer Options, then locate and check the USB Debugging option.
Connect your phone to the computer, and at this point, a message 'USB Debugging Enabled' or similar will appear on the notification bar.
Step 2: Set up the project and create Particle Prefabs
After creating the project, ensure that your assets folder contains the following directories:
- Fonts
- Materials
- Menu
- Prefabs
- Scenes
- Scripts
- Textures
If not already present, you can create these directories, then navigate to File =>Build Settings. In the window that appears on the screen, click on Android under Platforms, then click on Switch Platform in the bottom left corner. Save the scene by pressing Ctrl + S or selecting Save Scene from the File Menu. Save the scene as Main Scene in the Scenes folder.
Create Particle Prefabs
Save the image below into the Textures folder to use for the Particle System.
Set the Texture Type to Texture by clicking on the image in the Textures folder and selecting the appropriate value in the Inspector window, then click on Apply. Check the box next to the Alpha Is Transparency option.
Next, create a Particle System and set the values for the Particle System as shown in the image below.
Ensure the Texture field of the Particle System is set to Star Texture by expanding the Shader section and selecting Star Texture. Alternatively, you can drag and drop the image from the Textures folder into the box. Click on the Shader menu, choose Mobile =>Particles =>Alpha Blended.
Rename the top corner of the Inspector window to Explosion, perform the same action in the Hierarchy window. Then drag the Explosion Particle System from the Hierarchy folder into the Prefabs folder in the Project window. This operation creates a prefab of the Particle System.
Next, we'll create a Trail Prefab.
Create an Empty Game Object by right-clicking in the Hierarchy window, selecting Create Empty, naming it Trails, or similar. Add a Trail Renderer component to the game object and set the values as shown in the image below:
Create a new Material by opening the Materials folder in the project window, right-clicking, selecting Create =>Material. Name this material Trails Material or Trails Mat.
Next, click on the Trails Game Object in the Hierarchy window, drag and drop Trails Mat from the Project view into the Inspector window. This action assigns the Material to the Trails Game Object. Then drag the Trails Game Object into the Prefabs folder.
Finally, we'll create another Particle System alongside the initial Particle System, named Pinch Explosion, and assign Star Material as the Prefab for Pinch Explosion. These are all the effects we'll be using.
In the Hierarchy window, change the value of the Background Color field to Black or other dark colors and change the value of the Size from 5 to 10.
Step 3: Create and Add Effect Scripts
Create Script
Create a new C# script in the Scripts folder by opening the Scripts folder, right-clicking, selecting Create => C# Script, and naming the script SpecialEffects.
Open the script in the appropriate Integrated Development Environment (IDE) and add the following code:
Add Script to Scene
After creating the script, the next step is to add the script to the scene. Create a new Empty Game Object named Scripts, then drag the script you just created from the Scripts folder into the Scripts Game Object.
Assign variables to corresponding Prefabs by dragging the Prefab from the Prefabs folder in the Project View into the Scripts properties in the Inspector window.
Step 4: Create and Add Touch Script
Create Script
In this step, we will program touch gestures for the application. The 3 gestures we will use include tap, swipe, and pinch to zoom. You can also add other gestures if desired.
The effects we trigger depend on input gestures (tap, swipe, pinch to zoom, etc.). To detect these gestures, we use the Input class, which gathers all information about input types in one place.
Unity also includes a subclass named Input.touches, which contains events for different gesture types in a specific frame.
Touch is a container that holds all the information about different fingers.
The first touch gesture we'll implement is pinch. Simply put, pinch involves placing two fingers on the touch screen and dragging to zoom in or out. Tap is pressing on the screen for a short period of time (usually about 1 second), similar to a mouse click. Lastly, swipe gesture involves swiping a finger across the screen.
Create a new script in the Scripts folder, name it TouchControls, then enter the following code after the Update function:
For the Tap functionality, we access the position of all fingers placed on the screen, then create an Explosion effect when the finger leaves the screen.
For the Drag function, when a new variable is created, we'll adjust it to establish a connection between the finger and the Trail.
If a new touch has been created, create a new Trail and link it to the finger. As the finger moves, as long as the finger has a linked Trail, the position of the Trail GameObject will move along with the finger.
When the finger leaves the screen, the trail will be completely erased.
For the Pinch function, we'll create 3 new variables. These variables store the initial position of the fingers. Additionally, we'll create a Pinch Explosion effect to update and clear the variables.
When detecting 2 fingers on the screen, a new Pinch Explosion effect is created between the 2 positions, with the gesture declared as Pinch. If the fingers move, the Pinch Explosion GameObject also moves and shrinks.
After the Pinch Explosion effect is created, if the fingers leave the screen, the effect will transition into a Tap Explosion effect.
Add Script to Scene
After Script Controls is created, the next step is to add the script to the scene, similar to the steps above.
Step 5: Testing
Testing is one of the crucial steps in software development. Without testing, you won't know if everything is working properly or not. There are many ways to test an application or software. The most effective way is to build and run the application on an Android phone.
Go to Edit =>Project Settings =>Player. At the top of the Inspector window, you'll see Company Name and Product Name. Change them to any names you prefer. Then, expand the Other Settings tab and access Bundle Identifier, change the value to com.CompanyNameYouSet.ProductNameYouSet.
If Drivers are installed, access Build Settings from File Menu, then click Add Open Scenes. Add Main Scene to the list and set the value to 0.
Once done, ensure you've selected the platform as Android and your phone is connected to the computer, then click Build and Run. Unity will start building your application. Before completion, Unity will ask for the Android SDK location.
To find the SDK location, open Android Studio =>Configure Settings =>SDK Manager, look at the top-right corner of the window for Android SDK Location, this is the Android SDK location.
Once Unity knows the SDK location, it will proceed to build your application. After the process completes, the app will automatically run on your Android phone.
Alternatively, you can test your app differently by clicking Build instead of Build and Run and selecting the .apk file location. Once the file is created and saved, your task is to move the file to the storage on your phone and manually install it.
Another way to test the app is through the Unity Editor. By pressing the Play button at the top middle of the screen, Unity will build and run your app. However, to apply this method for testing the app on Android, we'll need to download and install Unity Remote 4, currently available on the Play Store and App Store.
Download Unity Remote 4 to your device and install it from here.
Link:
Unity Remote allows users to test their games and apps on Unity without having to create a full build each time they test. This helps save a lot of time. The biggest limitation of Unity Remote is that it reduces the graphics quality on the phone.
Above are the 3 most effective methods for testing games and apps. With these methods, you can see how the code works.
Additionally, you can also apply other methods to test your app. Another method Mytour wants to mention here is breakpoints. If any code errors are detected, breakpoints will halt the program and provide full information on the location of the error code.
To create a breakpoint, locate the line of code suspected of errors, click on the first margin on the left. You will see a red dot on the screen, hover over this red dot to find the location of the breakpoint.
In the screenshot below, the line of code 'if (explosionEffect == null' is faulty. After creating the breakpoint, in the Visual Studio window, click on Debug =>Start Debugging or press F5. This action runs the program in Unity and links it to VS. At the bottom of the VS window, you will see a red bar with a Ready message.
Access Unity, find and click on the Play button icon to run the application, game until the breakpoint redirects you to the VS window, containing information about how the program runs.
After the testing process is complete, press Shift + F5 to end the debugging process in VS or return to the Debug menu and click on Stop Debugging. This action stops the debugging process, allowing the application, program to run normally.
A small note regarding spelling and syntax. Some programming languages, such as C#, distinguish between uppercase and lowercase letters, ... .
Step 6: Create Main Menu and HUD
Creating a menu / HUD for the application is also quite important. The main menu provides users with information about the application as well as options for configuring and using the application.
In this article, Mytour will guide you on how to create a simple Menu. Specifically, on the main menu, we will add 3 buttons: Play, Help, and Exit. The Play button will redirect users to the main window of the application, game. The Help button redirects users to the help control panel, and the Exit button exits the game, application.
You can use any font, background image, ... as long as it suits the application, game.
After downloading the fonts and background images, the next step is to import the fonts and images into the Unity project in their respective folders (Fonts and Images).
Create Menu
To create the menu, first we will create a new Scene by pressing Ctrl + N or accessing File =>New Scene, then name the scene Menu or Main Menu.
After creating the scene, next we will add a Canvas. This Canvas allows us to utilize all the UI features.
To create the Canvas, right-click on Hierarchy =>UI =>Canvas. In the Inspector window of the Canvas, set the Render Mode to Screen Space - Overlay and check the Pixel Perfect option.
Under the Canvas Scaler section, change the Scale Mode from Constant Pixel Size to Scale With Screen Size. Modify the Reference Resolution value to match the phone screen resolution in landscape mode (in this case, 1920 x 1080) and change the Match value from 0 to 0.5.
The next step is to adjust the Game View value to match the Reference Resolution value of the Canvas. To do this, click on the Game tab at the top corner, or alternatively access Windows =>Game, or press Ctrl + 2. Find and select the Free Aspect option at the top corner of the Game window.
At the bottom corner, you'll see a '+' icon, click on it to change the Type value from Aspect Ratio to Fixed Resolution.
Modify the Label value to match the device screen resolution (in this case, 1080p) and change the values of Width & Height to any desired values.
Create a Panel, similar to how we created the Canvas, but select Panel instead. If Canvas is already selected, Panel will automatically become a child item of Canvas.
To differentiate between the main folder and the subfolder, the main folder will have a left-facing arrow icon, which you can click to show or hide the subfolders.
To create a subfolder, simply right-click on the object in the Hierarchy window and create a child object, then drag it onto any object.
After creation, we can set the background image for the Panel. Before starting, let's change the name of the Canvas in the Hierarchy window to Menu Canvas and rename the Panel to Background Image. Next, click on Background Image Panel.
In the Inspector window, you'll see an attached Image component. Your task is to locate and change the source image. To do this, drag and drop the image you downloaded onto the project window, or alternatively, click the small circle icon in the middle. This action opens a window called Select Sprite. Here you can choose your background image.
After selecting the background image, find and click the Color box at the bottom corner and drag the slider next to the box named A to set the value to 255.
To set up the Title, create a new Text object, name it Title, and set it as a child object of Background Image. Change the color of the Text to white, add any text you want, and use any font you prefer.
In the Paragraph section, change the Alignment value to any value. Then click on Best Fit and change the Max Size value to any value. Make sure you have selected the Best Fit option for the Text Game Objects you have created.
Next, at the top right corner, under the Rect Transform section, you'll find a box containing text, which is the Anchor. Click on Anchor, then hold down Alt + Shift and click on the middle icon.
Click on Add Component =>Shadow and Outline. Increase the Alpha value of these components to 255.
After finishing the background image setup, we'll proceed to set up the buttons. Create an Empty Game Object as a child of Background Image Panel and name it Buttons Layout Manager. In the Inspector window of the Empty Game Object, click on Add Component, search for and add Vertical Layout Group to the object.
This Game Object will arrange the buttons in the appropriate positions. Change the Anchor value to Stretch Uniform and adjust the position, axis with Anchor.
On the screen, the Buttons Layout Manager will be displayed. Drag from the top downwards until the Buttons Layout Manager appears in the grid box. If not, find and click on the Gizmos option at the top corner of the Scene window, then select Show Grid.
Create a new Text Game Object, name it Start, and set any desired value. This object is a child of Background Image.
The rule of thumb is to set the size of the buttons smaller than the Title size by about 10 or 20 pixels.
Change Text to Start and align it according to your preference. Add a Button component and change the color to any color you want for the button.
Duplicate this button twice by right-clicking on the button, selecting Duplicate, or clicking the button and then pressing Ctrl + D.
Create a new Panel named Help Panel and set the size of the Source Image similar to the Background Image. Duplicate the Title and set it as a child object of the Help Panel. Rename the Game Object to Help Panel Title and the Text to Help Panel.
Duplicate the button and name it Back, set it as a child of the Help Panel, change the Text of the button to Back. Set the anchor, position, and pivot back to bottom left, and change Pos X to 10.
Create a new Text Game Object as a child of the Help Panel, name it Help Text Body, and set the Anchor to Stretch Uniform. Change the Top box value from 0 to 10. Change the values of Left, Right, and Bottom to 10, set the font, and change the Max Size value to 80.
Next, go to File => Build Settings => Add Open Scenes and add your Menu Scene. Then drag the Menu Scene to the top corner to make its build index 0 and the Main Scene 1.
Menu Programming
Create a new Script and name it Menu, then paste the following code:
Drag the Script onto the Menu Canvas, then scroll down to the bottom corner of the Inspector window, where you'll see the following items:
- Start
- Help
- Exit
- Back
- Help Panel
Drag the related Game Objects into their respective items.
Hold Ctrl and simultaneously click the Start, Help, Exit, and Back buttons, then scroll down to the Script Button. At the bottom corner, you'll find an empty list called On Click (). Click the '+'' icon. Drag the Menu Canvas Game Object into the None (Object) box. The Menu No Function will be activated.
Click the Start button, then select the menu No Function and scroll down the list to find Menu. Click StartPress () to create the StartPress menu. Do the same for the Help, Exit, and Back buttons.
At this step, we will run a test to see if the application and buttons are functioning correctly.
Create a new Panel as a child of the Background Image and name it Exit Panel. Set the Background Color value for the new panel to match the background color and set the Top and Bottom values to 450, and Right and Left to 760.
Copy the Title and set it as a child of the Exit Panel. Reduce the text size so that it does not take up too much space.
Duplicate the Play button twice, set them as children of the Exit Panel, change the Anchor, Position & Pivot to Bottom Left, and adjust the values for other properties to Bottom Right.
Change the name of the button in the bottom right corner to Yes and the button in the bottom left corner to No.
Open the Menu Script and add 2 buttons to the Back button and GameObject within the Help Panel, using the following code:
public Button yes;
public Button no;
Additionally:
public GameObject exitPanel;
Add the following line of code into the Start function, below the line helpPanel.SetActive(false);
exitPanel.SetActive(false);
Change the ExitPress function from Application.Quit(); to exitPanel.SetActive(true);.
Next, scroll down to the bottom of the script, and add the following code into the BackPress() function:
Finally, go back to Unity, select the Yes button, and change the Function from StartPress() to YesPress(), and change the No button to NoPress(). Also, make sure you have assigned the public variables in the Menu Canvas script. Click the Play button and test the Exit button.
Create and Program HUD
Return to the Main Scene and create a new Canvas, name it HUD Canvas, and set the values similar to Main Menu Canvas.
Add a Text Game Object as a child of the Canvas, name it Back To Main Menu, set the font, size, and alignment according to your preference.
Create a new script named HUD and input the following code:
Drag the script onto HUD Canvas, set up the script similar to Main Menu, then configure the button by dragging HUD Canvas into the On Click () slot. Choose the BackToMainMenuPress function and test the button by clicking the Play button icon.
Step 7: Organize Hierarchy
Return to the Main Scene and create a new Empty Game Object named Effects. Drag all Explosions and Trails Game Objects into this Effects script. Then drag the Effects Game Object into Scripts. Use this system to organize the projects you've created.
This article from Mytour just guided you on how to create an Android application using Unity. If you have any doubts or questions that need answering, readers can leave their comments below the article.
