Text Adventure Game
Text Adventure Game
You may also want to drag the Game Scene window side by side with the Scene
window to see what is happening.
To fix the ratio of our game, Go to Game Scene Window and click on the Free Aspect
Tab, select an Aspect, or we can set our own. To do that, click on the “+” button.
Set the Label to “1080” and Width & Height to 1920 x 1080.
Now let’s add some Text. Right-click on the Canvas and select UI, then choose Text.
You can change the properties of the Text by going to the Inspector. Example Changing
the Text Color to White.
You may also want to change the Font Size to 40 or bigger size.
Now that we’ve changed it to 40, we can’t see it at all. That’s because it doesn’t fit within
our text box. So, drag the sides of the text box to make it bigger.
Like this …
It doesn’t need to be perfect for now, we just need to get the general layout of the Text.
Next, always rename our Game Objects (Text), so it makes sense.
Scroll up to the top of Inspector, and instead of just plain old text, Rename it to the Story
Text.
On the Text (Script), we’ve just got placeholder that says, New Text.
Let’s make a whole bunch of placeholder story text just so we can see how it fits on the
screen. You can always adjust the Text size if necessary.
Now let’s add more Game Object.
Again, on the Canvas, right-click, then select UI and choose Image.
Finally, drag the Image at the top of the Text from the Hierarchy, to send the Image to
the back of the Text.
Challenge: Get your Canvas ready
• Create a text field for your story
• Create a heading
• Optional: Add some flair/ match your theme.
Update Text Component
Here, we are going to add Text, programmatically. So that, when we hit Play, the text is
fade in from our script and it is put within our game.
Now we want a script where we can put our game logic that says instruction to the
player. And when we create a script, it needs to be attached to a game object. They
need to be component of a game object.
To do that we need to create an Empty Game Object. Right-click on the Hierarchy, and
choose Create Empty, then rename it to Game.
And so, every game object has a transform because we need to know where in the
world it will be placed. It’s a good practice, when we create an empty game object, is to
reset the position back to zero.
Or right click on Transform and choose Reset.
Next, we are going to add a Script Component, click on the Add Component Button and
scroll down to New Script.
Or just search on the name of Script, example “AdventureGame”, then choose New
Script
Then, click Create and Add Button
You can see, after we do that, down in out Asset directory, we now have the new script,
with AdventureGame as the name.
That means, we have now this available within our Inspector, so that we can make
changes in there.
With that, we want our Story Text to be its Reference, so to do that. Click on the
Selector.
Challenge:
• What is the first thing that your player is confronted with?
• Write 2 or 3 sentences that will start your game.
• What are the 2 choices your player can take?
Scriptable Objects
• ScriptableObject is a class that lets us store data in stand alone assets.
• Keep mountains of data out of our scrpits
• It is lightweight and convenient.
• Used as a template for consistency.
If we want to convert this to a TextArea, so we can type a paragraph, add the following
code to our storyText variable.
The first number (14) is the minimum side visible in the Inspector and the second one is
the number of lines (10) before the scroll will appear.
Public Methods and Return Types
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as
functions.
• Why use methods?
o To reuse code: define the code once, and use it many times.
However, if there is a return type then the method is going to execute all of the things in
this curly brace and then it's going to say, hey, here's a piece of data for you back to the
place where it was called from.
In our Scriptable Object States, create a public method called GetStateStory that a
return type of string.
This means that the method will return whatever is current within storyText.
So, this is our end goal is to return that information into our text box in here.
Next, is to execute this particular method by going to AdventureGame script, typing the
following code:
Then, on the Inspector, drag the StartingState Scriptable Object to the Starting State
Reference of the Game Empty Object.
To know continually know what state were in, add the following code to the
AdventureGame script.
On the State script, what we want to do is have a variable which stores all of our next
states.
To do that create 2 more State. Name them Room 1 and 2 (for the moment)
Note that in the Inspector of StartingState, we have now a field Next States
Type in the 2 for the size of our States array (click on the dropdown icon to see
Elements field).
Then drag Room 1 to Element 0 and Room 2 to Element 1
And from Room 2 State, drag Room 1 to the Element 0 and StartingState to Element 1.
This will call the GetNextState() from the State script, that will return an array of State
(Room 1, Room 2, or StartingState)
Then, this will update the value of state if the player press number 1.
Challenge:
Then, we need a way of presenting the display to the player, within our text field. To do
that add the following code:
When press 2
Loop
• Repeated event until condition is met
• Very useful when counting or iterating
• One type of loop is For Loop
Here we have all the States that we could use in the Text Adventure Game as a
practice.
Challenge:
Then, click on “Import TMP Essentials” and “Import TMP Examples and Extras” button
Close it, now we have Font Asset Creator
Then, click on the Generate Font Atlas button and Save it to the default directory.
Close it. Back on the Canvas, right-click on that and select UI, then choose
Text-TextMeshPro.
Move it on the Title Text and delete the Title Text. Then Type in the Title
Challenge
Example:
Publishing
First, we need to check our canvas if it fits to screen size. Click on the Canvas, on the
Inspector, scroll-down to Canvas Scaler and change the UI Scale Mode to Scale with
Screen Size.
Go to File->Build Settings
Check on some setting, like what platform are you going to target.
For this example it will be deployed on PC. Then , click on Build and Run.
End.