- Home /
How to open/read/write CSV file once the app is built? (like Resources.Load but whit the built app at Application.dataPath)
Hello there.
As i say in the title, i have this code to load the file in editor:
if (Application.isEditor)
{
Something= Resources.Load("ResourceName") as TextAsset;
}
But how to load the same info once the game is built, if the csv file is stored at Application.dataPath?? i can't find the function in google/Unity manual... :(
THAAANKS!
Answer by kskjadav007 · Feb 05, 2020 at 12:23 PM
I think you can do something like this.
private string m_path;
// Start is called before the first frame update
void Start()
{
m_path = Application.persistentDataPath+ "/newcsv.csv";
if (File.Exists(m_path))
{
byte[] m_bytes = File.ReadAllBytes(m_path);
string s = System.Text.Encoding.UTF8.GetString(m_bytes);
Debug.Log(s);
}
}
Hey, Thanks for your answer, i will try it later. But in your code, "s" is a string, and i should get a TextAsset (it comes from a datatable and i need to be structured with columns) How should i get the info in a textasset format?
I think this will be useful .
https://bravenewmethod.com/2014/09/13/lightweight-csv-reader-for-unity/
Answer by Hellium · Feb 05, 2020 at 12:12 PM
// Code not tested
string path = Application.dataPath + "/Path/To/File.csv";
Something = new TextAsset( System.IO.File.ReadAllText(path) ) ;
The question is: are you sure your CSV will be located here when the app runs? You may want to consider putting the file in the StreamingAssets if the file already exist in your project, or use Application.persistentDataPath
to save & load the file.
Thanks @Hellium for yout time :D I will try it later
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Loading TextAsset returns null 1 Answer
Trouble with Resources.Load 1 Answer
Mac OS X Build Execute File? 1 Answer