In my game I have characters that talk to each other using speech ballons.
My goal is to write whatever sized text and have the GUI break it in pieces and display each part for about 5 secs.
So lets say you wanted to say something like:
“Watch some TV then, I’m going to finish the dinner, should be ready really soon. And if you want to set the table in the meantime that would be great!”
The game would split it into pieces and say each one like
Watch some TV then, - (wait 5 secs)
I’m going to finish the dinner, (wait 5 secs)
should be ready really soon. (wait 5 secs)
and so on…
This is what I managed to do so far.
This is the GUI for the text display. I’m using String.Length to know how big a line is and dividing it by the maxSize to know how many times I should break the text apart:
function OnGUI (){
//Basic definitions
GUI.skin = customSkin;
GUI.depth = 4;
//Converting selected actor into Screen coordinates.
var actorPosition = camera.main.WorldToScreenPoint(talkingActor.position);
//DialogGUI
if (showDialogGUI){
//gives you the Length of the GUI.Box
var textSize = GUI.skin.GetStyle ("Box").CalcSize (GUIContent(dialogLine));
var ballonWidth : float;
var ballonHeight = 45;
//Defines length of dialog ballon based on the size of the line.
if (textSize.x > 300){
ballonWidth = 300;
}
else {
ballonWidth = textSize.x;
}
//Defines how many times to break a dialogue based on the size of the text.
var stringSize = dialogLine.Length;
var maxStringSize = 47; //maximum size a dialogue line can have
var numberOfLines = stringSize/maxStringSize;
print(numberOfLines);
//displays the dialog line
GUI.BeginGroup (Rect (actorPosition.x -(ballonWidth * 0.7), actorPosition.y - 80, 500, 500 ), "");
GUILayout.Box (dialogLine);
GUI.DrawTexture(Rect(ballonWidth * 0.7, ballonHeight, 24, 24), balloonTip);
GUI.EndGroup ();
}
}
I then have some code on Update to define for how long the dialog shows up (in this case 5 secs)
function Update () {
//Dialog Display Timer
if (showDialogGUI){
timer += Time.deltaTime;
}
//Dialogue Display
if (startDialog == true){
timer = 0.0;
showDialogGUI = true;
startDialog = false;
}
if (timer >= 5){
timer = 0.0;
showDialogGUI = false;
}
}
And right now this works but I get the full gigantic line of text. How would I go about breaking it every line? Help very much appreciated!
// UPDATE //
So trying to find the solution I decided to try something like a for loop, so that knowing the amount of lines would try to run through each one of them. I got this code so far but it doesnt work. There really isn’t a Array for ‘numberOfLines’ (dont know how I can create one) nor do I have the timing controls.
var totalLines = numberOfLines;
var oldString = dialogLine;
var newString : String = "";
var startCharacter = 0; //used to know what line of text to start displaying from
for (i in numberOfLines){
if(totalLines != 0){
newString = oldString.Substring(startCharacter, maxStringSize);
GUI.BeginGroup (Rect (actorPosition.x -(ballonWidth * 0.7), actorPosition.y - 80, 500, 500 ), "");
GUILayout.Box (dialogLine);
GUI.DrawTexture(Rect(ballonWidth * 0.7, ballonHeight, 24, 24), balloonTip);
GUI.EndGroup ();
}
startCharacter += maxStringSize;
totalLines -=1;
}