How can I do it? whatever I try, I get one line that doesnt fit inside of the box
Couldn’t you just make the box bigger or the text smaller?
If you use a GUIStyle for the box, you can set the text to Word Wrap which should restrict it to multiple lines inside the box. You’ll lose the default Unity default style as a result, but you can always add in a background texture of your own.
ok, thanks!
is there any way I can do that with the standard GUI?
can you just put \n between the lines?
doesnt work
You could do what I did, and have the GUIBox point to an external .txt file.
var dialogue : String;
// The dialogue of the NPC (i.e. what the NPC says)
// NOTE: the above variable should be left blank -- the dialogue will be assigned using the questText variables.
var questText1 : TextAsset; // Points to the text file that contains the NPC dialogue
dialogue = questText1.text; // Assigns the text file to the dialogue variable
Use the above code, and just replace the text you want to appear in the GUI Box code with the dialogue variable.
I am trying to keep it all in unity
Why? Game development is about getting things to work by whatever means necessary. You shouldn’t shun a working solution just because it functions in a particular way.
one: I am lazy
two: I am going to upload it to a games site so I dont think an external file will work.
It’ll work just fine if the text file is in your projects folder. Once you build your project, everything that your game uses will get compressed into the executable anyway.
also, that still doesnt fix what the problem was. I want to know how to make the text wrap with the default GUI style.
So you want to achieve something like this, correct?
yes, please
Alright, well that’s what my method accomplishes. Here, I’ll even give you the full code for it. All you have to do is drag it onto the mesh of any object (make sure it has a renderer attached), and then create a .txt file and drag it onto the QuestText1 variable in the Unity Inspector.
But if you really, really don’t want to use a plain text file in your project, you can just copy/paste some pre-formatted text into the Dialogue variable instead (keep in mind you’ll have to either remove or comment out the QuestText1 variable, as you’ll get an error if you don’t assign anything to it, and it overrides anything you type into the Dialogue variable).
Anyway, here’s the code (written in JavaScript):
var highlighted = false; // Used to determine whether or not NPC is highlighted
var talk = false; // Used to determine whether or not NPC is talking
var title : String; // Title of the textbox for the NPC dialogue
var dialogue : String; // The dialogue of the NPC (i.e. what the NPC says) -NOTE: this variable should be left blank -- the dialogue will be assigned using the questText variables.
var questText1 : TextAsset; // Points to the text file that contains the NPC dialogue (regular dialogue)
dialogue = questText1.text; // Assigns the text file to the dialogue variable
var textLength : int = 335;
// Determines the length of the dialogue text area
// Adjust textLength value if text is cut off or if there is too much empty space (best to do this in the Unity Inspector)
// Set default textLength to 335 if all text is visible without scrolling
var scrollViewVector : Vector2 = Vector2.zero; // Tracks and stores the position of the scrollbar
// Changes the color of the material while the mouse is over the mesh
function OnMouseOver ()
{
renderer.material.color = Color(1.5, 1.5, 1.5);
highlighted = true;
}
function OnMouseExit ()
{
renderer.material.color = Color(0.5, 0.5, 0.5);
highlighted = false;
}
// Detect left mouse click and print it in the debug log.
function OnGUI()
{
var click : Event = Event.current;
if(click.button == 0 click.isMouse)
{
if (highlighted == true)
{
//Debug.Log("Left Click");
talk = true;
}
}
if (talk == true)
{
//(horizontal position, vertical position, horizontal size, vertical size)
GUI.Box(Rect(50,50,325,400),title);
// Begin the ScrollView
//(h-position, v-position, h-size, v-size)
scrollViewVector = GUI.BeginScrollView (Rect (60, 75, 310, 345), scrollViewVector, Rect (0, 0, 90, textLength+10));
// Put something inside the ScrollView
innerText = GUI.TextArea (Rect (5, 5, 287, textLength), dialogue);
// End the ScrollView
GUI.EndScrollView();
// Close button
if (GUI.Button(Rect(350,53,22,15),"x"))
{
//Debug.Log("Closed the dialogue box.");
talk = false;
}
//(h-position, v-position, h-size, v-size)
if (GUI.Button(Rect(307,425,64,20),"Goodbye"))
{
talk = false;
}
}
}
thanx!