Creation of applications for Android. Using Unity to develop applications Creating a 3d game for android

Development mobile applications is one of the most profitable occupations in the computer industry. Creating an Android game costs hundreds or thousands of dollars, and the profit can reach up to a million dollars. In this regard, many people are interested in developing applications for the phone. In this article, you will learn how to create an Android game from scratch using a computer, which engine and designer is better to choose.

None successful game can not do without beautiful graphics, so the creation of design is one of the most important stages of development. Design on the Android OS is implemented using a “design document” or design document. You need to start creating a toy with its detailed study. The file contains:

  1. Object models;
  2. Functional specifications;
  3. Game content;
  4. Interface.

Let's consider each of the points in more detail.

Object models

This is information about the functionality of each item. Object models are responsible for the ability to buy and sell items, as well as for improving the game characteristics of characters.

Functional specifications

It describes the gameplay, the main features of each character. It also describes the features game items- weapons, first-aid kits, armor and others. In essence, functional specifications are the rules by which the game process takes place. The better this section is worked out, the easier it will be to create a high-quality Android game.

Game content

This is the text filling of the game. It describes the dialogues of the characters and what weapons can be used to deal damage, how much health a hit will take, what characteristics will increase when using various equipment. It also contains detailed description each item.

Interface

The interface is how the user will interact with the game. It includes buttons with which you can control the character, and menu sections: for example, play, score, settings, top players, help. If you have no experience in creating Android applications, before creating your own, download from the Play Market and analyze popular games and transfer the best solutions to your project.

Game engine

The core of any game is the engine. This is software that allows you to develop and run it. It contains a whole range of programs, including a rendering engine, a physics engine, sound, animation, and much more. To facilitate the process of writing programs, third-party developers create their own game engines specifically for Android applications.

Each of them offers different features: some are designed for 3D, others for 2D, multi-platform can be supported. There are a huge number of such engines, but if you are a beginner, it is better to choose one of the most popular ones, since all the necessary functions will be present there.

UDK

Torque 2d/3d

What is a game designer?

Constructor is a program that combines a game engine and an integrated development environment. The constructor makes the development process accessible to people who do not have programming skills. Some of the designers allow you to create games of certain genres, others have maximum functionality, but cost much more money. For a novice creator of mobile applications, the choice of a designer is one of the most crucial moments, because the fate of the future application will depend on its capabilities.

The constructor allows you to create games of various genres for Windows, Android and iOS. Offers big choice ready-made locations, objects, characters and sound design, so the creation of the first Android game will not take much time. Users familiar with the JS and C++ programming languages ​​can use the embedded GML. The only drawback is that the program is not translated into Russian.

Conclusion

Creating an Android game is not an easy, but very profitable business. If you decide to develop a game and make money on it, first work out the idea. Next, write a “design document” and decide on a game engine that can maximize its potential. After that, you can proceed directly to creating the application in the constructor.

Video

To learn more about the process creating android games, check out the video series for this activity.

Unity is the engine on which most of the modern games on different platforms. Through the engine, it is possible to create games that are oriented to run on computers (Windows, Linux, MacOS), mobile phones, tablet computers (Android, iOS) and even game consoles PlayStation, Xbox, Nintendo.

How to create games in Unity?

Unity is based on a component-oriented concept. In fact, any game consists of a mass of objects diluted with additional components. For example, during the creation of a platformer, we create a GameObject, additionally attach a graphic component to it, which is responsible for displaying the character, and a control component, it provides control of the character using a mouse, keyboard, joystick or touchscreen. The engine does not impose restrictions on the number of such modules. We can add as many components to the GameObject as needed. All work on the engine is based on the very creation of GameObject and the application of suitable components to them.

Beginners may be misled by the apparent simplicity of the process, although this is not the case. To create a unique and popular game you have to write a lot of components from scratch. More specifically, in the Unity environment, this is called a script. The native scripting language is C#, but JavaScript and Boo (a cropped version of Python) are also partially used. Creating your own components is not easy, one might even say it is a complex procedure, directly tied to classical programming. Without a sufficient level of knowledge in programming, it will be hard.

Supported Platforms

Many large projects prefer Unity because of the huge list of platforms compatible with the engine. The finished application can be launched on any computer operating system, popular mobile platforms and SmartTV. What can we say, even browser games and applications for specific platforms ( like Tizen OS) are predominantly developed in Unity.

However, not everything is so smooth here. If necessary, you can write specific algorithms based on low-level code, for the same Android you need to have a thorough knowledge of Java.

The same is true for iOS. Another one iOS feature that it is possible to develop for it only from a computer or tablet from Apple. In the absence of a Macbook or similar device, the release of the game will quickly stall and Unity has nothing to do with it, Apple itself imposes a similar restriction. The conclusion is simple: if there is an iOS development in the plans, you should choose a suitable device for assembly in advance.

How and where to develop a game on the engine?

We can carry out a long process of game development both within Windows and Mac OS X. There are already versions of the editor for Linux, but so far we cannot count on their stable operation. A developer spends most of the time creating a game inside the Unity editor. We still have a lot of time to write script code, we can create it either in the standard MonoDevelop or in any third-party editor. Now they actively use Visual Studio, as well as Sublime Text, but only with proper configuration.

Hi all! Having learned how to make games on this engine, I want to teach you the same. First, we need Unity3d itself, any 3D model editor and straight arms growing from the shoulders.

1. Learning the program

For starters, I recommend installing Monodevelop! Let's understand what Unity is. Unity is a multi-platform game development environment. It is because of its simplicity and understandability that every third person has made games on it at least once.

Consider the screenshot above:

  • The stage is your creative sandbox.
  • Project - here are all the animations and models, sounds and textures.
  • To install models in the top toolbar, select Assets-Import New Asset. You can also Assets-Import Package-Custom Package. In the second case, the package must be in the .unity extension.
  • Hierarchy - all objects in the scene. There you create parents for the object. Just move an object to another in the hierarchy menu, and it will be attached. The result will be a kind of folder.
  • Toolbar - list of tools. There you can enable 2D,3D mode.
  • Inspector - a feature of an object, where you add animations, scripts and more. There you can use the tools: move the camera, move the object, stretch, rotate the object.


Now you see the object menu in it you can add animation, scripts and more.

I think that with this menu you will not have problems.
Click to create any object.

In general, we have studied the program, soon it will be possible to write our own games.

2. Create a simple game

First, let's create a Create-Terrain territory. Add grass and trees. By clicking on the ground on the right, a window will appear, select trees, grass.


Also create mountains and bumps. Then load the Characher controller pack into the project. Then we search in the window with the Standard assets-Characters controller project and select the first-person view (capsule) or third-person view (mechanic).
If the first person, then just put it on the ground. If from the third, then you need to download the animation. I also recommend setting the scene lighting to Create-Directional Light.


To add music, drag it to the desired item.
The game is ready, but how to compile it?

To do this, go to File-Build settings-player settings (for label and name). Choose a platform and voila, you're done. To do this, you need to install the Android SDK. But there you need to specify the folder with the SDK, and you need not just the SDK, but the API of the desired level (for example, Android 4.2 has API 17). For example, in order for the game to be on Android 4.0+, you need to install these APIs.

In the next article, I will tell you how to set up controls for Android or iOS and learn how to write scripts yourself.

Post Views: 19 374

We talked about how you can create your own game on Android using the Unreal Engine. In this article, we will look at another equally popular game development engine - unity.

Unity is a very famous engine among indie developers. In fact, it is a cross-platform engine that allows you to develop 3D- and 2D-games. The feature that sets Unity apart from other engines is the low entry threshold for beginners, while having a rich toolkit for professionals. Cross-platform allows you to develop applications for any platform, from desktop games to mobile ones.

Separately, it is worth mentioning the subscription system, since Unity is not a completely free product. There are several types of subscriptions:

  • Personal. Free version that contains all the main features of the engine. It has the following limitation: income per year or the amount of funds raised must not exceed 100000$ .
  • Plus. Per $35 per month various reports and analytics are provided, as well as the ability to change the splash screen, 20% -th discount on purchases in Asset Store and various minor benefits. It has the following limitation: the income for the year or the amount of funds raised must not exceed 200000$ .
  • Pro. Per $125 per month includes all the benefits of the version Plus and additionally professional service and premium support. No restrictions on turnover or amount of funds.
  • Separate versions for business (used by large companies).

Thus, indie developers with small budgets should be satisfied with the version Personal or Plus otherwise you will need to subscribe Pro. In this article, we will use free version for the first run.

Step 1Install Unity

In order to start installing Unity, you need to go to the Unity Store website, where you will be prompted to select the type of subscription. As mentioned above, we choose Personal.

You will then be prompted to accept the terms of use and download the installer. You can also check out system requirements to work with Unity.

