0% found this document useful (0 votes)
6 views

Unit 3_introduction to Unity

Unity game engine introduction
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit 3_introduction to Unity

Unity game engine introduction
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIT 3 INTRODUCTION TO UNITY

Overview of Unity: Windows, Interface, Navigation, Terminology, Game Objects,


Hierarchy, Parenting Objects - Asset Store, Importing Plug-ins - Terrain Creation,
Materials, Colors , and Transparency - Introduction to Mono behaviours: Awake, Start, and
Update

1) Overview of Unity:
What is Unity
 Unity is an engine for creating games on multiple platforms.
 Unity was released by Unity Technologies in 2005.
 The focus of Unity lies in the development of both 3D and 2D games and
interactive content.
 Unity now supports 27 different target platforms for deploying.
 The most popular platforms are Android, PC, and iOS systems.
Unity is an integrated platform that is used as a gaming engine and framework.
Unity allows you to develop once and publish everywhere.
Although unity is considered to be more appropriate for creating 3D games, it can
also be equally used to develop 2D games.
In Unity, it is possible to develop games with heavy assets without depending on the
additional frameworks or engines. It really enhances the experience of users.
With the help of Unity, our game developers can access a wealth of resources like
intuitive tools, ready-made assets, clear documentation, online community, etc. free of
cost for creating exciting 3D contents in the games.
Asset tracking and rendering, scripting are some of the features of Unity game
development that we use in reducing time and cost.
2)Factors that Enhances the Efficiency of Unity

Unity Multiplayer: Multiplayer experience in unity is unparalleled. Unity enables the


users to play for the traffic that uses the relay servers and matchmakers.

IDE: Unity provides the text editor for writing the code. To reduce confusion,
sometimes, a distinct code editor is also used by our developers. As the IDE (Integrated
development editor) of the unity engine supports C# and Unity Script (JavaScript), we use
it in our game development process for creating immersive and exciting games.
Debugging: Debugging and Tweaking is extremely easier with Unity game
development. During the gameplay, the game variables are displayed, and it allows the
developers to debug the process at run-time.

Unity Analytics: The built-in analytics of Unity offers indispensable insights


regarding your game. It is necessary during the release of a game. Information related to the
distribution of the game and feedback of the players can be easily obtained through unity
analytics.

Graphics: The unity engine provides high-quality visual effects and audio. The visuals
developed by Unity are adaptable on every device and screen without any compromise or
distortion with the quality of the images.
3)Features of Unity 3D
Here is a list of some of the numerous features of Unity from the technical point of view.
These are:
 Creating and Destroying Game Objects
 Access the Components
 Events for Game Object
 Dealing with Vector Variables and Timing Variables
 Physics Oriented Events
 Coroutine and Return Types

Unity 3D: Interface/Window


 The main editor window of Unity, you will notice that the window of Unity 3D is
made up of small individual windows that can be rearranged, grouped in sets,
detached from one position and docked back again, within the main window.
 This shows that the editor's appearance can differ from project to project and from
developer to developer.
Unity 3D: The Project Window
It will display the library of assets which are available for use and images, music files and
other additional files you will implement in your project. Assets appear here when you import
them within your project.

in Project view, just above the Assets, there is a Favourites section where you can select and
keep items which are frequently used by you.
The Create option in the top-left corner can be used to add new Assets to your project.
Unity 3D: The Scene View
It allows developers to have a visual navigation and editing capability for your scene, that
you are creating. This view has the capability to show a 2D as well as 3D view, based on the
project type you are working on. You can move the objects in this view to position them
correctly.
Unity 3D: The Hierarchy Window
It shows a hierarchical representation of each of the available object in the scene. It reveals
the formation of how objects get attached to one another. With the whole Scene being
the Parent Object, the objects added to it becomes the Child object, this concept is also
known as Parenting in Unity world.

Unity 3D: The Inspector Window


