- Home /
Answer by jahroy · Apr 22, 2011 at 09:07 PM
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[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 commas to process the data.
I get this error: BCE0019: 'text' is not a member of 'System.Type'.
Answer by Eric5h5 · Mar 31, 2011 at 08:18 PM
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.
This works for $$anonymous$$onoBehaviours but not for singletons.
Your answer
Follow this Question
Related Questions
How to create and write a text file after compile/build 0 Answers
Create Text File 3 Answers
Write file after build 1 Answer
How can I get game data from a specified file? 0 Answers
Error copying PNG 1 Answer