After launching the installer, you will be prompted to select which components to install. Since we need to create an Android application, we will check the box for Android Build Support. Also, if desired, instead of with Unity, you can install Visual Studio Community 2017 for programming on C#.

After that, all that remains is to choose the installation path and start installing Unity.

Step 2: Registering and Setting Up Unity

After completing the installation and running Unity, we are prompted to log in with our account.

Then again you will be asked to select a subscription, we will also focus on Personal. In this case, you will need to confirm that the company's annual income is less than $100,000, or that Unity is used for training purposes.

At the end, it is proposed to take a small survey in which you need to answer what you do and for what purpose you install Unity.

Step 3. Create a new project

Having configured Unity, we get to the screen for selecting / creating a project. Click here New to create your new project.

After that, in the window that appears, you are asked to enter the name of the project and the directory where it will be located. Also don't forget to check the box 3D to use the editor in 3D mode.

When done with this, click Create project to create a project. After that, an editor window will open with a pre-generated scene.

Let's not deal with the intricacies of programming for now and take a finished game as an example. For this there is Asset Store, which contains a huge number of ready-made models, animations, sounds (and even games), both paid and free.

To open the Asset Store, you need in the menu Window choose Asset Store(key combination ctrl-9).

The store window will open. Enter in the search bar " flappy bird style example game" and open a free example that we will use in our project.

Clicking Download and thus having downloaded it, you can then import it into your project. To do this, click Import, after which a warning will appear that the project settings will be changed after import.

After agreeing to import, you will see a window in which you need to choose which components to import. Select all and click Import.

Once the import is complete, you will see the new files in the Project Browser. These are the game files. To open the scene in the editor, expand Flappy bird style - scenes and double click on Main.

As a result, the game scene will appear in the 3D editor window.

You can check how the game works right in Unity by clicking on the button play above the editor window.

Step 4Configure Android Tools

Note: if you are using android studio, then you already have everything installed necessary components and so you can safely move on to the next step.

In order to assemble the resulting game on Android, you need to install the following tools:

  • Java Development Kit (JDK). You can download it from the Java website. By clicking on the link, you will see at the very top Java Platform (JDK), click on Download next to the inscription, then select your operating system and start downloading. After that just follow the instructions of the installer.
  • Android SDK. by the most in a simple way to install the latest version is to download the Android Studio with which this SDK comes. To do this, go to the Android Studio website and click Download Android Studio. The Android Studio installer installs the core Android SDK components required for Android development.

Step 5. Prepare the project for launch

First, you need to change the development platform to Android. To do this, in Unity, open the File menu and select Build Settings.

In the window that appears, select Android and then press switch platform.

The platform switch tells us that we will be building an Android app. This means that when we build the application, Unity will create APK file. The platform switch also forces Unity to re-import all assets in the project. This won't take long on a small project, but keep in mind that this operation can take a long time on large projects.

Now we need to provide a package name for the application.

Note: The package name is a unique identifier for the application and is written in reverse DNS style in the format com.CompanyName.ProductName. After publishing the application in Google Play the package name can no longer be changed.

To do this, go to the menu Edit and choose Project Settings - Player.

On the right side of Unity, a window will open with many different settings, such as the application version, target and minimum SDK, icon selection, etc. Here we need in the block Other settings find Identification and set the package name in the field package name. You can also change other settings if you wish.

Now it remains only to specify the path to the Android SDK and JDK. To do this, go to the menu and select Edit-Preferences.

In the window that appears, go to External Tools and in the fields SDK and JDK specify the appropriate paths, and then close the window.

Step 6: Building and Running the Application

It's time to build your first Unity app. To do this, select from the menu File - Build Settings. A window will open, in which you will need to add the scene that you want to assemble. Since we already have this scene open in the editor, just click Add Open Scenes to add it to the assembly, after adding it you will see it in the list of scenes.

It remains only to press Build, after which Unity will start building the application. Before that, you may be asked to select a folder where the collected application files will be saved, as well as the name of the APK file.

Note: on the this moment if you have alpha version installed in Android SDK Build-Tools(version 28.0.0-rc1), when building Gradle will give an error. To fix this, simply remove this version from the Android SDK.

As a result, the built APK file will appear in the folder you specified, ready to be installed on the emulator or on a real device.

Let's see what's inside the built APK. To do this, we use the utility APK Analyzer, which is included with Android Studio,

The uncompressed APK file size is 21.1MB, in a compressed 20.9MB. As you can see from the graph, most of the volume is occupied by third-party libraries added by Unity. Then in assets all the resources that are used in the assembled scene are located. File classes.dex contains only 89 classes and 479 methods.

