i have a csv file in my resource folder which i need to read, anybody know how to do it? i tried resource.load textasset but it didnt work. What is a good workaround to that?
You can create a TextAsset by dragging a text file into your project.
Then you can drag it 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 pieces separated by commas */
var stringArray = myTextFile.text.Split(","[0]);
for ( var i = 0; i < stringArray.length; i ++ ) {
print("I am element number " + i + ": " + stringArray*);*
*}*
*}*
*```*
*<p>The argument passed to the String.Split function is a hacky way of specifying a single character in UnityScript. </p>*
*<p><strong>","[0]</strong> 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.</p>*
*<p>In other words, it's something like this:</p>*
*```*
*var theInputString = "Hello, world, I, like, commas...";*
*var theToken = ",";*
*var theArray = theInputString.Split(theToken[0]);*
*print(theArray.length);*
_/* output would be 5 */_
*```*
*<p>You probably want to split your file by newline characters to process it line by line. Then you would split each line by commas to process the data.</p>*
Any reason you have it in the Resources folder? Typically you'd just do:
var myTextFile : TextAsset;
as a public variable, then drag'n'drop.