- Home /
Loading text into Gui.Box
I've searched couldn't find a clear answer. Maybe I missed it. I have a text area that i want to load text into from a text file that I've added as an asset. Is there a way in C# to load the text directly to the GUI.Box along with line breaks so it wont just be one long line of text in the Gui.Box?
Answer by Molix · Mar 11, 2011 at 05:59 AM
I just did a quick test and the line breaks came in no problem in a default GUI.Box from a TextAsset. e.g.
public TextAsset textAsset;
void OnGUI()
{
GUI.Box( new Rect( 0, 0, Screen.width, Screen.height ), textAsset.text );
}
Maybe if you're not seeing the line breaks there could be an issue with the text asset itself (line breaks often have platform specific quirks).
the file i'm trying to load is about.txt. Would I change textAsset to About or would i leave it named textAsset?
Add the script to a game object, then in the Inspector assign (e.g. by dragging from the Project window) the text file to the variable. Its just like if you were assigning an audio file or texture to those types of variables.
Thanks, it works. Only problem is the width and height. I tried to scale it down but it wouldn't wrap the words correctly. Is there a code I can add into a text file to make it have a new line?
Check out GUIStyle -- you can pass a style to the GUI/GUILayout functions. One of the options is to wrap text.
Thanks, the answer helped me ^_^. Anyway, How if i want to change the textAsset.text into textAsset2.text when i press the button?
Answer by Extrakun · Mar 11, 2011 at 07:01 AM
You can also use the WWW class to load resources from the hard disk, by using Application.dataPath
var www : WWW; var text_: string;
function LoadAndDisplay() { www = new WWW(Application.dataPath + "/file.txt"); yield www; text_ = www.text; }
function OnGUI() { if (text_) GUI.Label(new Rect(0,0,100,30), text_);
}
application.dataPath won't work on a web build will it? Trying to keep the code simple so it can go across windows and the web. Eventually $$anonymous$$ac also.
Your answer
Follow this Question
Related Questions
How to get info from a text file 1 Answer
Executing code from a text file and writing changes to it 1 Answer
How to save and load text in a GUI TextArea 1 Answer
Load and save text from web server 2 Answers
Problem Reading A Text File 2 Answers