Moreover, if you look at AndroidManifest.xml, the application consists of one activity.

Conclusion

That's all. In this article, we learned how to install and configure Unity, and also built our first Android app.

How to quickly and relatively easily create a simple game or application for Android? Read about this in the article below, which deals with working with the Unity3D engine.

Do you play computer games? If so, then, for sure, in the process of passing the next game blockbuster, you sometimes had thoughts like: "But here, I would have done it wrong!". Do you want to be a developer yourself? Then, I offer you a small, but very entertaining "adventure";)

What is an engine and why is it needed?

Previously, back in the days of the first computers, games and programs were created by the so-called "hardcore" method. That is, for a full-fledged development, it was required to be a fairly experienced programmer who thoroughly knows his programming language and can write almost the entire game (including pixel graphics and special effects!) in a simple Notepad. There were few such people and they were highly valued (and are still valued) ...

Today, the threshold for entering the topic of programming has decreased significantly. And this is facilitated not only by the availability of literature and all kinds of training materials, but also by the increasingly simplified development process. Nowadays, it is rare to meet real "hardcore" people, since quite convenient development environments have appeared, which are called "engines".

The essence of the engine is that it allows you to import all the necessary game objects in a simplified visual mode, arrange them on a virtual stage and configure all their parameters. That is, the task of the programmer is only to write the game logic correctly!

However, you have to pay for the convenience of work with a slight increase in the size of the project (sometimes even several times!) in comparison with its counterpart, written in the old "hardcore" way. It is for this reason that some developers do not like engines or try to write their own, in which only the components they need are used ...

But you and I are far from true programmers :) Therefore, let's create a simple game for Android based on the popular and powerful Unity3D engine.

Getting started with a project in Unity3D

So, why did we decide to use Unity3D:

  • the ability to create full-fledged 2D and 3D games;
  • the ability to compile the finished game for any device;
  • relative ease of control of the engine;
  • good extensibility of functionality by connecting plugins;
  • relatively small size of the final assembly of the game (compared to other engines).

The main trump card of Unity3D, of course, is its versatility and multiplatform. Finished game(perhaps with a little tweaking) You can port to PC (Windows, Linux or MacOS), Android, PlayStation or XBox! To do this, we need to download only the engine itself, which, by the way, "weighs" more than a gigabyte and the necessary SDKs (if development is planned for mobile platforms or console). If you want to make a game for Windows, then you do not need to download anything, but since we are going to make a game for Android, we will also need to download the Android SDK (Software Development Kit). I advise you to download the SDK in a ZIP archive for greater convenience of working with it.

When everything is downloaded, you can unpack to any Android SDK folder that is convenient for you, and then proceed to install Unity3D itself. It is installed like any regular Windows program, so there should be no problems with installation. After installation, the engine will start and we will first need to create a new project:

To do this, we will need to close all welcome and introductory windows, and then select "New Project" from the "File" menu. In the window that opens (see the screenshot above), we will be prompted to specify the project save folder (its name will be the same as the project name), import some standard sets of functions (we do not need them) and specify the operating mode. Select 2D as the mode and click the "Create" button. This completes the preparatory part of the work :)

Engine interface and project hierarchy

After creating the project, it will be loaded into the Unity3D workspace:

This workspace itself has a modular structure and consists of several nested windows that can be moved around as you like. I have it set up like this (from left to right and top to bottom):

  1. Hierarchy window - displays the hierarchical position of active and inactive objects on the scene. By default, the only object immediately in the list is the Main Camera.
  2. Scene window - displays the entire game scene and objects on it (including cameras, sounds, textures, etc.).
  3. Inspector window - displays all the properties of the object selected in the hierarchy or on the stage and allows you to edit, supplement or delete them.
  4. Game window - displays the game world that is visible to the camera and allows you to select the size of the virtual game test window in pixels. Also, allows you to activate the expansion of the game window in full size (Maximize on Play) and enable / disable the display of various information for the developer.
  5. Project and Console windows. The Project window is used to navigate through project resources: uploaded images, sounds, models, and so on. Also by means of this window (context menu) you can create new empty game objects of different types and scripts. The Console window is used to display error messages (red), warnings (yellow) and various debugging information requested by you (gray).
  6. The Assets Label window is the lower part of the inspector window, which displays a preview of the object or file selected in the Project window.

In addition to all of the above, pay attention to the three buttons at the top center. They allow you to start your project, pause it (sometimes useful when debugging to see what is being done in the Hierarchy, for example) and, accordingly, turn it off.

