- Home /
Read and Write to text file Online
Hi
I have created a text file to store a few numbers. I just want to be able to read these numbers from the program at runtime. Also want to write to it at runtime.
Any ideas?
Thanks, Chris
Oh yeah.... I need to do this on the online version. Seems to work from the local version but not when I export online. I will post the code in the following comment.
import System.IO; var menuSkin : GUISkin; var color:Color;
private var levelToRead : String = "database.txt";
function OnGUI(){ var levelText : String; var maleScore : String[];
levelText = File.ReadAllText(Application.dataPath +"/"+levelToRead);
maleScore = levelText.Split(","[0]);//levelText;
GUI.skin = menuSkin;
//Printing out values here. Took it out because not enough characters in this comment
}
Perhaps more details about what you're trying to accomplish would help. Are you trying to store player scores locally on their computer? Globally, on a server? Store player progress on the machine?
Thanks for the help. Seems to work now. I just use a link and passed parameters ex: www.mysite.com/functionPage.php?value=1&type=2 etc... Then the php file updats the file.
To read everything this is what I did in case anyone needs it: var updateScoresLink="http://www.mysite.com/updateScores.php"; var updateScores = new WWW(updateScoresLink); yield updateScores;
//Put data in string var downloadText : String; downloadText = download.data;
Answer by Eric5h5 · Feb 14, 2010 at 10:17 PM
You can read and write text files as long as you store them on a server online, rather than locally.
Yes, this is what I wanted to do. To read a text file and write a text file online. In the same place as the Unity program I am making.
In that case, use the WWW class. Reading online data is simple, writing is harder, because you also need to do some server-side scripting (using PHP, Perl, or whatever) so the server knows what to do with the data you're posting.
Answer by Ashkan_gc · Feb 15, 2010 at 05:35 AM
you can use an online file with it's url but you can not use local files. i did not test if system.io works well with online files or not. you can use PHP scripts and www class to read/write the file. using online databases is possible too. take a look at http://www.mono-project.com/ODBC
can you explain this with the php script and the www class to me, i need this.
using www you can call urls. to write files using php you should learn the language http://www.tizag.com/phpT/filewrite.php this is a small tutorial for writing files. working with databases with php is easy but you should know how to write sql queries and ...! it's not something to be explaned in an answer!
Answer by Ricardo · Feb 14, 2010 at 10:08 PM
Not happening - it's a security issue. Webplayer distributions are not be allowed to write to the local filesystem.
You can read more on this answer.
Perhaps what you can can be accomplished with the use of preferences.
So you are saying, from the web game, you cannot update a text file data or retrieve the data that is already stored in a file online? If that is the case, is there a way to store and retrive the data from a database online?
Not quite. What I'm saying is that from the webplayer, you cannot write to a local file on the computer, which is what your sample code tries to do. If the information you wish to store is just a few characters (for instance, the player's last level), you can use the player preferences, which have a 1$$anonymous$$B data limit.
There are also ways to access online APIs via the WWW class, as Eric5h5 mentions on his note, but these are not Unity-specific. You would need to learn about how to create and secure a public web API. You could also connect directly to an open database via ODBC classes (also not a Unity-specific topic) but I don't recommend having your database connection open to the world on a server.
Answer by porglezomp · Apr 28, 2014 at 12:05 AM
I was trying to do this, and I found an easier way. If you really just want to store "a few numbers", you're probably better off using the PlayerPrefs methods than using a server.
Using this, you can store and retrieve floats, strings, and ints assosciated with some name, for instance: PlayerPrefs.SetInt("High Score", score);