In this short tutorial, we will be looking at creating levels from a text file; this will involve;

Creating a text file accessible from a resources folder within your project

Reading this file line by line

Instantiating objects based on the value read in the file

This technique can be used to create multiple levels very easily. This will at least do two things for you: (1) it will make it possible to modify the structure of the level without having to modify the code, and (2) to create individual files for each level.

So, for this purpose, we will be using a new method called Resources.Load. This method makes it possible to load resources (e.g., textures or files) from your Unity project. So first, we will create such a text file and then access it through our script.

Please create a new scene (File | New Scene) and rename it gameLevelAuto (or any other name of your choice).

Create a new cube (Game Object | 3D Object | Cube) and rename it ground.

Make sure that the scale property for this object is (100, 1, 100) and that its position is (0, 0, 0).

You can also create a material for (and apply it to) the ground object if you wish. To do so you can either import a texture and apply it to the object or create a new color material through the Project window (i.e., Create | Material).

Next, we will create an object that will be used to instantiate the walls.

Please create a new cube (Game Object | 3D Object | Cube).

Rename it wall.

Set its scale property to (10, 2, 10).

You can also create and apply a blue Material to this object or use any other texture of your choice.

Once this is done, you can create a prefab from this object, rename this prefab wall, and deactivate the object wall in the Hierarchy window. To create the prefab, you can either drag and drop the object wall to the project window, or create a new prefab from the Project window (Create | Prefab), and then drag and drop the object wall on to this prefab.

Now that our prefab has been created, we can create a text file that will be used to define the layout of the level.

Please create a new folder called Resources within your project.

Create a new text file using the editor of your choice.

Add the following text to it.

1111111111

1010000001

1010101001

1010000001

1011110001

1000000001

1010101111

1001000001

1010000001

1111111111

Save it as maze.txt within your Resources folder.

Once this is done, we can create a script that reads information from the text file.

Please create a new C# script (for example GenerateMaze.cs).

Open this script and add the following code at the beginning of the class (i.e., just before the Start method

public GameObject wall;

Please add the following code to the Start function.

void Start () { TextAsset t1 = (TextAsset)Resources.Load("maze", typeof(TextAsset)); string s = t1.text; int i, j; s = s.Replace("

",""); for (i = 0; i < s.Length; i++) { if (s [i] == '1') { int column, row; column = i%10; row = i / 10; GameObject t; t = (GameObject)(Instantiate (wall, new Vector3 (50 - column * 10, 1.5f, 50 - row * 10), Quaternion.identity)); } } }

In the previous code we do the following:

We declare a variable of type TextAsset that will be used to store the content of the file we have created as a resource.

We then access its content and save it into a string variable.

When this is done, we replace all the “end of line ” characters (i.e., “

”); these are present in our text for convenience to better tell each row apart, however, we don’t need this anymore.

We then loop through the content of the string and instantiate an object whenever the number 1 is read. Columns and rows for our grid are identified also by either dividing the counter (i.e., i) by 10 or by using the modulo operator (i.e., %10 ).

We then do as previously to locate and rotate the instantiated object.

Once you have added this code:

Please save your script

Create an empty object and link this script to this empty object (e.g., drag and drop),

Drag and drop the prefab wall to the field called Wall on the script attached to the empty object

Run the scene

You should see that the scene has been generated as the content is now read from a text field that is saved within the project.

Following this principle, you could have several files that correspond to each level, each with a different name, and load these depending on the level to be displayed.