Create level based on XML/TXT file

Hi everyone!

I’m new to unity and was thinking about a nice concept of creating levels based on an XML/TXT file.

Let’s say I have an file which looks like this:

X X O X
X O X X
X X X X
X X X O

I want to create a grid (underground) from this file. For every X I want to load an area on which it is possible to walk. For every O it should load an obstacle, which cannot be passed.

Is this possible to do? Or are there already scripts/implementations which can be used?

Here is an example of how the game should look like:

So I will load also 3D objects according to the marks set.
I want to be able to load the underground, holes in the underground, obstacles and also checkpoints.

The idea is to extend the levels further on easily.

Here is the overall solution for creating an level based on an TXT file:

Textfile looks like this:

S 0 0 0 1
0 0 1 0 2
2 0 0 2 1

We need to read the file and save it’s content into an twodimensional array. We will split by line breaks and spaces:

string[][] readFile(string file){
	string text = System.IO.File.ReadAllText(file);
	string[] lines = Regex.Split(text, "

");
int rows = lines.Length;

	string[][] levelBase = new string[rows][];
	for (int i = 0; i < lines.Length; i++)  {
		string[] stringsOfLine = Regex.Split(lines*, " ");*

_ levelBase = stringsOfLine;_
* }*
* return levelBase;*
* }*
In the next step we are going to iterate through the matrix and instantiate the Prefabs we bound to our Script via public variables:
* public Transform player;*
* public Transform floor_valid;
public Transform floor_obstacle;
public Transform floor_checkpoint;*

* public const string sfloor_valid = “0”;
public const string sfloor_obstacle = “1”;
public const string sfloor_checkpoint = “2”;
_
public const string sstart = “S”;*_

* void Start() {*
* string[][] jagged = readFile(“D:/level1.txt”);*

* // create planes based on matrix*
* for (int y = 0; y < jagged.Length; y++) {*
* for (int x = 0; x < jagged[0].Length; x++) {*
* switch (jagged[y][x]){*
* case sstart:*
* Instantiate(floor_valid, new Vector3(x, 0, -y), Quaternion.identity);
_
Instantiate(player, new Vector3(0, 0.5f, 0), Quaternion.identity);_
_
break;_
case sfloor_valid:
Instantiate(floor_valid, new Vector3(x, 0, -y), Quaternion.identity);
_
break;_
case sfloor_obstacle:
Instantiate(floor_obstacle, new Vector3(x, 0, -y), Quaternion.identity);
_
break;_
case sfloor_checkpoint:
Instantiate(floor_checkpoint, new Vector3(x, 0, -y), Quaternion.identity);
_
}_
_
}_
_
} _
_
}_
Here is the result of this. I was reading a 10x10 matrix holding several different strings (just like the mentioned text file):
_
![alt text][1]_
I hope this will help all you guys for making games with nearly no effort to create new levels. I would also like to thank [gajdot][2] who helped me a lot in finding the answer.
_
[1]: http://answers.unity3d.com/storage/attachments/18104-unbenannt.png*_
_*[2]: http://answers.unity3d.com/users/246911/gajdot.html*_

Unity now has built in 2d capability, so he may need it. It can be done, even though I didn’t saw a pre made script for you so you need to create your own one.

Basically what you can do like in any programming environment is that you create a script to read a file, and interpret what he reads. You create a small prefab with the obstacle and collider. After this you just go trough the file and hold a value for column and row you are reading from the file and basically you instantiate a prefab(if you have x) on a coordinate based on your value read, something like rowsizeOfPrefab, columnsizeOfPrefab else if you have 0 you don’t do anything. This way you get a nice grid map made up from your prefab.

You can go even beyond if you do other values and create other prefabs like wood wall, brick wall etc and you instantiate the specific prefab on the coordinate instead of a general one.

Basically this can be used to create a 3d environment too, you just need a 3d prefab in that case and generate floor when you have x, otherwise wall :slight_smile:

It’s really simple, for beginning, you create a script with two GameObject vars, one for the walkable tile, the other for the collider tile.

On start, you load your file and with two loop (can’t write an entire and real source now) :

for (x = 0 to length)
    for (y = 0 to length)
        if (file[x, y] == 0) Instantiate walkable
        else Instantiate walkable