Import Tiles from Text FIle

I’m not really sure where this belongs so I’ll just leave it here. Below is a script that will parse a comma delimited text file into a 2D int array and then instantiate prefabs based on the integers of the array. Essentially it allows you to make tile maps from a text editor or tiled map editor (if you export to .txt, only supports 1 layer import).

Attach this script to a GameObject and specify Width/Height (in tiles, assumed a tile is 1x1 unity unit) and the filename including extension of your text file. By default it will look in your project’s Assets folder for the file but this can be changed in the first line of the Awake() function. This script also instantiates along the XY axis but can be easily changed for XZ, or YZ, or whatever you want.

using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;

public class MapImporter : MonoBehaviour {

	public string fileNameToLoad;

	public int mapWidth;
	public int mapHeight;

	public GameObject Floor;
	public GameObject Bricks;
	public GameObject Wall;
	public GameObject WallWithTorch;
	public GameObject Roof;

	private int[,] tiles;

	void Awake () {
		tiles = Load (Application.dataPath + "\\" + fileNameToLoad);
		BuildMap();
	}

	void BuildMap () {
		Debug.Log("Building Map...");
		for(int i = 0; i < tiles.GetLength(0); i++) {
			for(int j = 0; j < tiles.GetLength(1); j++) {
				if(tiles[i,j] == 1) {
					GameObject TilePrefab = TilePrefab = Instantiate(Bricks, new Vector3(j - mapWidth, mapHeight - i, 1), Quaternion.identity) as GameObject;
					TilePrefab.transform.parent = GameObject.FindGameObjectWithTag("Room").transform;
				} else
				if(tiles[i,j] == 2) {
					GameObject TilePrefab = TilePrefab = Instantiate(Roof, new Vector3(j - mapWidth, mapHeight - i, 0), Quaternion.identity) as GameObject;
					TilePrefab.transform.parent = GameObject.FindGameObjectWithTag("Room").transform;

				} else
				if(tiles[i,j] == 3) {
					GameObject TilePrefab = TilePrefab = Instantiate(Wall, new Vector3(j - mapWidth, mapHeight - i, 0), Quaternion.identity) as GameObject;
					TilePrefab.transform.parent = GameObject.FindGameObjectWithTag("Room").transform;

				} else
				if(tiles[i,j] == 4) {
					GameObject TilePrefab = TilePrefab = Instantiate(Floor, new Vector3(j - mapWidth, mapHeight - i, 1), Quaternion.identity) as GameObject;
					TilePrefab.transform.parent = GameObject.FindGameObjectWithTag("Room").transform;
				} else
				if(tiles[i,j] == 5) {
					GameObject TilePrefab = TilePrefab = Instantiate(WallWithTorch, new Vector3(j - mapWidth, mapHeight - i, 0), Quaternion.identity) as GameObject;
					TilePrefab.transform.parent = GameObject.FindGameObjectWithTag("Room").transform;
				}
			}
		}
		Debug.Log("Building Completed!");
	}

	private int[,] Load(string filePath) {
		try {
			Debug.Log("Loading File...");
			using(StreamReader sr = new StreamReader(filePath) ) {
				string input = sr.ReadToEnd();
				string[] lines = input.Split(new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
				int[,] tiles = new int[lines.Length, mapWidth];
				Debug.Log("Parsing...");
				for(int i = 0; i < lines.Length; i++) {
					string st = lines[i];
					string[] nums = st.Split(new[] {',' });
					if(nums.Length != mapWidth) {
					
					}
					for(int j = 0; j < Mathf.Min(nums.Length, mapWidth); j++) {
						int val;
						if(int.TryParse(nums[j], out val )) {
							tiles[i,j] = val;
						} else {
							tiles[i,j] = 1;
						}
					}
				}
				Debug.Log("Parsing Completed!");
				return tiles;
			}
		}
		catch(IOException e) {
			Debug.Log(e.Message);
		}
		return null;
	}
}
2 Likes

I know it’s been a long time since you posted this, but after much looking I stumbled upon your post, I tinkered a bit and in the end it worked seamlessly, i’m very grateful, many thanks for sharing!! =)