- Home /
How to load CSV files from webplayer
Yes, I realize that I could simply make the extension of my CSV file .TXT and use TextAsset to load it, however I do not want to do this. The CSV extension must remain as-is.
Basically I have a CSV file that I need to load and get the full text from. I can't use TextAsset because it's not a TXT file, and I've tried using Resources.Load(), however I'm not sure what type to cast it to.
How can I load a CSV file and access its contents? I tried using System.IO.StreamReader, and while this works in the editor, it does not work in the web player.
Let me ask the obvious question, which I should have asked before :) Where is the file located?
And a second question - why do you want to keep the .csv extension? Is there a specific need, say another program reads/writes it? Depending on the reason, there may also be a solution or work-around for that.
Answer by Cyclops · Jun 04, 2010 at 04:04 PM
To quote from: Best way to output data from Unity: "...not available in the webplayer for security reasons." The extension doesn't matter, the Browser simply isn't allowed to read or write to the local machine. You could either make your program a stand-alone executable, or put the datafile on the Server.
Update: to demonstrate this is an issue with the web-player, try building a standalone executable. Your System.IO.StreamReader code should read the file with no problem (assuming no bugs). Does that solve your problem?
If your answer is "no, I want the Browser to read the file", then sorry, it won't work.
Depending on the type of data, you might be able to use PlayerPrefs. Also see Does Unity have cookies? Since you appear to want a .csv file, probably not, but I thought I'd mention them.
The question isn't about saving files, it's about loading them.
It doesn't matter if it's about saving or loading, the web player cannot read or write to the disk at all, for security purposes. This isn't possible, and he'd have to make the game a standalone EX$$anonymous$$
I know a little more background about this question than what's in the question. Basically the question should be "Can you use Resources.Load to load a file as a text asset that doesn't have the .txt extension". It's not about loading a file outside the unity bundle.
But is it about loading a file from the webplayer? Because that's what is in the title :) A standalone executable can read any type of file using .Net System.IO. A Browser can not read anything from the user's hard drive, regardless of file type.
This particular use case doesn't involve loading a file off the user's machine. The web player/streamreader stuff is a red herring that was intended to say "hey I tried doing it this way and found out it obviously doesn't work on the web player".
Answer by dineshrajpurohit · Jun 20, 2012 at 03:22 AM
Unity Web player does not allow file manipulation(read or write). The best way to do this is using WWW in Unity to retrieve information from the file via PHP. Since CSV basically contains comma separated valued text, they can be passed echoed out as it is and on the unity side they can be split in an array using ',' (comma) delimiter. Let me know if you need more information or a sample code.
Answer by Zogg · Jul 02, 2013 at 02:51 PM
I think the answers miss the point.
You can actually "load" a .txt file from the webplayer when you refer to it from a TextAsset or put the file into a Resources folder. That's possible because in this case it's not actually loaded in runtime from the hard drive, but included in the executable - just like textures, sounds or whatever other resources you use in your program.
The asker wants to know (and so do I) if there is a way to do a similar thing with a .csv file, without changing the extension. It seems not to be possible, but maybe I'm wrong?
Your answer
Follow this Question
Related Questions
How do I make sure a resource is loaded or is instantiated? 2 Answers
Accessing 4.6 GUI build-in sprites in C# 0 Answers
load file with www on webplayer 1 Answer
WbPlayer Loading From File 0 Answers
Resources.Load("sprite") returning nil 0 Answers