• Home  / 
  • Unity3D
  •  /  Using Unity3D Xml Files for game Data – Quiz Game Example

Using Unity3D Xml Files for game Data – Quiz Game Example

Recently I shared a method for creating a QuizGame using the ScriptableObject functionality built into Unity3D.  Today, we’re going to modify that example to be a little more scalable for cases where you might want 1000’s of questions that aren’t all managed in the editor.  To accomplish this, we’ll use some Unity3D xml files and XmlSerializer.  By using xml files, we can easily swap or modify the data outside the engine, and if we pull the XML data from a web service, we could change the questions for our quiz game without any modification or update to the game itself.

Setup – Read Part 1

If you haven’t seen the previous post, take a look at it now.  It covers the basics for laying out a sample UI and how the game will work.

You can see that here: HowTo: Build a Unity3D Quiz Game – Like Trivia Crack / Heads Up

The differences

In the previous examples, our QuizQuestion class was actually pretty heavy.  Almost all of that was due to editor customizations that made it more streamlined to work with.

Since we’ll be serializing our QuizQuestions in xml now, we can go with a very simple class that only contains 4 auto properties.

The QuizCollection

Because of how the previous sample is architected, we don’t need to change much to allow for XML serialization of our questions.  Other than the QuizQuestion class, we only need to adjust our collection.

Instead of using Resources.LoadAll, we’ll be using the XmlSerializer and StreamReader classes to do the work.

The XmlSerializer requires a type for it’s constructor.  Our data is an array of QuizQuestions, so we initialize it by passing “typeof(QuizQuestion[])” into the constructor on line 23.

In Awake, we check to see if a questions.xml file exists.  If not, we’ll write out a sample xml file that can be used as a starting point.

Then we load the questions..  everything else is the same as the previous example.

Separation of Concerns

The reason this change is so small is that in part one, we focused on keeping a clear separation of concerns.  Keeping things split up like this makes change easy and painless.

What about JSON?

There’s no doubt a good number of people will think “why use xml, what about json?”, “json is sooooo much better”, “etc”.

As a whole, I agree json is a better format.  It’s cleaner and removes a lot of the clutter from verbose ceremony..

Unfortunately, I still really dislike the built in json serializer for Unity.  If you do want to use JSON though, I’d recommend something like JSON.Net for Unity.  I’ve used it in the past and it was great.

For this example though, that seemed like overkill.  Xml will work fine here, and realistically you’ll probably want a web service providing the data here… and writing out to xml/json for most services is as simple as swapping the request header.

Conclusions & Download

This post is meant to answer a specific question from the Unity3D.College facebook group.  If you have a question of your own, join us and share 🙂
If you’d like to download the full project sample, it’s available here: https://unity3dcollege.blob.core.windows.net/site/Downloads/QuizGameXml.zip