- Home /
The question is answered, right answer was accepted
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.
Answer by Bunny83 · Aug 05, 2011 at 09:28 PM
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('\n');
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("\n"[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#;)
@sharat: :D sorry, i've even waited 2h before i started to write the function ;)
Could someone translate it to java script? I don't understand C# code and especially when I see dictionary's because I've never used them before. And also do I need to import anything else besides System.IO?
Well, i'm using the generic Dictionary so you need "System.Collections.Generic" but i'm not sure if that namespace is automatically included in UnityScript or not.
I've converted it to UnityScript but haven't tested it yet...
... edit : Tested and works ;)
You have to import the System.Collections.Generic namespace with that line:
import System.Collections.Generic;
Answer by Meltdown · Aug 05, 2011 at 06:55 PM
You can do the following using C#
string line = "";
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
while ((line = file.ReadLine()) != null)
{
// Do what you want with the line of text read from the file here...
}
file.Close();
Right but do you know how to actually use the line of text read? because once I get the line of text read from the file it prints "RGBA(0.800, 0.533, 0.161, 1.000) skinColor" and that can't be applied to a material's color because of the "skinColor" word. Also the first line is different and so I need a way of just getting the first line and taking out "name" from it.
Answer by cj_coimbra · Aug 05, 2011 at 07:00 PM
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('\n');
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.
PS: StreamReaders won´t work on OSx or iOS, so you´d better stick with text assets...
Correction: string[] lines = fileRead.Split('\n'); You can throw other chars in the Split function to work as separators and help your parsing. The inverse bar isn´t appearing for some reasing so it is n
Right but do you know how to actually use the line of text read? because once I get the line of text read from the file it prints "RGBA(0.800, 0.533, 0.161, 1.000) skinColor" and that can't be applied to a material's color because of the "skinColor" word. Also the first line is different and so I need a way of just getting the first line and taking out "name" from it.
Remove the spaces contained inside the parenthesis then do another Split with (' ') empty char. Then you will separate the RGBA(0.800,0.533,0.161,1.000) from skinColor.
Follow this Question
Related Questions
Brightening the colour of a material that can be any colour without shaders. 1 Answer
Does Material.color, ParticleSystem.startcolor property clones/copies the material? 2 Answers
Pressing play Causes Re-instancing of material (Editor) 1 Answer
Instantiated object material colors 1 Answer
One fraction of game object one material, other fraction different material? 1 Answer