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?
2 Answers
2I 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).
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_);
}
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.
– MolixCheck out GUIStyle -- you can pass a style to the GUI/GUILayout functions. One of the options is to wrap text.
– MolixThanks, the answer helped me ^_^. Anyway, How if i want to change the textAsset.text into textAsset2.text when i press the button?
– jong