Search Unity

Reading and Writing to a .txt file from a WebPlayer in C#

Discussion in 'Scripting' started by g-henshall, Mar 29, 2015.

  1. g-henshall

    g-henshall

    Joined:
    Jan 21, 2015
    Posts:
    4
    I am trying to read settings from a .txt file stored on my Mac and use them in a web app.

    I would then like to save a rating from the app in a new .txt file in a specific folder on the mac.

    Any ideas how I would go about doing this.

    I have looked at WWW and PlayerPrefs but can't really get my head around either.
     
  2. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751
    WebPlayers do not have direct file access. This is a security feature.

    You can still read/write to playerprefs though.
     
  3. g-henshall

    g-henshall

    Joined:
    Jan 21, 2015
    Posts:
    4
    So basically I won't be able to what I want to?

    Doesn't the PlayerPrefs store the info onto the local machine and not onto a server?

    Is there a simple way to do a client/server based web app?
     
  4. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Yes PlayerPrefs saves it to local machine almost just like your .txt would have been. Your original post makes not much sense what are you trying as ratings sound more like server data and not local.

    If you want users to give ratings and store them in one place you need a webhost with a database access and create a webservice to handle the data between webplayer and database. You use the webservice with the WWW class.
     
  5. g-henshall

    g-henshall

    Joined:
    Jan 21, 2015
    Posts:
    4
    Sorry, I clearly didn't ask my question very well.

    What I am trying to achieve is:
    The player (anywhere in the world) runs my webapp (from a simple wordpress.com page)
    The webapp will pull in the parameters for their game from a .txt file stored on the server.
    They are asked to rate what they see on a scale of 0 - 5
    This rating is then saved into a directory on the same server as before.

    Hopefully this now makes more sense?
    So like you said Ostwind, would I still need a web host with database access?
     
  6. aiab_animech

    aiab_animech

    Joined:
    Jul 4, 2012
    Posts:
    177
    Fetching a file from a server is no problem.
    Just do it like this:

    IEnumerator ReadFileAsync(string fileURL)
    {
    WWW fileWWW = new WWW(fileURL);
    yield return fileWWW;

    // now you have your file's text, you can do whatever you want with it.
    string textFileContents = fileWWW.text;
    }

    Saving the rating can definitely be done without a database, but it would be far less handy to work with.
    If you have a php-server, you can check this tutorial on writing files:
    http://php.net/manual/en/function.file-put-contents.php

    Then you can use WWWForm, or maybe even simpler with a GET-approach (if it's just a rating). If you're using the GET-approach you can do a WWW with a URL-argument.
     
    Last edited: Mar 29, 2015
    angrypenguin and NomadKing like this.
  7. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Related question - can you build some kind of application on your desktop that could hook into the web app to save data locally? If you're making a voxel game and saving the chunks, playerprefs seems like a bad way to do that. A database might work, but that would either be huge values or too many rows per player.

    Of course for something like that I'd probably just deploy to desktop, but webgl could be more attractive and get more views online.
     
  8. aiab_animech

    aiab_animech

    Joined:
    Jul 4, 2012
    Posts:
    177
    A desktop application can use both WWW for web access and normal System.IO File-handling for local access.

    (Note: but web applications can't talk to desktop applications. It was possible with NPAPI-plugins, but those days are over.)
     
  9. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Darn, that's what I was curious about. Any chance the desktop app could read playerprefs? Like...

    playerprefs var 1 - outgoing data stored here, begins writing and sets var 2 to value '1' when it is read as '2'
    playerprefs var 2 - set to 1 when writing, is set to 2 by a desktop program to tell the program to continue writing to var 1

    Might be kind of inefficient, but would it work?
     
  10. aiab_animech

    aiab_animech

    Joined:
    Jul 4, 2012
    Posts:
    177
    I think that with a good serializing algorithm you could store the voxels online. The voxel-information-map will be almost like an image, and images are fine to store in loads right? Ok you will have a 3rd dimension, but on the other hand you wont need as many bits for each voxel as an image needs. Remember, you can still run all kinds of packign algorithms if you need to lower the size.
     
  11. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @Horp1 what about my silly idea? Would that work? I may test it later, but it would be convenient to know now :p
     
  12. aiab_animech

    aiab_animech

    Joined:
    Jul 4, 2012
    Posts:
    177
    That would be possible technically, but it would require your users to start a desktop app while running your webapp. Might as well make the whole game a desktop app then?
     
  13. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I probably would if I needed local file access, but I find it interesting that it's possible. Thank you.

    It would be odd to run a desktop app alongside the webapp, but you might get away with it if the desktop app was some kind of offline companion app with minigames to affect the saved files :)
     
  14. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,148
    Tomnnn likes this.
  15. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Haha yea, I've seen that a few times now. If you're going for a minecraft sort of thing, you'd need to store 4 little numbers per block

    x y z id

    I'm sure there's ways to store large sections of land as algorithms, as horp1 may have been suggesting (dunno, I'm kinda dumb), but you still need individual block data if the player is modifying things voxel by voxel.
     
  16. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    A local application? I doubt it. What I've done in the past is write a web service that allows the Unity app to send the client PC downloads via the web page it's embedded in. So, the Unity app sends data to the server, the server checks that it's not junk, formats it appropriately and offers it to the client browser as a file download.

    Edit: Actually, since you can make stuff like text editors in JavaScript (the web kind, not the Unity kind) and they can save local files, and Unity WebPlayers can call JS scripts in their containing page, is there anything to stop you from writing a JavaScript that lets a Unity WebPlayer application write stuff out via that?
     
  17. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    That sounds a lot less clunky than my playerprefs idea if it's doable.
     
  18. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    @angrypenguin @Tomnnn
    The both of you really piqued my interest in just what was possible with passing data back and forth between the client machine and the WebPlayer/WebGL. I ended up taking the idea and running a little ways with it, you can observe the fruits of my efforts below.

    http://198.199.111.210/~lorenalexm/BrowserFilesPOC/

    There is no verification in place, as this was a simple experiment, but the upload form requires a text file to be parsed - with the basis already in place support for JSON or SQLite data is fairly trivial to add. Secondly, most browsers will load the data back from the application as a new webpage instead of downloading it automatically. This is a fairly simple fix, but ultimately this was only a proof of concept.

    If either of you have any questions, please feel free to ask!
     
    Last edited: Mar 30, 2015
    angrypenguin and Tomnnn like this.
  19. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @lorenalexm haha, it worked! Wasn't hard to figure out how to use either, nice job for a proof of concept version 1.0!

    Bravo :) I'm glad this is doable instead of my silly idea lol.

    --edit

    Hmm... is it possible to make the javascript part of the page function within the unity code? I'm just wondering if a web portal would be willing to let you place & run your own JS code outside of the unity app. Could my dumb idea still have merit? :eek:
     
    Last edited: Mar 30, 2015
  20. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    @Tomnnn I haven't had a chance to look into anything regarding the second part of your message as of yet. But as far as I understand the player is essentially sandboxes, and I believe it would be very difficult if not impossible to get it to function without some sort of control over the actual page the player is sitting in. To echo what you said, I doubt many portals would be willing to provide you access to modify the actual page your player was sitting on unless you were providing significant revenue to them.

    That being said, it wouldn't be difficult to write a web application that you run on your server which communicates with the player and facilitates the writing and reading from either a database or actual JSON files. You'd loose the ability to have the user upload and download the files such as in my proof of concept, but everything would be kept on your server and could then work seamlessly between mobile, desktop and even a Webplayer/WebGL using a few simple API calls.
     
    angrypenguin likes this.
  21. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    That'd be interesting too. I'm glad this forum topic has produced 2 and a half viable solutions for storage.
     
  22. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    It wasn't JSON in particular, but Master Thief had a level editing/sharing feature that worked exactly like this.
     
    Tomnnn likes this.
  23. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    My apologies for bringing this thread back from the dead, but in attempt to not leave the initial question hanging without providing a true resolution I have rewritten and opened the code I used for the sample posted earlier. Please see the link for a vague detailing on the script, and the GitHub repo shown within for the code and updated demo.

    http://forum.unity3d.com/threads/webgl-read-write.336171/

    Any questions or comments to the nature or code of the sample should be directed to the above thread; it is time to let this thread rest in peace.
     
    Tomnnn likes this.