We will create a simulator of a musical instrument: Indian folk drums called "tabla", therefore, to get started, we will create two empty folders in the project in which sounds and pictures will be stored. You can create a new folder by calling the context menu in the Project Navigator window and selecting the "Folder" item from the "Create" list.

Here it is worth saying a few words about how these folders should be named. For a simple game, like our simulator, in principle, the folders can be "named" as you like, however, there are at least two names that are better not to use, since they are reserved by the engine. These are the names Resources and Plugins. In large projects, the Resources folder is created immediately and all game "spare parts" (pictures, sounds, models, textures and prefabs) are already stored in it (sorted by subfolders). In Plugins, as the name implies, all additional third-party sets of functions (the so-called "assets" (assets)) are stored, of which there are quite a few on the Asset Store - a store for extensions for Unity3D.

Importing Files and Creating Game Objects

Before creating a new game project, it is advisable to prepare in advance all the files necessary for its operation. In our case, these files are pictures and sounds. The easiest way to import them into a project is to simply drag and drop them into the desired folder. Let's start with pictures...

After the images appear in our Images folder, they need to be tweaked a bit:

The fact is that by default Unity3D compresses all images added to the project with loss of quality. If we do not need such a loss (especially true for games with HD graphics), then we should select each image and in the Inspector window change the value of the "Format" parameter from "Compressed" to "Truecolor" in the "Default" tab. You may also need to change the "Max Size" parameter if it is smaller than the actual image width (default is 1024 pixels). After all the changes, you need to remember to click the "Apply" button to apply them. If necessary, we perform similar actions for all other images.

Now, let's create game objects from our pictures. To do this, simply drag the desired image into the Hierarchy window. The picture or 3D model will be automatically converted into a Game Object and displayed at the starting coordinate point in front of the camera.

First of all, we added a background image, which should be displayed in full screen. You can check whether this is so by selecting a camera in the hierarchy window and looking at how its frame is displayed. In the screenshot above, you can see that part of our background remains outside the camera's view, so we need to adjust the size of this view by dragging the frame directly on the stage or by editing the "Size" parameter in the Inspector window. In our case, we had to change the default value (5) to 5.4.

That's it, we don't touch the camera anymore, but add and adjust the rest of the pictures. When adding, we see that some of them are too big for our game. You can reduce them either physically in a graphics editor, or by means of the engine. Let's try the last method. To do this, select the image in the Hierarchy window, and in the Inspector window change the "Scale" values ​​for the X and Y coordinates from 1 to 0.5 (that is, reduce by half). In the same place, it is desirable, in order to avoid overlaps, to assign to all game objects (except for the background) a non-zero order of layers (Order in Layer):

The last step in preparing game objects from pictures will be "hanging" colliders on them. In Unity3D, colliders are used as interaction detectors between objects. That is, in fact, a virtual shell is put on our picture, which can register touches to it.

To add a collider to an object, select it in the Hierarchy window, and then click the "Add Component" button in the Inspector window. In the list of components that appears, we are looking for Physics 2D (ordinary 3D physics will not work in our mode) and out of all the options, "Circle Collider 2D" is the most suitable for us. We choose him. A green stroke will appear around our object, which displays the borders of the collider. It remains only to check the "Is Trigger" checkbox so that our collider only registers clicks and no longer interacts with the surrounding game space:

Similar actions are repeated for all remaining objects. Only our background will differ from other objects. For this, we'll apply a Box Collider 2D and set the Position Z to be greater than zero to push the collider and the background itself into the background (we'll need this later).

Working with sound

With sound in Unity 3D, at first glance, everything is somewhat confusing. But, let's try to figure it out in order :)

First of all, we need to drag all the sounds into the Sounds folder and tweak them a little too:

If the volume level of all your sounds is initially adjusted, then all you need to do is turn off 3D sound processing. In 3D games, this processing is needed in order to simulate the approach of the sound source when the camera approaches it. But in 2D we don't need this, so we select each sound in turn and uncheck "3D Sound" from it.

Now we can start adding sounds to game objects. To do this, we need to select each button in turn and add a new "Audio Source" component to it from the "Audio" section. When the component is added, select the corresponding "Audio Clip" button and uncheck the "Play On Awake" checkbox active by default (play when it appears):

When all the sounds are attached to the right buttons, it's time for the fun part - scripting...

First script

Before starting to parse our first script, it is probably worth saying a few words about the scripting system in Unity 3D itself.

