- Home /
How to read text file from resources and store into 2d array
Hello guys, I want to read a text file from resources folder and save the content of the file into 2d array. Here is my code where I can read the text file in editor but i can not access the file in build. Is there any solution of this or is there any other way I can do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
public class TestScript : MonoBehaviour
{
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 ("assets/resources/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, y, 0), Quaternion.identity);
//Instantiate (player, new Vector3 (0, 0.5f, 0), Quaternion.identity); //old line
Instantiate (player, new Vector3 (0, 0f, -0.5f), Quaternion.identity);
break;
case sfloor_valid:
Instantiate (floor_valid, new Vector3 (x, y, 0), Quaternion.identity);
break;
case sfloor_obstacle:
Instantiate (floor_obstacle, new Vector3 (x, y, 0), Quaternion.identity);
break;
case sfloor_checkpoint:
Instantiate (floor_checkpoint, new Vector3 (x, y, 0), Quaternion.identity);
break;
}
}
}
}
// Update is called once per frame
void Update ()
{
}
string[][] readFile (string file)
{
string text = System.IO.File.ReadAllText (file);
string[] lines = Regex.Split (text, "\r\n");
int rows = lines.Length;
string[][] levelBase = new string[rows][];
for (int i = 0; i < lines.Length; i++) {
string[] stringsOfLine = Regex.Split (lines [i], " ");
levelBase [i] = stringsOfLine;
}
return levelBase;
}
}
Answer by kainerya · May 29, 2017 at 06:34 AM
Object textFile;
textFile = Resources.Load ("assets/resources/level1");
TextAsset temp = textFile as TextAsset;
char[] cArr = temp.text.ToCharArray ();
using System.IO;
Answer by UnityGISTech · May 29, 2017 at 06:35 AM
Try this : using System.IO;
To Read: string text = System.IO.File.ReadAllText("your file location"); or string[] lines = System.IO.File.ReadAllLines("your file location");
To write: using (System.IO.StreamWriter file = new System.IO.StreamWriter("save location")) { foreach (string line in lines) { // If the line doesn't contain the word 'Second', write the line to the file. if (!line.Contains("Second")) { file.WriteLine(line); } } }
Answer by FM-Productions · May 27, 2017 at 03:20 PM
Hi,
I can only make a guess, but if your code is working in the editor and not in the build, it is very likely that the relative path to the resources folder has changed. Maybe you could try to use a function for accessing the resource folder instead of using a hardcoded path? Maybe it works with Resources.Load:
https://docs.unity3d.com/ScriptReference/Resources.Load.html
Your answer
Follow this Question
Related Questions
2D array problem in C# 2 Answers
How can I read data from a text file, putting a large amount of data into structures 2 Answers
Can not load csv file 0 Answers
Is this possible to work as a temporary storage? 2 Answers
Best way to manage stats in a text file 2 Answers