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.
string path = Application.dataPath + @"/test.txt";
Debug.Log("Pfad: " + path);
if(!File.Exists(path)) {
StreamWriter sw = File.CreateText(path);
}
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
The web player cannot use filesystem stuff (this has been disabled on purpose). Use PlayerPrefs or WWW calls instead.
Thanks for the fast replay.
Ok, I tried it with WWW and it works. Here is the code:
void Start () {
Debug.Log("Loader Script started");
string url = Application.dataPath + @"/Test.txt";
WWW request = new WWW(url);
while(!request.isDone) {
Debug.Log("Loading...");
}
Debug.Log("Data: " + request.data);
// Initialize the Game class
Game.Instance.Setup();
Application.LoadLevel(LevelToLoad);
}
But how can I write to such a file with WWW?
best regards
Georg
Aras
March 12, 2009, 2:44pm
4
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.
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
anon_38579998:
Thanks for the fast replay.
Ok, I tried it with WWW and it works. Here is the code:
void Start () {
Debug.Log("Loader Script started");
string url = Application.dataPath + @"/Test.txt";
WWW request = new WWW(url);
while(!request.isDone) {
Debug.Log("Loading...");
}
Debug.Log("Data: " + request.data);
// Initialize the Game class
Game.Instance.Setup();
Application.LoadLevel(LevelToLoad);
}
But how can I write to such a file with WWW?
best regards
Georg
This code reads the file “Test.txt” from the local application folder… right? It’s not reading from the server… right?
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.
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)
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
frogsbo
January 26, 2014, 8:51am
10
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.
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.
frogsbo
January 27, 2014, 1:10pm
12
Please give details of above process.