Unity allows you to write code in three languages ​​at once: JavaScript (more precisely, its slightly modified version of UnityScript), C # and Boo. In a number of books and guides, you can find the opinion that for beginners, scripting in Unity is easier to master using JavaScript as an example. However, this language is not as comfortable as the others, and in the future you will most likely have to relearn. Therefore, it is better to start immediately by learning C # (Boo is also good, but there are few training materials on it).

Now about where to write the code. The default code editor in Unity 3D is the MonoDevelop IDE. The advantage of this development environment is that it has hints for quickly entering standard methods, classes, and functions, as well as a syntax checking system. However, for me, the disadvantage was the bulkiness and high resource intensity. Fortunately, in the Unity settings you can set anything as a text editor, so I "hung" the lightweight and functional Notepad ++ for myself (almost all examples will be shown in it).

And the last. Naturally, without reference materials in mastering all the intricacies of the engine, you will not go far, so here is a list of several sensible Internet resources on the topic of Unity 3D:

  • http://docs.unity3d.com/ru/current/Manual/UnityManualRestructured.html - official manual for the engine with partial translation into Russian (appeared recently and is sometimes unstable);
  • http://docs.unity3d.com/ru/current/ScriptReference/index.html - official scripting reference manual in English;
  • http://unity3d.ru/distribution/index.php - Russian forum for Unity 3D developers;
  • http://habrahabr.ru/post/141362/ - a series of lessons on creating three-dimensional games on Habrahabr;
  • http://msdn.microsoft.com/ru-ru/library/618ayhy6.aspx - Microsoft's official C# reference in Russian.

Well, in order not to drag it out for a long time, I propose to immediately create the first script. Under scripts, if there are a lot of them, it is better to allocate a separate folder, but our game will be simple, so we can create a script directly in the root folder of the project. To do this, call the context menu in the Project window, select the Create list and click "C # Script". Let's name our new script, for example, Sounds and open it with a double click. Having removed the standard methods Update and Start, and then, having registered the necessary ones, we get the following script:

If you have not reconfigured the editor, then our script will open in MonoDevelop. Let's take a look at its structure...

In fact, every C# script consists of three main parts:

  1. At the very top, the libraries that need to be connected for the script to work are listed (for example, "using Unity.Engine" connects the work of the engine itself, and "using System.Collections" - a library containing a number of built-in classes for the script to work under different systems).
  2. Class and variable declaration. There is no concept of a script in C# as such. Instead, the concept of a "class" is used, which includes a number of methods and functions. Although, in fact, in the context of Unity 3D, this is almost the same thing :) The main thing to remember is that the class name must match the name that we gave the script. The peculiarity of variables in C# is that they must (except for temporary ones, which are declared in the script itself) be typed. In practice, this means that the access level (private or public) and type (int, float, bool or, as we have in the script, AudioClip) must be specified for the variable. At the same time, any variable name can be given. Or you can immediately give it a value ascribe.
  3. A set of methods and functions. A method in C# is almost any function that performs certain actions. In principle, in the literature on Unity 3D they are called functions, but if we are talking about C # as a separate programming language, then these are, after all, methods :). Pure functions in Unity are, for example, standard functions for outputting sound, drawing GUI elements, etc., which are used inside methods.

Knowing now the structure of the script, it is easy to understand its meaning, which boils down to the following... In the Sounds class (script), we declare 9 variables of the AudioClip type with the names we need for the convenience of distinguishing them. We make them necessarily public (private ones will not be displayed in the editor, and we will need to "hang" sounds on them through the editor). And then in the body of the script we use the standard method "void OnMouseDown ()". Previously, it was responsible only for processing a mouse click, but in new versions it is also interpreted as a touch to the touch screen. In it we write the conditions by which we check the names of the pressed colliders and, if we detect a press of one of them, we output the corresponding sound (we use the standard audio.PlayOneShot(); function).

When writing code, be careful to include all opening and closing brackets, otherwise you will constantly receive error reports in the Console and will not be able to run your project at all! Also pay attention to the use of capital letters in the names of methods and functions. They are also often the cause of errors. And, of course, observe all signs (periods, colons and brackets), in short, the syntax of the language. At first it will be difficult, but over time you will start writing from scratch without mistakes!

When the script is ready, select it and in the Inspector window assign a different sound to each variable. Now we just have to apply our script to the objects in the scene. The easiest way to do this, again, is by dragging the script directly onto the desired game object in the Hierarchy list:

If the script was dragged correctly, then it should appear in the Inspector window next to the object on which we dragged it. Accordingly, we drag the script to all the objects that should sound in the game and we can try to run it. When you click on our game buttons, now, finally, the specified sound will sound!

Introduction to GUI

In principle, the Game is already functioning with us, but somehow it is too "bald". Something is missing in it ... Or, at least, an inscription with the name that is in front of us! In other words, you need to implement some kind of intelligible user interface.

Actually, GUI is an abbreviation for English. "Graphical User Interface", that is, a graphical user interface. Traditionally, it includes such components as:

  • buttons;
  • window;
  • text areas;
  • checkboxes and/or radio buttons;
  • sliders (scroll bars), etc.

In Unity 3D All this (and even something more than that) is fully present. The main thing is to learn how to use it! Let's take a look at how Unity's GUI can make a simple heading.

To do this, create a new script named Interface and modify it as follows:

Consider carefully the script in the screenshot. Here we write only one public variable of the GUIStyle type, which is new for us. This type allows you to visually change the appearance of the elements to which this style is assigned. Styles are useful when there are many interface elements of the same type (for example, buttons), but they should have a different look.

Next, we create a button ("GUI.Button"), which has the following syntax: condition - 4 coordinates (horizontal indent from the left edge, vertical indent from the top edge, width, height) - visible text in quotes and further, optionally, style indication. The necessary functions are written inside the button condition. In our case, we registered the transition to the developers' site ("Application.OpenURL("");").

GUI scripts can work from any game objects, but usually they are advised to "hang" them on the camera, which we will do by dragging the script onto the Main Camera object. Now, if we select it in the Hierarchy window, then in the Inspector window we will be able to see the public variable of our style, by expanding it, we will get full access to the settings of this style!

We can specify the text color and texture for the background in any situation (for the button, you need to register its three options (Normal - normal view, Hover - on hover and Active - on click), set padding and adjust the font. By default, the only one available in Unity 3D the font is Arial, however, you can manually load any other font into the project and apply it in the "Font" field, setting all related parameters.

What is interesting and at the same time not very convenient is that the GUI elements are not displayed on the stage until we start the project, so adjusting the parameters of each element can take a lot of time. Let's run the project and see what our button created above will look like:

Changing the interface by condition and static variables

We have already done a lot, but there is always room for improvement :) Therefore, I propose, as a last example, to create a help window, with which normal developers usually supply their games and applications. To do this, add a picture for such a button in the form of a question mark to the folder with images and make a game object out of it. Let's hang a collider on it and write a script with a name, for example, "Help" with the following content:

In this script, we have two variables. The first variable defines the skin that will be used for all interface elements described in this class. Unlike styles, which we discussed above, skins allow you to change the appearance of not only the individual element to which they are assigned, but all elements of a certain type at once. In addition, unlike styles, skins are not assigned to elements, but are declared immediately for the entire method and work until another skin is declared. It should also be remembered that skins exist not only as variables, but also as individual files. Therefore, before declaring a skin, you must immediately create it in the "Project" window from the context menu: "Create" - "GUI Skin".

The second variable we have is a logical (or otherwise Boolean). It can only have two values: "true" (true) or "false" (false). Due to its binary nature, such a variable is perfect for implementing all kinds of switches. In our case, it will be responsible for displaying the text describing our game.

However, if you look closely, you will notice that the last variable has one wonderful parameter - "static". Adding this parameter to variables allows you to make them static and access them from other scripts!

But back to the rest of the code. The "void OnMouseDown()" method already familiar to us here will serve as a switch for the "isVisible" variable using a simple condition. However, after the condition there is another function. This function ("Debug.Log();") is used to output "on the fly" to the Console the values ​​of the variables we need (we enter them without quotes in brackets). As you can see, inside a function, you can combine both immutable text (written in quotes) and variables (without quotes). The main thing is that there should be a "+" sign between all the data.

Further in the text of the script, we have a method for drawing the interface ("void OnGUI()()"), inside which we see the declaration of a new skin ("GUI.skin = HelpSkin;") and a new interface element of the GUI.Box type. Unlike a button, a box is an inactive element and usually serves to display various text or graphic data.

