Resources/Games.xml
In your Assets folder, you’ll find a Resources sub-folder.
Unity allows us to load assets directly from the Resources folder.
Open up the Games.xml file in your text editor to take a look.
<?xml version="1.0" encoding="utf-8"?>
<QuizList xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<elements>
<QuizElement>
<text>Tony Hawk</text>
</QuizElement>
<QuizElement>
<text>Zelda</text>
</QuizElement>
<QuizElement>
<text>GTA</text>
</QuizElement>
<QuizElement>
<text>Mario</text>
</QuizElement>
<QuizElement>
<text>Uncharted</text>
</QuizElement>
<QuizElement>
<text>GoldenEye</text>
</QuizElement>
<QuizElement>
<text>BioShock</text>
</QuizElement>
<QuizElement>
<text>Half-Life</text>
</QuizElement>
<QuizElement>
<text>Halo</text>
</QuizElement>
<QuizElement>
<text>Pac-Man</text>
</QuizElement>
</elements>
</QuizList>
This is a simple XML file describing the data for our first quiz.
As you can see, this file describes something of type QuizList which itself contains a series of elements of type QuizElement.
QuizGame.cs
The classes QuizElement and QuizList can be found inside the class QuizGame.
QuizElement is just a string, but as the comment says, this allows us to add more data later.
public class QuizElement
{
// we only have a single string in here, but made it a class in case you ever want to expand
public string text;
}
QuizList is the list of QuizElements
public class QuizList
{
// this is nothing but a list, but like QuizElement, the class allows us to expand easily
public List<QuizElement> elements;
}
The bulk of the game is handled within the QuizGame class.
public variables
At the top of the class, you’ll find the public variables. These are the pieces that are exposed in the editor, so this is how everything is hooked up.
m_text is where we will be putting the strings from the QuizList.
We need m_background to change the color for the “Correct” and “Wrong” messages.
Next up, we have prefabs for those “Correct” and “Wrong” screens. We will get the colors from those.
For more information about prefabs, check out this supplementary video: Prefabs
After that, you’ll see sounds that we’ll play when the player makes a choice.
We’ll talk about audio in more detail in week 4. For now, I’ve set it up to play the sounds. You won’t have to do anything with those.
Finally, we have 2 angles that determine how far the player needs to tilt the phone.
These are in degrees. I’ve chosen angles that I think work well, but you’ll need to write the code to detect the tilt angle in part 4.
QuizInput
QuizInput is another class-within-a-class.
/// <summary>
/// Bundle up all the potential input values
/// These are "abstracted" away from the input mechanism
/// This "decouples" the input commands from the input mechanism
/// This makes it easy to create alternative input mechanisms (keyboard, joystick, AI, etc)
/// </summary>
class QuizInput
{
public bool up = false;
public bool down = false;
public bool center = true;
}
The input for this game is very simple. It’s just tilt up or tilt down.
But I’ve set it up as 3 separate bools.
As the comment says, the purpose of this class is to create an abstraction to separate the idea of the tilt angle apart from the concept of choosing “Correct” vs “Wrong”.
This allows us to set up alternative input methods and get the same output.
GameManager.cs
We’ll spend plenty of time in the QuizGame class as we go, but for now let’s take a look at GameManager.
This class manages the flow of the game.
If you pause the game, that’s handled here.
We will also be using the GameManager class to choose which XML file to use.