2D array from text file

Hi there, I am new to unity and I’m looking for a way to bring in a 2d array from a outside text file into unity then processing the information. I did it with java but Im not sure how to do it in unity on account that I don’t know much about C#. I want to read a file full of listed arrays for example:

1,0,0,1,0,1,0,1,0
1,0,0,1,0,1,0,1,0
1,0,0,1,0,1,0,1,0
1,0,0,1,0,1,0,1,0
1,0,0,1,0,1,0,1,0
ect. up to 3000 lines

Is there something in C# like a buffered reader in java that can do this? Also I heard that Unity has something called a Jagged Array which is similar to a 2d array but not constrained to a single length. For what I am doing I know I need 8 indexes, but a variable amount of indexes down. Again I have no idea how to do this, any help would be great. This is my java code if that helps:

			FileReader ConnectionToFile = new FileReader(filename);
			BufferedReader reader = new BufferedReader(ConnectionToFile);

			int[][] spaces = new int[10][10];
			String line = null;
			int row = 0;
			while ((line = reader.readLine()) != null)
			{
			    String[] array = line.split(",");
			    for (int i = 0; i < array.length; i++)
			    {
			        spaces[row] _= Integer.parseInt(array*);*_

* }*
* row++;*
* }*
I then plan to go through each array row saying:
if(spaces[row][1] ==1)
{
do something
]
else if(spaces[row][1] ==0)
[
do nothing
]
if(spaces[row][2] ==1)
{
do something
]
else if(spaces[row][2] ==0)
[
do nothing
]
ect…

If the number of entries on each line is fixed, then a jagged array is not the right solution. If you were going to read the file a line at a time, then maybe you’d want something that grows dynamically like a .NET List to store the values in each line, but the reality is that this text file is a tiny fraction of the size of any bitmap, so just pull the entire thing into memory and see how many lines you have to deal with.

Note if this file exists at build time (rather than a file that gets changed after the app is built), consider using the Unity’s TextAsset class rather than an external text file. Here is a bit of starter code that will split your file into a 2D array. Your test file had nine entries on a line, so this code uses ‘9’. I’m sure there are even more elegant solutions:

using UnityEngine;
using System.Collections;

public class TextDataReader : MonoBehaviour {

	private int[,] spaces;

	void Start() {
		spaces = ParseFile("E:\\Input.txt");

            // Output the data to verify the read
		for (int i = 0; i < spaces.GetLength(0); i++) {
			Debug.Log(spaces[i,0].ToString ()+spaces[i,1].ToString()+spaces[i,2].ToString ()+spaces[i,3].ToString()+spaces[i,4].ToString()+
			          spaces[i,5].ToString ()+spaces[i,6].ToString()+spaces[i,7].ToString ()+spaces[i,8].ToString());
		}
	}

	private int[,] ParseFile(string filePath) {

		string input = System.IO.File.ReadAllText (filePath);
		string[] lines = input.Split (new[] { '\r', '

’ }, System.StringSplitOptions.RemoveEmptyEntries);
int[,] spaces = new int[lines.Length, 9];

		for (int i = 0; i < lines.Length; i++) {
			string st = lines*;*
  •  	string[] nums = st.Split(new[] { ',' });*
    
  •  	if (nums.Length != 9) {*
    
  •  		Debug.Log ("Misforned input on line "+i+1);*
    
  •  	}*
    
  •  	for (int j = 0; j < Mathf.Min (nums.Length, 9); j++) {*
    
  •  		int val;*
    
  •  		if (int.TryParse (nums[j], out val))*
    
  •  			spaces[i,j] = val;*
    
  •  		else*
    
  •  			spaces[i,j] = -1;*
    
  •  	}*
    
  •  }*
    
  •  return spaces;*
    
  • }*
    }

Thanks for such a quick response. I tried the code you had provided and then reworked it to get rid of errors I ran into and this is what I came up with. I still get an error though saying that “GameController.ParseFile(string)’ must have a body because it is not marked as abstract, extern, or partial”. Below is the modified code I came up with. I changed the length to 8 because I only need 8 indexes and moved the private int[, ] parse file(string to the top because it wouldn’t let me put it in the IEnumerator, and I figured it was having issues initializing the array. Is there anything else that you think I am missing here? Again thank you for your help, being new to C# and unity is pretty difficult but you guys are really helping a lot.

private int[,] ParseFile (string filePath);
	
	void Start ()
	{
		StartCoroutine (PlaySong1 ());
	}
	
	IEnumerator PlaySong1 ()
	{
			string input = System.IO.File.ReadAllText (filePath);
			string[] lines = input.Split (new[] { '\r', '

’ }, System.StringSplitOptions.RemoveEmptyEntries);
int[,] spaces = new int[lines.Length, 8];

			for (int i = 0; i < lines.Length; i++) 
			{
				string st = lines*;*
  •  		string[] nums = st.Split(new[] { ',' });*
    
  •  		if (nums.Length != 8)* 
    
  •  		{*
    
  •  			Debug.Log ("Misforned input on line "+i+1);*
    
  •  		}*
    
  •  		for (int j = 0; j < Mathf.Min (nums.Length, 8); j++)* 
    
  •  		{*
    
  •  			int val;*
    
  •  			if (int.TryParse (nums[j], out val))*
    
  •  				spaces[i,j] = val;*
    
  •  			else*
    
  •  				spaces[i,j] = -1;*
    
  •  		}*
    
  •  	}*
    
  •  return spaces;*
    
  •  yield return new WaitForSeconds (startWait);*
    
  •  for (int i = 0; i < lines.Length; i++) {*
    
  •  				if (spaces [i, 1] == 1) {*
    
  •  				} else if (spaces [i, 1] != 1) {*
    
  •  				}*
    

ect…