It allows developers to inspect and analyze all the editable properties of the selected object.
Since, different objects type have dissimilar sets of properties with varying layout and
contents. For example, when you select an Asset fron the project window, the inspector
window will show all the available information about the asset, with the editable properties
available for editing.
Unity 3D: The Toolbar Window
This is the most important window in Unity Editor. On the left, it will contain the
primary tools to manipulate the scene view along with the objects contained within.

The play, pause and step controls will also be available in this Window. You can
also get access to the Unity Cloud Services using the buttons on the right side and the Unity
Account as well, plus visibility menu & the editor layout menu that will provide some
alternating layout for editor-windows.

Basically the toolbar does not come under window category.

Unity 3D: The Game View


In this window the actual Game view is rendered from the camera in the game. This allows
you to check how the actual game view will look like.

Terminology: Game Project Elements of Unity 3D


There is is set of elements (or components) which together forms a Game, developed using
Unity engine. These elements play a major role in making the game interactive as well as in
adding features that can vividly express your Game's objective. Let's discuss about these
elements and their functionality.
Assets
Assets are the representation of project item(s) which you can implement in your
game or project. An asset can be a file imported from outside of Unity, like the 3D model,
audio and sound file, images (jpeg, gif, png etc), textures or any of the other file types which
is supported by Unity. There is also a collection of asset-types which can be produced within
Unity.

Examples of such assets are:


 Animator Controller
 Audio Mixer
 Render Texture
 Pictures
 Animation files

The Project
Project in Unity is a folder or location which holds your complete game project along with all
its associated assets which may contain the library and assets sub-folder also.

Packages
It is a precompiled cluster of game assets. Unity comes with various packages.
Game Object
Every object present within the game is a Game Object. Technically, they don't append any
functionality to your game project but merely acts as holders for components like
the Transform, Light, Script, and Rigid Body components. You will learn about them in
the coming chapters, with practical implementation in the form of small projects.

Components
Components are the basic building blocks i.e. the nuts & bolts of objects and their activities
in a game. They act as functional pieces for each Game Object. By default, every Game
Objects have a Transform Component set automatically, because, it dictates where the
Game Object is positioned within the Unity environment, and how it gets rotated and scaled.

Scenes
A Scenes can be defined as the base or the parent object, where you can place your Game
Objects to make a level of the game. One or more scenes (aka, levels) are generally put into a
game and they are linked together, which your audience will cross or pass by clearing some
specific objectives. All these objectives and the logic of the game will be put in methods
which will run along with the player within the scene.

Prefab
Prefabs are the reusable Game Object components which are laid up in the Project View
window. Prefabs can be introduced into any number of scenes, as many times as you want,
per scene. You can create instance of these prefabs which are linked to the main Prefab. It
doesn't matter how many instances exist in your project; when you do any changes to the
Prefab, you can visualize the change applied to all other instances. You will work with
prefabs in the later chapters, while developing small game projects.
Build
It is an exported adaptation of your game that will contain all the essential scenes for
playback on the specific platform.

Transforms and Object Parenting

In Unity, the Transform component has three visible properties - the position, rotation, and
scale. Each of these properties has three values for the three axes. Means, Transform is used
to determine the Position, Rotation, and Scale of each object in the scene. Every GameObject
has a Transform.

Properties

Position: This is the position of the transform in X, Y, and Z coordinates. 2D games


generally do not focus on the Z-axis when it comes to positioning. The most frequent use of
the Z-axis in 2D games is in the creation of parallax.

Rotation: This property defines the amount of rotation (measured in degree) an object is
rotated about that axis with respect to the game world or the parent object.

Scale: The scale of the object defines how large it is when compared to its original or native
size. For example, let us take a square of 2x2 dimensions. If the square is scaled against the
X-axis by 3 and the Y-axis by 2 we will get a square of size 6x4.

These properties are measured relative to the transform's parent. If the transform has no
parent, the properties are calculated in world space.

Object Parenting

In Unity, GameObjects follow a Hierarchy system. Using this hierarchy 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.
A parent object causes all children objects to move and rotate the same way the parent object
does, although moving children objects does not have any effect on the parent. Children
themselves can be parents; e.g., your hand is the child of your arm, and fingers are children of
your hand.

