Search Unity

Read/Write text files from Unity Webplayer

Discussion in 'Scripting' started by Vincent22, Mar 12, 2009.

  1. Vincent22

    Vincent22

    Joined:
    Mar 4, 2009
    Posts:
    3
    Hi all,

    my question is about reading or writing external files from Unity. I searched all topics about that and find solutions. They work fine until I build my application as a web application.

    Sure there are reasons that an application is not allowed to write to users drives from web. But how can I read or write text files from web.

    Code (csharp):
    1.  
    2. string path = Application.dataPath + @"/test.txt";
    3. Debug.Log("Pfad: " + path);
    4. if(!File.Exists(path)) {
    5.   StreamWriter sw = File.CreateText(path); 
    6. }
    7.  
    When I start the application in the IE I get following Exception in the web log:


    090312 14:46:36 ------------------------------------------------------------
    090312 14:46:36 Instance starting, version 2.5.0f1_21077
    090312 14:46:36 Datafile file:///E:/UserData/Georg/work/Unity/Levels/Assets/GuiText.unity3d
    Direct3D:
    Version: Direct3D 9.0c [nv4_disp.dll 6.14.11.6921]
    Renderer: NVIDIA GeForce 8800 GTS 512 (Omega 2.169.21)
    Vendor: NVIDIA
    VRAM: 512 MB
    Audio devices: Generic Hardware (default: Generic Hardware)
    web: load mono
    web: start, src=GuiText.unity3d abs=file:///E:/UserData/Georg/work/Unity/Levels/Assets/GuiText.unity3d
    Player: init engine
    web: sucessfully initialized
    090312 14:46:37 loader: start ok
    web: parent window change: 90768 1024x768
    desktop: 1280x1024 75Hz; virtual: 1280x1024 at 0,0
    Game Awake
    UnityEngine.Debug:Log(Object)
    Game:Awake()

    (Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1658)

    Loader Script Awake
    UnityEngine.Debug:Log(Object)
    Loader:Awake()

    (Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1658)

    Loader Script started
    UnityEngine.Debug:Log(Object)
    Loader:Start()

    (Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1658)

    Pfad: file:///E:/UserData/Georg/work/Unity/Levels/Assets/test.txt
    UnityEngine.Debug:Log(Object)
    Loader:Start()

    (Filename: ..\..\Runtime\Export\Generated\BaseClass.cpp Line: 1658)

    IOException: Win32 IO returned 50. Path: file:///E:/UserData/Georg/work/Unity/Levels/Assets
    System.IO.Directory.Exists (System.String path)
    System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize)
    System.IO.StreamWriter..ctor (System.String path, Boolean append)
    (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
    System.IO.File.CreateText (System.String path)
    Loader.Start ()

    (Filename: Line: -1)


    Thanks for help.
    regards Vince
     
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    974
    The web player cannot use filesystem stuff (this has been disabled on purpose). Use PlayerPrefs or WWW calls instead.
     
  3. Vincent22

    Vincent22

    Joined:
    Mar 4, 2009
    Posts:
    3
    Thanks for the fast replay.

    Ok, I tried it with WWW and it works. Here is the code:

    Code (csharp):
    1.  
    2.     void Start () {
    3.         Debug.Log("Loader Script started");
    4.        
    5.         string url = Application.dataPath + @"/Test.txt";
    6.         WWW request = new WWW(url);
    7.        
    8.         while(!request.isDone) {
    9.             Debug.Log("Loading...");
    10.         }
    11.        
    12.         Debug.Log("Data: " + request.data);
    13.        
    14.         // Initialize the Game class
    15.         Game.Instance.Setup();
    16.        
    17.         Application.LoadLevel(LevelToLoad);
    18.     }
    But how can I write to such a file with WWW?

    best regards
    Georg
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,534
    You can't just "write file to the internet". You usually send a WWW request to your server with some data, then some script on your web server processes the request parameters and creates a file (on server), or puts something into a database, or ...

    You can't write files into the player's machine in web player, period. If you want to store small amount of data (e.g. game preferences) on player's machine, use PlayerPrefs class.
     
  5. Vincent22

    Vincent22

    Joined:
    Mar 4, 2009
    Posts:
    3
    Ok, thanks a lot. That made things clear.

    I use it only as a test scenario for now, later we will have a database.

    best regards
    Vince
     
  6. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255

    This code reads the file "Test.txt" from the local application folder... right? It's not reading from the server... right?
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    36
    No, in the webplayer Application.dataPath returns the path on the webserver where the unity3d file is located. It wouldn't make much sense to return a local path since your application sits inside a security box. All you can access is your own stuff (from your webserver) which includes the website the player runs in.
     
  8. ben.06feb

    ben.06feb

    Joined:
    Dec 8, 2010
    Posts:
    9
    for a single system playerprefs working fine, if build stored in local system and played from other systems datas not carry forward from one to another system. How can i read/write file without having server which have database (php server)
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can't, anything that is meant to work across users or machines requires a server backend.

    the data the webplayer stores are local to the user account on the machine
     
  10. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    You could print a file as text in a textbox and print a copy to clipboard.

    you select all the text edit window, it can copy all txt, i.e. save object mesh files!. it may lag / crash on many kb / mb.
     
    Last edited: Jan 26, 2014
  11. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,182
    I would do this by writing a php script and posting to it, even once you get a database I would write to the database via php scripts.
     
  12. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    Please give details of above process.