The only example I could find in my projects is from a script I found on the net, maybe it can help you understand what’s possible when readin a text file.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class WriteToFile : MonoBehaviour
{
private Color varToGet;
private Color varToGet2;
private TextAsset textFileVar;
private string textFileToString;
void Awake() {
textFileVar = Resources.Load("Scripts/blubb") as TextAsset;
}
void Start() {
Main ();
textFileToString = textFileVar.text;
var stringToDic = ParseColorTable(textFileToString);
varToGet = stringToDic["skinColor"];
varToGet2 = stringToDic["hairColor"];
}
public static void Main() {
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("Assets/Resources/Testfiles/TestFile.txt")) {
Debug.Log ("writing to file");
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
}
System.IO.File.AppendAllText("Assets/Resources/Testfiles/TestFile.txt", System.String.Format("{0} {1} {2} {3} {4}", "string1", "string2", "float1", "float2", "float2"));
}
Dictionary<string, Color> ParseColorTable(string aText) {
Dictionary<string, Color> result = new Dictionary<string,Color>();
string[] lines = aText.Split('
‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');
// Remove all spaces and split the 4 color values
string[] values = S[0].Replace(" ","").Split(',');
// Parse the 4 strings into floats and create the color value
Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
// Read the colorname and remove leading or trailing spaces
string colorName = S[1].Trim();
result.Add(colorName,col);
}
}
return result;
}
Dictionary<string, Color> ParseFillTable(string aText) {
Dictionary<string, Color> result = new Dictionary<string,Color>();
string[] lines = aText.Split('
‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');
// Remove all spaces and split the 4 color values
string[] values = S[0].Replace(" ","").Split(',');
// Parse the 4 strings into floats and create the color value
Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
// Read the colorname and remove leading or trailing spaces
string colorName = S[1].Trim();
result.Add(colorName,col);
}
}
return result;
}
}