Parenting GameObjects has a number of uses. For example, all the different parts of a tank
could be separate GameObjects, parented under a single GameObject named "tank." Hence,
when this "tank" parent GameObject moves, all the parts move along with it because their
positioning is updated constantly according to their parent.

Internal Assets
Along with the external assets that you import from other programs such as image, audio files,
3D models, etc., Unity also offers the creation of internal assets. These assets are formed
within Unity itself, and as such, do not need any external program to create or modify.
Let's see some examples of internal assets:
o Scenes: These act as levels.
o Animations: These contain data for the animation of GameObject.
o Materials: These are used to define how lighting affects the appearance of an object.
o Scripts: The code which will be written for the GameObjects.
o Prefabs: These act as a blueprint for GameObjects so they can be generated at
runtime.
To create an internal asset, go to the Assets folder and right-click on that folder and
select Create.
In this example, we will create a Triangle and a Square.
To create a Triangle asset, right click on the asset -> Create -> Sprites -> Triangle
Repeat the same process for Square, and now you should have two new graphic assets.
As we move along, we will explore more of these internal assets, since they are very crucial
to build a proper game.
Add a Plugin to Your Unity Project
To add a Unity package to your project, follow these steps:
1. In your Unity development environment, open the Project tab.
2. In the left pane of the Project tab, right click the Assets folder, then
select Import Package → Custom Package… in the context menu. An Import
package… window opens.
3. In the Import package… window, navigate to the folder containing the file with
the .unity package extension that represents the plugin you wish to import.
4. Select the .unitypackage file, and click Open. An Importing package window
opens.
5. In the Importing package window, review the list of files to be imported and
check for conflicts with files already in your project. If a conflict exists, save any
local changes somewhere outside of your project.
6. Click Import. The package's files are added to the Assets folder.
7. If you saved any local changes outside of your project, use your favorite file
merging software to merge the differences between the file(s) you imported and
the file(s) you saved.
Terrain Creation

1.Getting Set Up With Your Project

To begin, create an empty scene in a newly created Universal Render Pipeline


(URP) Project. You are able to work with the Terrain Editor, however, in
either of the 3D, HDRP, or URP Project templates.
Note that the Terrain Editor is unavailable in the 2D Project template. It is
encouraged that you practice within your own saved Project, otherwise go
ahead and create a new URP project from the Unity Hub and follow along
with this tutorial. You shall begin by creating a new Terrain GameObject.

2.Creating a Terrain
To begin using the Terrain Editor in Unity, you need to first create a Terrain
GameObject.
1. From the top menu dropdown, select: GameObject > 3D Object > Terrain.
This will add a new Terrain GameObject into your Scene.

Creating the Terrain also adds a corresponding Terrain Asset to the Project window.
Select image to expand
Your Terrain will be a large, flat plane. When the Terrain GameObject is selected in the
Hierarchy window, the Terrain tools appear in the Inspector.
Select image to expand
The five-icon toolbar in the Terrain component in the Inspector provides options to create
neighbor Terrains, sculpt features into the landscape, and paint texture maps onto your
Terrain. You can add trees as well as additional details such as grass, flowers, and rocks. You
are also able to change general settings for the selected Terrain.

Select image to expand


3.Creating Neighbor Terrains
Your Terrain’s default size has a square mesh resolution, where the length and width
= 1000 meters. Supposing you want to expand your terrain using a tiled grid system, you can
do so using the Create Neighbor Terrains button.
1. First select the Create Neighbor Terrains button, which is the first button in the Terrain
toolbar in the Inspector. Next, hover your mouse in the Scene window, and you will see a
new grid appear, showing 4 quadrants, outlining to the North, South, East, and West of the
Terrain.

Select image to expand


The Create Neighbor Terrains button showing the outlined quadrants for the single Terrain.

2. In the Scene window, select any adjacent quadrant outlines to add a neighboring Terrain.
With each newly created Terrain, an accompanying GameObject is incrementally created in
the Hierarchy.

Select image to expand


