- Home /
How do I load my txt file in iOS
Hello! Ok, I'm pretty noobish so here we go. I've been at this for hours now, researching and testing but to no avail. I've got this working just fine in the editor, but when I build to my iPhone, it builds fine, but the text won't load. I'm guessing it's because it can't find the .txt file once it's built. I tried using resources.load, but frankly,I really can't figure this out.
So, I have a giant word list named "wordlist.txt". All words are separated by line breaks. I'm just trying to load a word from the file and pass it to the textmesh on the iOS. Thanks!
#pragma strict
import System.IO;
function Start () {
var fileName = "wordlist.txt";
var sr = new StreamReader(Application.dataPath + "/" + fileName);
var fileContents = sr.ReadToEnd();
sr.Close();
var mydata = fileContents.Split("\n"[0]);
// pick a random number between 1 and 10
var myrandom = Random.Range(1,100000);
GetComponent(TextMesh).text = mydata[myrandom];
}
Answer by robertbu · Oct 21, 2014 at 02:00 AM
If the text file is static (i.e. does not change after the app ships), just drop into your Assets folder and link it in as a TextAsset:
public TextAsset wordlist;
Just drag and drop your wordlist.txt file on this variable in the Inspector.
Thanks for the help! Ok, I'm struggling with this. I created a new c# script with the declaration public TextAsset wordlist and added it to my game object. No luck. (obviously because I'm doing it wrong. derp!) Do I need to alter my original script above in anyway or should it now be able to find it? I appreciate the help robertbu.
Your script above could look like this:
#pragma strict
public var fileContents : TextAsset;
function Start () {
var mydata = fileContents.text.Split("\n"[0]);
// pick a random number between 1 and 10
var myrandom = Random.Range(1,100000);
GetComponent(Text$$anonymous$$esh).text = mydata[myrandom];
}
You would take your 'wordlist.txt' file and place it anywhere in Assets but Resources or Strea$$anonymous$$gAssets. Once the above script is attached to a game object, select the game object in the hierarchy and then drag the 'wordlist.txt' file on the 'fileContents' variable in the inspector.
A note about your splitting. If you are authoring on Windows, you will have carriage returns at the end of lines as well, so you may want to trim the result:
GetComponent(Text$$anonymous$$esh).text = mydata[myrandom].Trim();
Bam. Thank you so much. I finally got it with your wisdom. $$anonymous$$uch appreciated!!!
Hey robertbu! I've been at this one all damn day and I'm frustrated that I'm here begging for help again. Not Giving up! :D Ok, so I'm implementing another function in my game. I want to take this var, mydata[myrandom], we generate in the above script, and use it with a cs text-to-voice script I'm using. I've read up (A lot) on the order Unity compiles (standard assets folder in first pass, etc) but I've tried a million different wrong ways and I'm getting errors, of course.
In a cs script I would call the function like this - TextToSpeech$$anonymous$$anager.SendTextToSpeech(stringname);
However, now matter what I try, I keep getting errors and can't compile. I'm not sure the best way to go about doing this, call the mydata[myrandom] from a new TextToSpeech$$anonymous$$anager cs script or call the TextToSpeech$$anonymous$$anager fro a js. I really don't care how it's done as long as it get's done. I'm thinking to convert mydata[myrandom] to a string a just call it from a cs script, but no matter what I try, I'm gettting the type or namespace name "randWordGen" (the script above) cannot be found error.
anyone-Thanks in advance for your help!
Your answer
Follow this Question
Related Questions
load file from Xcode 1 Answer
GetComponentInChildren is not a member of 'Object' 1 Answer
Save object position (random) 1 Answer
How to load a sprite via WWW on IOS? 1 Answer
The best way to save and load data for mobile in unity ? 1 Answer