I am planning to use a lot of text (branching dialogues, item descriptions, etc.) in my project and I was wondering where I can store this text. I thought I’d put it in some external file and somehow access it in unity, but I haven’t found a way to do this yet. If anyone knows how to do it, I’ll appreciate it.
I haven’t gone too deep into them yet, but the Mono libraries that Unity uses have all sorts of features for file I/O, reading and parsing text. You might for example investigate the System.XML namespace, it contains a number of ways of getting XML text and parsing it.
Joachim, it’s nice to hear that
I haven’t taken a look at accessing other APIs from within unity (I guess you need to script in C# to access the mono libs right?), but It would be a nice idea to have xml integrated into unity somehow, because of it’s format flexibility. I can already imagine storing more stuff than just plain text inside those files… :roll:
You can also use javascript as it also uses the mono runtime. You can see the documentation on msdn.microsoft.com for information how to use the library. Here’s a link to the System.XML namespace documetnation on msdn.
Also take a look at the following: How to: Write Text to a File, How to: Read Text from a File and the System.IO namespace documentation
Thanks freyr, you’ve been most helpful.
Also, if you want to parse a plain-text file, REGEX (regular expressions) are your friend. .Net has good support for regex. (AFAIK the .net class for regex is: System.Text.RegularExpressions)
-Jeremy
I’m curious: are you doing anything special to use upper and lower case for your dialogue? I haven’t much experience yet but I’m planning to do a game with lots of text, like yours. I have been a little worried actually that variable screen resolutions might make readability difficult. Obviously, I have nothing to justify my fears but screenshots.
Marble, I haven’t actually tried to implement anything yet, I am only planning for now. I guess that it would be nice to have resolution-independent text, but it would be hard to implement.
However, have you considered using actual 3D (2D) objects for text characters? I am wondering how much would a page full of 3d text overlaying a 3d scene slow things down. I don’t know about you, but I don’t think I’ll have more than a single paragraph of text on-screen at once, so this might as well be a solution for me.
I’ve nothing but speculation. Wouldn’t writing all the text be a real pain if you had to do it by ordering objects?
Avernum 4 (and most RPGs of that ilk, like BGII, Fallout, etc.) deals with the problem by limiting the user to just a couple of resolutions that have similar aspects. I find even with A4 that only its native resolution at 1024x768 is tolerable for text-reading. Still no solution for upper and lower-case text, though (from what I’ve heard; I’m very good at getting proven wrong).
Looking forward to hearing how things turn out! I’m on hiatus while Apple takes their sweet time repairing my Powerbook.
You just need to create a font with both upper and lowr case characters in it. (And make sure the font is not set to fold cases.)
Here’s a link to a thread about creating fonts with upper and lowercase charachters
Thanks freyr!
Has anyone got a simple Unity-specific example showing:
a) Text I/O
b) XML I/O
Preferably:
- in JavaScript
- allowing you to specify exactly where the files will be in relation to the compiled app and/or project.
This seems like a really neat thing to have around. I suppose I’ll end up writing this myself, but I’m lazy…
For text, try this to start with. To specify paths, you use Application.dataPath, so if you had a folder called “Stuff” that was next to the compiled app, you can do something like
var path = Application.dataPath+"/../../Stuff/";
var filename = path+"MyFile.txt";
var sr = new StreamReader(filename);
//etc.
Keep in mind that the path is going to be different for OS X player/Windows player/the editor. But that’s what Application.platform is for…
–Eric
Thanks again Erich
For what it’s worth, here are my completed text routines:
import System.IO
static function SaveTextFile ( fileName : String, fileContent : String ) {
var sw : StreamWriter = new StreamWriter ( Application.dataPath + "/" + fileName );
sw.Write ( fileContent );
sw.Close ();
print ( "Saved " + Application.dataPath + "/" + fileName );
}
static function LoadTextFile ( fileName : String ) {
var t : String = "";
var line : String = "-";
try {
var sr : StreamReader = new StreamReader ( Application.dataPath + "/" + fileName );
line = sr.ReadLine();
while (line != null) {
t += line;
line = sr.ReadLine();
if ( line != null ) {
t += "\n";
}
}
sr.Close();
print ( "Loaded " + Application.dataPath + "/" + fileName );
}
catch (e) {
print ( "Error: " + Application.dataPath + "/" + fileName );
}
return t;
}
The files end up in the project Assets directory.