x


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

more ▼

asked Feb 14 '10 at 09:34 PM

cmos gravatar image

cmos
467 29 40 54

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.

Feb 14 '10 at 09:49 PM cmos

import http://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

}

Feb 14 '10 at 09:50 PM cmos

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?

Feb 15 '10 at 08:12 AM Ricardo

Thanks for the help. Seems to work now. I just use a link and passed parameters ex: http://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;

Feb 16 '10 at 01:10 AM cmos

Sorry, the code formatting doesn't stay in the comment.

Feb 16 '10 at 01:11 AM cmos
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can read and write text files as long as you store them on a server online, rather than locally.

more ▼

answered Feb 14 '10 at 10:17 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

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.

Feb 14 '10 at 10:25 PM cmos

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.

Feb 15 '10 at 12:39 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 15 '10 at 05:35 AM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

can you explain this with the php script and the www class to me, i need this.

May 03 '11 at 12:10 PM Zhapness

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!

May 03 '11 at 03:08 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Feb 14 '10 at 10:08 PM

Ricardo gravatar image

Ricardo
5.2k 20 32 96

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?

Feb 14 '10 at 10:28 PM cmos

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 1MB data limit.

Feb 15 '10 at 08:05 AM Ricardo

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.

Feb 15 '10 at 08:06 AM Ricardo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x831
x60
x16

asked: Feb 14 '10 at 09:34 PM

Seen: 10810 times

Last Updated: Feb 14 '10 at 10:18 PM