Final Lecture 4
Final Lecture 4
Lecture 4
Coroutines
● Typical functions in Unity and C# act synchronously. This means that, when an event (like
Start) calls a function in a class, the function performs its behavior sequentially, line by line
from top to bottom, and then finally terminates at the end, returning a value. When the
function returns, the calling event will resume its execution at the next line.
● Coroutines are like functions, but they don’t seem to act that way. They act like they are
asynchronous.
● The coroutine has the ability to pause execution before it is completed entirely and return
control to Unity but then resume where it left off on the next frame. This demonstrates the
asynchronous behavior of coroutines.
● In other words, coroutines are a way of handling tasks that execute incrementally over time.
In contrast, most functions make the program wait until they finish. 2
Coroutines
● For instance, a really slow
function, which fills the
scene with 400,000 randomly
placed cubes, might take a
while to complete.
5
Coroutines
● The yield return null line terminates
execution of the coroutine for the
current frame, but the coroutine will
resume at the next line on the next
frame.
7
Coroutines
● You will notice that the loop
counter in the Fade function
maintains its correct value
over the lifetime of the
coroutine.
8
Countdown Timer
● To create a countdown timer:
1) Create a GUIText GameObject in the
scene: RIGHT CLICK on Hierarchy view
and choose UI Text-TextMeshPro.
1) When we need a large number of copies of a single element (e.g., streetlights). Once a
streetlight prefab is designed, it can be instantiated and placed in various parts of the
scene. If we decide to change the intensity of light for all the streetlights, we can modify
the prefab, and this will cause all the instances to change.
10
Prefabs
2) When we need to generate dynamic game objects. For example, we could model an
explosive shell shot from a cannon as a prefab. Each time the cannon is shot a new
prefab shell would be instantiated (through one of your scripts). In this way each new
instance of the shell will inherit all the prefabs properties, but it will have its own
location and state.
● To create a prefab, simply drag the GameObject from the Hierarchy view and drop it into
the Project window. This will automatically save the object as a prefab.
● Prefabs can be done manually in the visual editor or spawned by using commands in
scripts.
11
Prefabs – Brick Shooter
1) Create a Cube GameObject in the scene and name
it Brick.
8) Drag the Bullet prefab into the Projectile slot of the script.
14
Prefabs – Brick Shooter
● Instantiate ( Object Prefab, Vector3 Position, Quaternion Rotation ) Instantiate method
takes three parameters 1) Prefab which is a reference to object to be spawned. 2) The world
position for the new object. 3) Orientation of the new object. If position and rotation are not
specified, the GameObject is spawned at the origin (0,0,0) with zero rotation.
● The first argument, the prefab that is to be spawned, is generally a public GameObject
variable. Once the variable is declared as public, a slot will appear in the component in the
Inspector, and you can drag in a prefab that you have saved (make sure you are dragging
the prefab that is saved in the Project Panel) which is then connected to the variable and can
be instantiated.
15
Spawning/Destroying GameObjects
● In this tutorial, we will spawn a number
of spheres and cubes at random
positions and with different colors.
16
Spawning/Destroying GameObjects
2) Create two empty game objects named
for example:
■ SpawnedObjects: it is considered a
parent game object for all objects
that will be instantiated.
17
Spawning/Destroying GameObjects
3) Create a new C#
script and name
it ObjectSpawner,
and write the
following code:
18
Spawning/Destroying GameObjects
● objectsToSpawn is an array of objects
that will contain the prefabs we wish to
spawn. Later on, we will insert the
prefabs that we have saved into this
array from the Inspector panel.
● In each iteration, Instantiate method creates one of the objects listed in ObjectsToSpawn
array and stored it as a child in spawnParent object. The position and rotation of the
instantiated object are specified randomly.
20
Spawning/Destroying GameObjects
● The color of the spawned object is identified randomly using RandomColor function.
● The yield command here allows us to pause the while-loop and hence limit the creation of
the prefabs to one every some seconds specified by delay.
● Spawn coroutine is called using StartCoroutine function in Unity, which is found in the
Start function of this example.
21
Spawning/Destroying GameObjects
4) Attach the C# script to the Spawner
object: select the object in the Hierarchy
panel click the Add Component
button in the Inspector panel choose
the ObjectSpawner script.
23
Spawning/Destroying GameObjects
7) To destroy the instaniated objects after
a set amount of time, create a new
script, name it DestroyingObject, and
write the following code:
● New scenes come with a simple default skybox already assigned to them.
● Just like the brick textures, skybox images can be obtained from a variety of websites (search
for skybox textures).
1) Create a new material: either right-click and choose Create, or choose Create from the
Assets menu.
26
Creating A New Skybox Material
4) Import the skybox images into Unity: drag the
files into the Project view or right-click in Project
and select Import New Asset.
Up Down
Right Left
27
Front Back
Creating A New Skybox Material
5) Click the imported texture to see its properties in the Inspector and
change the Wrap Mode setting to Clamp and click Apply.
Ordinarily, textures can be tiled repeatedly over a surface, and for
this to appear seamless, opposite edges of the image bleed together.
But this blending of edges can create faint lines in the sky where
images meet, so the Clamp setting will limit the boundaries of the
texture and get rid of this blending.
28
Creating A New Skybox Material
6) Drag these images to the texture slots of the
skybox material. The image names correspond to
the texture slot to assign them to (such as left or
front). Once all six textures are linked up, you can
use this new material as the skybox for the scene.
29
Creating A New Skybox Material
7) Open the lighting window:
Window Rendering
Lighting.
or
● The Application class is a special class used to access and control runtime data, like whether
to quit the game or load a specific scene.
● Before we can load a level we have to add it to the list of levels used in the game. Use File
Build Settings Add Open Scenes.
31
Level Changing
● Scene files can be dragged and dropped from the Project panel into the levels list.
● We can also rearrange the order of scene files within the list by dragging and dropping
items.
● Each level is assigned a unique number or ID, shown in the right-hand column. This ID can
be passed to the LoadLevel function to load the appropriate level.
32
Level Changing
● In this tutorial, we will create two different scenes. The first scene is considered the main
menu of the game which contains a button Start, while the second scene can be any other
scenes (e.g., the starting scene). When the player click on the start button, he/she is directed
to the starting scene.
World
Local
37
Local vs Global Coordinate Space
World
Local
38