I have my desired txt files which I want to use as TextAssets. I need these files to be usable at runtime by my other scripts. Now the issue is that I can not figure out a way to make these things work.

I know that I should be using the Assets/Resources or the Streaming Assets folder but for some reason things are not working properly. Is there a way to incorporate it all with StreamWriters and Filestreams? What about TextAssets assigned in Unity Editor, can those also be setup as Streaming? Some examples of code that uses my assets:

 public void TaskOnClick() //getting multi-values { string filename = "Assets/Resources/TempoText/multi-export.txt"; using (StreamWriter writeFile = new StreamWriter(filename, false)) { foreach (string inputJson in File.ReadLines("Assets/Resources/TempoText/multi-import.txt")) { string temperature = GetTemperatureByRegex(inputJson); Debug.Log(temperature); writeFile.AutoFlush = true; Console.SetOut(writeFile); writeFile.WriteLine(temperature.ToString()); } } File.Copy("Assets/Resources/TempoText/multi-export.txt", "Assets/Resources/multi-export.txt", true); } //or FileStream filestream = new FileStream("Assets/Resources/TempoText/multi-import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); var writeFile = new StreamWriter(filestream); { var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit: limit).ForEachAsync(d => Console.WriteLine(d)); //displays last 10 entries Debug.Log(document.ToString()); writeFile.AutoFlush = true; Console.SetOut(writeFile); writeFile.Write(document.ToString()); } 

All help greatly appreciated, I've basically messed up big time since I only found out about this now when I built everything as is...

Edit: got the streamwriters to do everything nicely with Application.persistentDataPath! Now stuck with a problem that I already struggled with - how to assign a TextAsset to get the file from a fixed path...

public TextAsset textFile; 

Wondering how to set this to get it's .txt from Application.persistentDataPath

8

1 Answer

Application.persistentDataPath is what was needed all along.

Something nobody ever mentioned wherever I looked around. Hope somebody will be able to find the correct way using this mess of a question and lackluster answer.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.