Reader small image

You're reading from  Unity Certified Programmer Exam Guide - Second Edition

Product typeBook
Published inMay 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781803246215
Edition2nd Edition
Languages
Right arrow
Author (1)
Philip Walker
Philip Walker
author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker

Right arrow

Chapter 5: Creating a Shop Scene for Our Game

In this chapter, we will incorporate and extend the scriptable objects that heavily helped make our player and the enemy ships in the previous chapter. We will customize a new shop scene, where we will add new upgrades for the player's ship with the use of scriptable objects.

We will also look at the common uses of raycasts; if you aren't familiar with them, they're best described as an invisible laser that shoots from one point to another:

Figure 5.1 – Identifying game objects with a raycast

When the ray hits a game object with a collider, it can retrieve information about the object, and then we can go a little further and manipulate the object we've hit. For example, we can cast a ray to a game object cube, and the ray will confirm to us that it's a cube. Because we have the cube's reference, we can change its color or scale, or position or destroy it – we can pretty...

The core exam skills covered in this chapter

We will cover programming core interactions:

  • Implementing and configuring game object behavior and physics
  • Implementing and configuring input and controls

We will also cover working in the art pipeline:

  • Understanding materials, textures, and shaders, and writing scripts that interact with Unity's rendering API

This chapter also covers developing application systems:

  • Interpreting scripts for application interface flow, such as menu systems, UI navigation, and application settings
  • Interpreting scripts for user-controlled customization, such as character creators, inventories, storefronts, and in-app purchases
  • Analyzing scripts for user progression features, such as scoring, leveling, and in-game economies, utilizing technologies such as Unity Analytics and PlayerPrefs
  • Analyzing scripts for two-dimensional overlays, such as Heads-Up Displays (HUDs), minimaps, and advertisements
...

Technical requirements

The project content for this chapter can be found at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition/tree/main/Chapter_05.

You can download the entirety of each chapter's project files at https://github.com/PacktPublishing/Unity-Certified-Programmer-Exam-Guide-Second-Edition.

All the content for this chapter is held in the chapter's unitypackage file, including a Complete folder, which holds all of the work we'll carry out in this chapter.

Check out the following video to see the Code in Action: https://bit.ly/3klZZ9q.

Introducing our shop scripts

In this section, we will make some new scriptable objects, as we did when we created our player's ship settings (health, speed, firepower, and so on). You can refer to the Introducing our scriptable object (SOActorModel) section of Chapter 2, Adding and Manipulating Objects, for a reminder of how this is done. Instead of changing our enemy's or player's ships, we will be manipulating the shop's selection of powerups (with a selection grid) to add our own ship upgrades that the player will be able to choose from. These upgrades will then be transferred to the player's ship, which will be visually recognized, and two of the three upgrades will carry out alterations to the gameplay.

Before we go into further detail, let's refresh our memory on where the shop scripts are within the game framework that we introduced in Chapter 1, Setting Up and Structuring Our Project.

The following diagram shows the location of the shop scripts...

Customizing our shop selection

In this section, we are going to use scriptable objects to customize each selection. We have already used scriptable objects in Chapter 2, Adding and Manipulating Objects. This time, we will use a similar method but for our selection grid; hopefully, this will make you appreciate how scriptable objects can be expanded and used across the game.

As mentioned in Chapter 2, Adding and Manipulating Objects, I make a habit of initialing scriptable objects with an SO prefix so that they're easy to identify. Let's create an SOShopSelection script:

  1. In the Unity Editor, go to the Project window and navigate to Assets/Script.
  2. Create a script (using the same method from Chapter 2, Adding and Manipulating Objects) and name it SOShopSelection.

This SOShopSelection script will create a template of data types for our asset files (the same as with our player and enemy ships). These asset files will be attached to each of the player ship...

Selecting game objects with raycasts

In this section, we are going to create the final shop script, PlayerShipBuild. This script holds properties such as selecting any button from the selection grid, running adverts, communicating with our existing game framework scripts, launching our game to play, and a few other things that we will cover.

One of the subjects you will likely come across in your Unity programmer exam and when developing games/applications in Unity is shooting invisible lasers, which are used for things such as shooting a gun, making a selection in three-dimensional space, and more. In this section, we are going to make a button on the selection grid light up blue when the player presses on it to let them know that it has been selected. We already have each of our buttons set up with blue rectangles that are permanently on. So, all that we need to do now is turn them all off when the scene becomes active and make any of the buttons turn on when a ray (invisible...

Adding information to our description panel

When a selection is made in the shop scene, we can take the information from the selection's scriptable object asset file and display its details within its textBoxPanel game object.

Let's take a look at the textBoxPanel object in the Hierarchy window:

Figure 5.29 – Our 'textBoxPanel' content in the Hierarchy window

The textBoxPanel game object holds a black quad that is used for its background. It also holds four other game objects, as follows:

  • name: This game object contains a TextMesh component that receives data from the selection that was made that contains the iconName scriptable object variable.
  • desc: This game object also holds a TextMesh component that receives data from the selection's description scriptable object variable.
  • backPanel: This game object serves as a background for the selection grid.
  • BUY?: This game object will be covered later on when...

Summary

In this chapter, we started creating a shop scene that holds various buttons and panels created from three-dimensional polygons. We then created our own script to fill the scene with images, values, names, and descriptions of assets that can potentially be purchased with virtual credits.

We also made use of scriptable objects to create a template for our code so that it can be topped up with various in-game powerups without inflating our game's framework. We also made it interchangeable, so if a weapon needs changing, replacing, or removing, we can simply remove the template without affecting the rest of the code in our game framework.

The other lesson we learned in this chapter is being aware that we can create games that are free to download but also knowing how to create a form of income with a monetization game design to create revenue.

In the next chapter, we will continue with our shop scene and focus more on the content of each button and the overall functionality...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Certified Programmer Exam Guide - Second Edition
Published in: May 2022Publisher: PacktISBN-13: 9781803246215
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker