WIll “System.IO” also work on a Mac?
If somebody could explain exactly how this works, thanks
public TextAsset asset; // Assign that variable through inspector
private string assetText;
void Start() {
assetText = asset.Text; // The assetText variable contains the text
// If you're looking to extract data from that,
// I would say go with XML. Otherwise, use the
// System.Regex class
}
Writing to the text file is quite a bit more difficult. However, here is another method that requires a bit of setup and isn’t exactly good practice…make sure to add using UnityEngine
at the top. The text file you’re reading/writing from needs to go into a special directory called "Resources"
.
public string FileName; // This contains the name of the file. Don't add the ".txt"
// Assign in inspector
private TextAsset asset; // Gets assigned through code. Reads the file.
private StreamWriter writer; // This is the writer that writes to the file
void AppendString(string appendString) {
asset = Resources.Load(FileName + ".txt") as TextAsset;
writer = new StreamWriter("Resources/" + FileName + ".txt"); // Does this work?
writer.WriteLine(appendString);
}
In Update()
use the AppendString()
function to write text.
Yes, System.IO
works on Mac