With this technique, you can create a vast range of Terrain tiles, spanning across an area as
wide or as narrow as your project requires. For this tutorial, the neighboring Terrains is set to
an even grid of 2 x 2, or 2000 x 2000 meters.
Select image

Color and Transparency

A Standard Shader material with default parameters and no values or text The Albedo Color parameter is highlighted.
Assigned.
The Albedo parameter controls the base color of the surface.

A range of black to white albedo values

Specifying a single color for the Albedo value is sometimes useful, but it is far more common
to assign a texture map for the Albedo parameter. This should represent the colors of the
surface of the object. It’s important to note that the Albedo texture should not contain any
lighting, since the lighting will be added to it based on the context in which the object is seen.

Two examples of typical Albedo texture maps. On the left is a texture map for a character
model, and on the right is a wooden crate. Notice there are no shadows or lighting highlights.
Transparency

The alpha value of the Albedo colour controls the transparency level for the material. This
only has an effect if the Rendering Mode for the material is set to one of the transparent mode,
and not Opaque. As mentioned above, picking the correct transparency mode is important
because it determines whether or not you will still see reflections and specular highlights at
full value, or whether they will be faded out according to the transparency values too.

A range of transparency values from 0 to 1, using the Transparent mode suitable for realistic transparent objects

When using a texture assigned for the Albedo parameter, you can control the transparency of
the material by ensuring your albedo texture image has an alpha channel. The alpha channel
values are mapped to the transparency levels with white being fully opaque, and black being
fully transparent. This will have the effect that your material can have areas of varying
transparency.

An imported texture with RGB channels and an Alpha Channel. You can click the RGB/A
button as shown to toggle which channels of the image you are previewing.
The end result, peering through a broken window into a building. The gaps in the
glass are totally transparent, while the glass shards are partially transparent and the frame is
fully opaque.

Unity - Materials and Shaders


Every beautiful looking game contains a different variety of surfaces. Like metal,
plastics, holograms, alien artifacts, and so on. Specifically, physical based Rendering.
Rendering in Unity uses Shaders, Materials, and Textures. And three of them have a close
relationship.
Materials
In Unity 3D, a Material is a file that contains information about the lighting of an
object with that material.
A material has nothing to do with collisions, mass, or even physics in general. It is
simply used to define how lighting affects an object with that material.
In unity, Materials are not much more than a container for shaders and textures that
can be applied to models. Most of the customization of Materials depends on which shader is
selected for it, although all shaders have some common functionality.
Let's create our new material, for that, first of all, create a new 3D project in Unity.
Then right-click on the Assets, and go to Create -> Materials and give a name like 'My
Material.'
See the properties of Material.
These properties are not that we have studied so far. That is because these are the properties
that are programmed in the shader, not in the material.
Materials are what make our objects visible in the first place. In fact, even in 2D, we use a
unique material that does not require lighting as well.
Shaders
A shader is a program that defines how every single pixel is drawn on the screen.
Shaders are not programmed in a C# or even in an object oriented programming language at
all. Shaders are programmed in a C-like language called GLSL. This language can give direct
instructions to the GPU for fast processing.
Shader's scripts have mathematical calculations and algorithms for calculating the
color of each pixel rendered, based on the lighting input and the material configuration.
If the texture of a model specifies what is drawn on its surface, the shader is what determines
how it is drawn. In other words, we can say that a material contains properties and textures,
and shaders dictate what properties and textures a material can have.
Textures
Textures are flat images that can be applied to 3D objects. Textures are responsible
for models being colorful and interesting instead of blank and boring.
It looks strange to think that a 2D image can be applied to a 3D model, but it is a very
simple and straight forward process once you are familiar with it. Let's see one simple
example: think about a water bottle, if you have taken off the label of a water bottle, you
would see that it is a flat piece of paper. That label is like a texture. After the label is printed,
it is then wrapped around the 3D bottle to provide a more pleasing look.
Just all other assets, adding texture to a Unity is very easy. To create texture in Unity,
simply create a folder for your textures; a good name would be Textures. Then download and
drag any texture you want in your project into the Textures folder you just created. That's it.
To create a folder, right click on the Assets and go to Create -> Folder. Rename it to
'Textures.' Now download any texture and drag it to that folder.

