Unit 3_introduction to Unity
Unit 3_introduction to Unity
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
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.
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
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.
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.
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.
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
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
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
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.
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.
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.
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.
Description
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.
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.
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.
void Start()
{
Debug.Log("Example1.Start() was called");
}
void Update()
{
if (Input.GetKeyDown("b"))
{
print("b key was pressed");
}
}
}
using UnityEngine;
void Awake()
{
Debug.Log("Example2.Awake() was called");
}
void Start()
{
Debug.Log("Example2.Start() was called");
}
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).