- Home /
reading a .csv file on a mac?
I made my dashboard and got everything done. now i want to read a .csv file with data like velocity, rpm, time etc and assign my dashboard's pointers to update after the values...
first things first, how can i actually read the file into unity and parse it properly ( a ";" is used to separate the values)
Answer by Graham-Dunnett · May 10, 2011 at 11:48 AM
Rename the csv to txt and then use the Text Asset asset. See http://unity3d.com/support/documentation/Components/class-TextAsset.html. The text variable will give you access to the contents of this file, and you should be able to use string functions to break the string into the values you need. (I guess using javascripts string.split function.)
i DO have another question though, when i want to actually use the values, how do i parse the file properly and access the values? i didn't find anything in the scripting reference
Answer by h4wkeye · May 12, 2011 at 08:52 AM
Well i am able to print the string now, even after parsing it it still looks the same when i print it though. Code:
var TXT_data : TextAsset; var newline : String = "\n";
function Update() { var TXT_parsedArray; TXT_parsedArray = TXT_data.text; TXT_parsedArray.Split(newline[0]); print("File Content : " + TXT_parsedArray); }
Now i want to write each line into an array. help?^^
Answer by jahroy · May 12, 2011 at 06:15 AM
You can create a TextAsset by dragging a text file into your project.
Then you can drag the TextAsset into the inspector to assign it to a public variable in your script:
/* public varialbe exposed in Inspector */
var myTextFile : TextAsset;
/ function that processes TextAsset /
function SomeFunction () {
/ split the contents of the file into an array of strings /
var stringArray = myTextFile.text.Split(";"[0]);
for ( var i = 0; i < stringArray.length; i ++ ) {
print("I am element number " + i + ": " + stringArray[i]);
}
}
The argument passed to the String.Split function is a hacky way of specifying a single character in UnityScript.
","[0] represents the 0th element of the one character String ",". As far as I know you can only use a character (not a String) to tokenize a string in Unity's version of Javascript.
In other words, it's something like this:
var theInputString = "Hello, world, I, like, commas...";
var theToken = ",";
var theArray = theInputString.Split(theToken[0]);
print(theArray.length);
/ output would be 5 /
You probably want to split your file by newline characters to process it line by line. Then you would split each line by semi-colons to process the data.
It might look something like this:
var newline : String = "\n"; var lineArray : String [] = myTextFile.text.Split(newline[0]);
var lineCount = 0;
for ( var thisLine : String in lineArray ) {
var wordArray : String [] = thisLine.Split(";"[0]);
lineCount ++;
print("Line number: " + lineCount);
var wordCount = 0;
for ( var thisWord : String in wordArray ) {
wordCount ++;
print(" Word " + wordCount + ": " + thisWord);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133158im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
What is a good unityscript writing program? 1 Answer
converting "int" to "string" 2 Answers
editor script makes editor crash 1 Answer