Unity – Introduction Unity is a cross-platform game engine initially released by Unity Technologies, in 2005. The focus of Unity lies in the development of both 2D and 3D games and interactive content. Unity now supports over 20 different target platforms for deploying, while its most popular platforms are the PC, Android and iOS systems. Unity features a complete toolkit for designing and building games, including interfaces for graphics, audio, and level-building tools, requiring minimal use of external programs to work on projects. In this series, we will be − Learning how to use the various fundamentals of Unity Understanding how everything works in the engine Understanding the basic concepts of game design Creating and building actual sample games Learning how to deploy your projects to the market Let us now get started.
Category: unity
Unity – Internal Assets Alongside the external assets that you import from other programs such as audio files, images, 3D models, etc., Unity also offers the creation of Internal assets. These assets that are created within Unity itself, and as such do not need any external program to create or modify. A few important examples of internal assets are as shown below − Scenes − These act as “levels”. Animations − These contain data for a gameObject’s animations. Materials − These define how lighting affects the appearance of an object. Scripts − The code which will be written for the gameObjects. Prefabs − These act as “blueprints” for GameObjects so they can be generated at runtime. A few other important assets are Placeholder, Sprites and Models. These are used when you need quick placeholders so they may be replaced with proper graphics and models later. To create an internal asset, right-click in the Assets folder and go to Create. In this example, we will create a Triangle and a Square. Scroll over the Sprites selection and click on Triangle. Repeat the process for Square, and you should have two new graphic assets. As we move along, we will explore more of these internal assets, since they are crucial to building a proper game.
Unity – Creating Sprites Sprites are simple 2D objects that have graphical images (called textures) on them. Unity uses sprites by default when the engine is in 2D mode. When viewed in 3D space, sprites will appear to be paper-thin, because they have no Z-width. Sprites always face the camera at a perpendicular angle unless rotated in 3D space. Whenever Unity makes a new sprite, it uses a texture. This texture is then applied on a fresh GameObject, and a Sprite Renderer component is attached to it. This makes our gameObject visible with our texture, as well as gives it properties related to how it looks on-screen. To create a sprite in Unity, we must supply the engine with a texture. Let us create our texture first. Get a standard image file such as a PNG or JPG that you want to use, save it, and then drag the image into the Assets region of Unity. Next, drag the image from the Assets into the Scene Hierarchy. You will notice that as soon as you let go of the mouse button, a new GameObject with your texture’s name shows up in the list. You will also see the image now in the middle of the screen in the Scene View. Let us consider the following points while creating a sprite − By dragging from an external source into Unity, we are adding an Asset. This Asset is an image, so it becomes a texture. By dragging this texture into the scene hierarchy, we are creating a new GameObject with the same name as our texture, with a Sprite Renderer attached. This sprite renderer uses that texture to draw the image in the game. We have now created a sprite in our scene. In the next lesson, we will look at some modifiers for the sprites we have.
Unity – Transforms and Object Parenting When we just got started, we discussed how a gameObject’s transform is arguably its most important component. Let us discuss the component in detail in this chapter. Additionally, we will also learn about the concept of Object Parenting. Transforms have three visible properties − the position, the rotation, and the scale. Each of these have three values for the three axes. 2D games usually do not focus on the Z-axis when it comes to positioning. The most common use of the Z-axis in 2D games is in the creation of parallax. The rotation properties define the amount of rotation (in degrees) an object is rotated about that axis with respect to the game world or the parent object. The scale of an object defines how large it is when compared to its original or native size. For example, let us take a square of dimensions 2×2. If this square is scaled against the X-axis by 3 and the Y-axis by 2, we will have a square of size 6×4. In our subsequent section, we will discuss what Object Parenting is. What is Object Parenting? In Unity, objects follow a Hierarchy system. Using this system, GameObjects can become “parents” of other GameObjects. When a GameObject has a parent, it will perform all its transform changes with respect to another GameObject instead of the game world. For example, an object with no parent placed at (10, 0, and 0) will be at a distance of 10 units from the game world’s centre. However, a gameObject with a parent placed at (10, 0, 0) will consider the parent’s current position to be the centre. GameObjects can be parented simply by dragging and dropping them onto the desired parent. A “child” object is depicted in the object list with a small indentation along with an arrow next to the parent object. Parenting GameObjects has a number of uses. For example, all the different parts of a tank could be seperate GameObjects, parented under a single GameObject named “tank”. That way, when this “tank” parent GameObject moves, all the parts move along with it because their positioning is updated constantly according to their parent. In our subsequent lesson, we will discuss the internal assets. We will also learn how to create and manage the assets in our project.
Unity – Modifying Sprites The sprite we have just imported can also be manipulated in various ways to change how it looks. If you look at the top left corner of the engine’s interface, you will find a toolbar as shown below − Let us discuss the functions of these buttons. The Hand tool is used to move around the scene without affecting any objects. Next, we have the Move tool. This is used to move objects in the game world around. In the centre, we have the Rotate tool, to rotate objects along the Z-axis of the game world (or parent object). The Scaling tool is positioned upwards. This tool lets you modify the size (scale) of objects along certain axes. Finally, we have the Rect tool. This tool behaves like a combination of the Move and the Scaling tool, but is prone to loss of accuracy. It is more useful in arranging the UI elements. These tools prove worthy as the complexity of the project increases.