Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by cmos · Feb 14, 2010 at 09:34 PM · webplayerfile-iopreferences

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Feb 14, 2010 at 09:49 PM 0
Share

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.

avatar image cmos · Feb 14, 2010 at 09:50 PM 0
Share

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

}

avatar image Ricardo · Feb 15, 2010 at 08:12 AM 0
Share

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?

avatar image cmos · Feb 16, 2010 at 01:10 AM 0
Share

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;

avatar image cmos · Feb 16, 2010 at 01:11 AM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Feb 14, 2010 at 10:25 PM 0
Share

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.

avatar image Eric5h5 · Feb 15, 2010 at 12:39 AM 1
Share

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.

avatar image
2
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zhapness · May 03, 2011 at 12:10 PM 0
Share

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

avatar image Ashkan_gc · May 03, 2011 at 03:08 PM 0
Share

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!

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmos · Feb 14, 2010 at 10:28 PM 0
Share

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?

avatar image Ricardo · Feb 15, 2010 at 08:05 AM 0
Share

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.

avatar image Ricardo · Feb 15, 2010 at 08:06 AM 0
Share

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.

avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

File IO on a server file from the web-player 2 Answers

Accessing arbitrary files within a web build 1 Answer

Game External Files on the Unity Web Player? 1 Answer

File manipulations in the unity web player. 1 Answer

WebPlayer call a server php script issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges