Reading a .txt File

So I made this script that allows you to change a color variable. When you click on a GUI button the variable gets written into a .txt file, and it looks like this. My question is how do I take one of these color variables from the file and apply the color to a game object’s material? I know it would be something like:

renderer.materials[0].color = ???;
renderer.materials[1].color = ???;
//and so on...

but I dont understand how to read the file and apply the color.

Can someone help me please? Thanks.

EDIT* This is for a standalone game.

You just have to parse the text file. It doesn't have a standard-format so you have to parse it yourself.

Usually i don’t write a whole script for others but i guess it might be useful for others:

// C#
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;
}

This function will return a Dictionary which can be used like this:

// C#
var playerColors = ParseColorTable(myTextFile);
renderer.materials[0].color = playerColors["skinColor"];
renderer.materials[1].color = playerColors["hairColor"];

The function is tested and works as long as the file doesn't contain some corrupted things. I don't do much error-checking. Lines that doesn't start with "RGBA(" are ignored


edit

This is just a quick on-the-fly convertion but should be correct:

function ParseColorTable(aText : String) : Dictionary.<String, Color>
{
    var result = new Dictionary.<String, Color>();

    var lines = aText.Split("
"[0]);
    for (var L in lines)
    {
        if (L.StartsWith("RGBA("))
        {
            // Cut "RGBA(" and split at ")"
            var S = L.Substring(5).Split(")"[0]);

            // Remove all spaces and split the 4 color values
            var values = S[0].Replace(" ","").Split(","[0]);

            // Parse the 4 strings into floats and create the color value
            var 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
            var colorName = S[1].Trim();

            result.Add(colorName,col);
        }
    }
    return result;
}

The usage is the same as in C#;)

You can do the following using C#

        string line = "";

        System.IO.StreamReader file = new System.IO.StreamReader("c:\	est.txt");
        while ((line = file.ReadLine()) != null)
        {
            // Do what you want with the line of text read from the file here...
        }

        file.Close();

Create a text asset in the game object and then drag and drop your txt file into the text asset slot in that game object.

string fileRead = myTextAsset.text;

Then use the fileRead.Split() function to parse your file. Example

string[] lines = fileRead.Split('
');

This will fill the array with your file lines. Then, after parsing the lines, you can parse each lines[x] element to do your logic. However you should think of a better set of separators so that the lines can parsed easily.