4) Introduction to Mono behaviours:


The Mono Behaviour class is the base class from which every Unity script derives, by
default. When you create a C# script from Unity’s project window, it automatically inherits
from Mono Behaviour, and provides you with a template script. See Creating and Using
scripts for more information on this.
The MonoBehaviour class provides the framework which allows you to attach your
script to a GameObject in the editor, as well as providing hooks into useful Events such
as Start and Update.

Description

Awake is called when the script instance is being loaded.

Awake is called either when an active GameObject that contains the script is
initialized when a Scene loads, or when a previously inactive GameObject is set to
active, or after a GameObject created with Object.Instantiate is initialized.

Use Awake to initialize variables or states before the application starts.


Unity calls Awake only once during the lifetime of the script instance. A script's
lifetime lasts until the Scene that contains it is unloaded.

If the Scene is loaded again, Unity loads the script instance again, so Awake will be
called again. If the Scene is loaded multiple times additively, Unity loads several
script instances, so Awake will be called several times (one on each instance).

For active GameObjects placed in a Scene, Unity calls Awake after all active
GameObjects in the Scene are initialized, so you can safely use methods such
as GameObject.FindWithTag to query other GameObjects.

The order that Unity calls each GameObject's Awake is not deterministic. Because of
this, you should not rely on one GameObject's Awake being called before or after
another (for example, you should not assume that a reference set up by one
GameObject's Awake will be usable in another GameObject's Awake).

Instead, you should use Awake to set up references between scripts, and use Start,
which is called after all Awake calls are finished, to pass any information back and
forth.

Awake is always called before any Start functions. This allows you to order
initialization of scripts. Awake is called even if the script is a disabled component of
an active GameObject.

Awake can not act as a coroutine.


Note: Use Awake instead of the constructor for initialization, as the serialized state of
the component is undefined at construction time. Awake is called once, just like the
constructor.

using UnityEngine;
public class ExampleClass : MonoBehaviour
{
private GameObject target;

void Awake()
{
target = GameObject.FindWithTag("Player");
}
}
An inactive Game Object can be activated when Game Object. Set Active is called on it.

The following two example scripts Example1 and Example2 work together, and illustrate
two timings when Awake() is called.
To reproduce the example, create a scene with two GameObjects Cube1 and Cube2.
Assign Example1 as a script component to Cube1, and set Cube1 as inactive, by unchecking
the Inspector top-left check box (Cube1 will become invisible).

Assign Example2 as a script component to Cube2, and set Cube1 as its GO variable.
Enter Play mode: pressing the space key will execute code in Example2.Update that activates
Cube1, and causes Example1.Awake() to be called.

using UnityEngine;

// Make sure that Cube1 is assigned this script and is inactive at the start of the game.

public class Example1 : MonoBehaviour


{
void Awake()
{
Debug.Log("Example1.Awake() was called");
}

void Start()
{
Debug.Log("Example1.Start() was called");
}

void Update()
{
if (Input.GetKeyDown("b"))
{
print("b key was pressed");
}
}
}

Example2. This causes Example1.Awake() to be called.

The Space key is used to perform this:

using UnityEngine;

public class Example2 : MonoBehaviour


{
// Assign Cube1 to this variable GO before running the example
public GameObject GO;

void Awake()
{
Debug.Log("Example2.Awake() was called");
}

void Start()
{
Debug.Log("Example2.Start() was called");
}

// track if Cube1 was already activated


private bool activateGO = true;

void Update()
{
if (activateGO == true)
{
if (Input.GetKeyDown("space"))
{
Debug.Log("space key was pressed");
GO.SetActive(true);
activateGO = false;
}
}
}
}

The MonoBehaviour class provides access to a large collection of event messages, which
allows you to execute your code based on what is currently happening in your project. Here
are a few of the more common examples. For a list of them all, see the Messages section on
the MonoBehaviour script reference page

Start - called when the GameObject begins to exist (either when the Scene is loaded, or the
GameObject is instantiated).

Update - called every frame.

You might also like