- Home /
write to a text file in resrouce folder
i have a text file in my resource folder which i can read. i am just wondering how can i write to it and save it?
this is the code i use to access my text file in the folder:
var tempAsset: TextAsset = Resources.Load("2011", TextAsset);
var myString: String = tempAsset.text;
It's impossible to write to a TextAsset (or indeed any other asset).
Answer by Statement · Mar 28, 2011 at 12:17 PM
Only through editor scripts. See AssetDatabase.SaveAssets and EditorUtility.SetDirty. Note that you can not call these functions from game scripts. It has to be in the editor. Otherwise you can check out this question which might be similar to your needs. It works on standalone players.
import System.IO;
var myString : String = "Write some text!"; var file : String = "somefile.txt";
Load();
function Load() { myString = File.ReadAllText(file); }
function Save() { File.WriteAllText(file, myString); }
Answer by Justin Warner · Mar 28, 2011 at 11:54 AM
http://forum.unity3d.com/threads/5084-Can-I-read-and-write-text-files-using-Javascript
That should help you a little... I recomend searchign around a little first =). But good job finding out how to read!
Answer by drudiverse · Aug 24, 2015 at 05:32 AM
in JS... THIS WORKS!:)
import System.IO;
function writeStuffToFile(){
var stuff : String = "stuff";
File.WriteAllText(Application.dataPath +"/stuff.txt",stuff);
}
function readStufFromFile(){
var stuff : String;
stuff = File.ReadAllText(Application.dataPath +"/stuff.txt");
Debug.Log(stuff);
}
@fafase - hi dude - they are BOTH WRONG, right?
You $$anonymous$$UST use
Application.persistentDataPath
particularly on Android, or you can't write.