cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a csv file ?

MrFlo666
Explorer
Hello,

I develop under Unity 2019.3.8f1 in C# for an Oculus Quest.
I wish to read a csv file, parse it, and display stuff depending on the data contained inside the file.
Problem is : 
1. I don't find my csv file inside the oculus quest (i checked with  SideQuest)
2. I don't know which path to use to access it (once it will be uploaded and visible)

Can someone help me ?

Thanks !
9 REPLIES 9

rh_galaxy
Heroic Explorer
If the file is static you can include it in "unity_proj\Assets\Resources\custom_dir" rename it to .txt and access it through
TextAsset f = (TextAsset)Resources.Load("custom_dir/" + filename_no_extension);
String fileText = System.Text.Encoding.UTF8.GetString(f.bytes);


If it is a dynamic file maybe 
String fileText = System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(Application.persistentDataPath + "/" + filename));

I use this to load text file level data, both default levels (at compile time), and custom levels (added dynamically later) in a Windows Desktop project, but it should work on the Quest as well.

MrFlo666
Explorer
I'll check that but i already tried the dynamic solution, with no success. And I've taken a look at the files through SideQuest but didn't file my csv. Is there something specific I have to do on my project to make the csv files uploadable ?

rh_galaxy
Heroic Explorer
I don't know how the Quest works with files, but what path is the Application.persistentDataPath? Can you access that path and upload files to it from SideQuest?

But the static variant is sure to be working because then the file will be integrated to the package data at compile time and be transferred with the project when you upload the package.

MrFlo666
Explorer
let's clarify something : what do you mean by "static" and "dynamic" ?
My need is that my csv files has to be installed in the same time than my apk. If it's the static variant you're talking about, then it's ok for me. If so, how to perform that ?

rh_galaxy
Heroic Explorer
Yes that's the meaning of static (compile time in the apk), dynamic (not known at compile time, not in apk but a file the user can upload at any time).

static: In your project path (on your PC), copy file to "\Assets\Resources\custom_dir" rename it to "file.txt" then in the code
TextAsset f = (TextAsset)Resources.Load("custom_dir/" + "file");
String fileText = System.Text.Encoding.UTF8.GetString(f.bytes);
then just parse the fileText string - it contains the whole .csv file.

char[] separator = { ',' };
string[] tokens = fileText.Split(separator, StringSplitOptions.RemoveEmptyEntries);
int first_value = int.Parse(tokens[0]);
...

MrFlo666
Explorer
ok i'll check that, thanks !
just one question : why do i have to rename it into file.txt ? Is csv not allowed ?

rh_galaxy
Heroic Explorer
Guess it's because you also access the file without the extension (just "file"), but the least confusing would just be to give the full file name and path all the time, so I don't really know the reason.

MrFlo666
Explorer
ok i'll check this afternoon and let you know. Thanks !

MrFlo666
Explorer
hey hey, I confirm : this works well. Thanks for your precious help !