Hello,
I working on a editor tool to create 2D map and I store datas in an Array that I copy in a txt file
Using StreamWriter works very well but I have some issues concerning streamReader when I did a test on a simple txt file that I included into the asset folder (I did try other location but same result) it says that unity can’t find the file :
and when I checked the txt file really was in the folder
here is the code
using UnityEngine;
using System.Collections;
using System;
using System.IO;
//fonction charger
//fonction sauvegarder
class DatasManager : MonoBehaviour
{
string line;
string MBC;
public void Sauvegarder(){
//pour les fichiers réservés à l'éditeur
using (StreamWriter sw = new StreamWriter(Application.dataPath+"/Editor_"+GameObject.Find("GUImanager").GetComponent<GUImanager>().nomFichierSauvegarde + ".txt")){
for(int i = 0; i < 15*25; i ++)
sw.Write(transform.GetComponent<ArrayController>().tableau[i] + " ");
sw.WriteLine(" ");
sw.WriteLine(transform.GetComponent<ArrayController>().coordXMB +" "+transform.GetComponent<ArrayController>().coordYMB);
}
//pour les fichiers utilisables sur MB
//~ using (StreamWriter sw = new StreamWriter(Application.dataPath+"/MB_"+GameObject.Find("GUImanager").GetComponent<GUImanager>().nomFichierSauvegarde + ".txt")){
//~ }
}
public void Charger(){
//fonction qui n'utilise que les fichiers Editor_ pour un traitement plus aisé
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
String MBC;
line = sr.ReadLine();
MBC = sr.ReadLine();
}
}
void Update()
{
if(Input.GetKeyDown("r")){
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
line = sr.ReadLine();
MBC = sr.ReadLine();
}
}
}
}
Is someone can help me to solve the problem? the fact is that I can’t use TextAsset because I need to re use the text file I created to load it into my editor
Reading and Writing both require a slightly different approach. I recently built my own system for this. It’s in JS, but it should get the point across.
function readSaveFile(){
var fileContent : String[];
Debug.Log(robotConfigStrings[loadInt]);
try {
var fs = new FileStream("TestFile.txt", FileMode.Open, FileAccess.Read);
var sr = new StreamReader(fs);
var fileInfo = sr.ReadToEnd();
sr.Close();
//Optional: Put content in an array.
fileContent = fileInfo.Split("\n"[0]); // "\n" is linebreak - can be any character.
// fileContent[0] = line 1 ; fileContent[1] = line 2 ; etc
}
catch (e) {
Debug.Log(e.Message);
}
I never wrote C#, but I think this should work at a basic level (without try-catch):
if(Input.GetKeyDown("r")){
FileStream fs = new FileStream("TestFile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader("TestFile.txt"));
fileInfo = sr.ReadToEnd();
sr.Close();
fileContent = fileInfo.Split("\n"[0]);
}
It looks to me like you are reading from a different location than you are writing to. You are writing to the location at (Application.dataPath+“/Editor_”+GameObject.Find(“GUImanager”).GetComponent().nomFichierSauvegarde + “.txt”), but you are trying to read from (Application.dataPath + “/TestFile.txt”). Are you trying to read from a file that you didn’t create with this save method?
thank you, but I still have the same issue with your code Senshi
Maybe there is something to do specific in C#
here are parts of the code use
save :
using (StreamWriter sw = new StreamWriter(Application.dataPath+"/Resources/Editor_"+GameObject.Find("GUImanager").GetComponent<GUImanager>().nomFichierSauvegarde + ".txt")){
for(int i = 0; i < 15*25; i ++)
sw.Write(transform.GetComponent<ArrayController>().tableau[i] + " ");
sw.WriteLine(" ");
sw.WriteLine(transform.GetComponent<ArrayController>().coordXMB +" "+transform.GetComponent<ArrayController>().coordYMB);
}
and load :
if(Input.GetKeyDown("r")){
string fileInfo;
FileStream fs = new FileStream(Application.dataPath+"/Resources/Editor_lvl", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
fileInfo = sr.ReadToEnd();
sr.Close();
I though also about another method using TextAsset by catching the new text made in the editor thanks to Resources.load but the problem is that the theoritically TextAsset I should retrieve is null
can someone help me out ?
sorry to answer so late, I still have some issues with that as well, I did try to change the name as you suggested Senshi but still Unity can’t find the txt file, well he can but not if it has been created whitin the tool I made. I found some dirty solution but it works thank anyway for your help :).
Just checking is this issue with Editor or Standalone builds?
You cannot load any files within resources in a standalone build without Resources.Load.
You’ll notice in standalone builds there is no Resources folder, it is all compressed into a single file call assets_0.asset or something similar.
This is for 2 reasons:
A) Speed. It’s organized in a way to allow for rapid loading and finding the right asset.
B) Security. It means people can’t buy your game and easily steal/modify all your assets. Helps prevent things like see through wall hacking on multiplayer games.