Pay attention to how we set the positioning for the box. Here, all values ​​are not specified directly, but with the help of mathematical expressions, which, in fact, is an analogue of "fluid layout" in web development. The problem is that when specifying the exact sizes of GUI elements at different screen resolutions, they will not adapt and may be either too small or, conversely, large. In the next version of the engine, the developers promise to make the GUI adaptive, but so far we have to pervert :(

The last step on the road to success will be customizing our skin. We specify the skin file in the script variable, and then select this file in the project window. As with other files, skin settings will be displayed in the Inspector window. Here we are looking for the element that is subject to change (in our case, Box), open it and set it up in the same way as the style discussed above:

To see if our script works correctly, let's run the project and try to click on the help button:

Now everything works, but it is not always convenient to press the help button to hide the description back. It is more customary to poke any notifications into any free place than aiming at the buttons. And this is where we just need to use the fact that we made the "isVisible" variable static, as well as the collider hung on the background image...

Let's go back to our very first script ("Sounds"), which tracked button presses to play sounds. It will be enough for us to write only one line into it (more precisely, inside the "void OnMouseDown ()" method:

if (this.name != "HelpButton")(Help.isVisible = false;)

With this line we set a condition: if the name of the pressed collider does not equal the name of the help button, then the "isVisible" variable in the "Help" script will equal "false". Only it is necessary that the variable "isVisible" definitely has the postscript "static", otherwise we will get an error.

Now, at startup, we will see that our help window is closed when not only clicking on the button in the upper right corner, but also when touching any free part of the screen. The only thing that can be improved is to make the window hide when you click on it... This will be your "homework";) I can only say that the GUI box will need to be changed to a similarly designed button, again, with the setting skin and prescribing a function that changes the "isVisible" variable. Everything, keep quiet :)

Compiling the game and saving the project

Finally, everything works for us! This means it's time to save our game and test it on a real device. And for this, it needs to be compiled into an executable file (for Windows, for example, in EXE, and for Android, in APK).

If you want to compile a game for PC, then you don't have to make any preliminary settings. However, for Android, you will first need to connect the Android SDK we downloaded at the very beginning. This is done quite simply: go to the "Edit" menu, call the "Preferences" item and in the window that opens, go to the "External Tools" tab. Here we are looking for the line "Android SDK Location" and in it we indicate the path to the unpacked archive with the SDK (see the screenshot below). By the way, here you can also change the monstrous MonoDevelop text editor active by default to your favorite one :)

And now the moment of truth has come! Go to the "File" menu and look for the "Build Settings" item there. In the window that opens, we will need to immediately add scenes for the assembly. If there is only one scene (as in our project), then it will be enough to click the "Add Current" button. If there are several of them, then you will need to open each of them and add them to the list of scenes in the same way, and then, by dragging and dropping, adjust their desired order:

The next step is to select the build platform (we are planning a game for Android, which means we choose it) and click the "Player Settings" button below. The Inspector window will open a number of settings, some of which are mandatory or highly desirable. It is desirable to indicate the names of the developer company, the program and its icon (Company Name, Product Name and Default Icon, respectively) at the top of the settings list.

At the bottom, in the "Other Settings" section, there are two required parameters: "Bundle Identifier" and "Bundle Version". The default version is set as 1.0, but the identifier will have to be created manually and, preferably, unique. It should consist of three parts, separated from each other by dots: the reserved word "com", then the name of the developer company, and at the end the name of the application itself.

You can change the rest of the assembly settings at your discretion or not change at all. The main thing is that now the compilation will already be able to start. To do this, just click the "Build" button in the "Build Settings" window and wait until the program gives us a ready-made application packaged in an APK file :)

Conclusion

This concludes our lesson-review on creating the simplest applications for Android. So that you can repeat everything we talked about, at the end of the article I will give a link to the archive, which will store all the resources of the game as a separate folder (if you want to do everything from scratch) and as a file with the unitypackage extension. The latter allows you to import the entire project into Unity at once, including dependencies, etc.

To import our project (and, by the way, any plugins downloaded from the Internet or the Assets Store), you need to go to the "Assets" menu, select the "Import Package" item, and in it "Custom Packege", then specify the path to our file and press the "Import" button. By the way, you can make a full backup of your project, saving it in such a unitypackage, from the same menu ("Assets") by clicking on the "Export Package" item.

It seems that everything :) As a person who himself mastered Unity 3D from scratch, in the article I tried to answer the maximum number of questions and highlight the main nuances of working with the engine that arise at first. But, perhaps, this will not be enough for someone, therefore, if you have any questions, you can contact me either directly by mail or on the forum: I will help you in any way I can!

I wish you all good luck and successful implementation of all projects!

P.S. It is allowed to freely copy and quote this article, provided that an open active link to the source is indicated and the authorship of Ruslan Tertyshny is preserved.