- Home /
Story Text to GUI Boxes
Hello,
I'm attempting to have text that is cycled through a series of boxes with player interaction advancing to the next box with its block of text. Right now things are as below, and it works fine.
What I'd like to do is to write the text for the story, and have a function that converts that into the box text. Right now I'm thinking, where: box height = h, box width = w, character width = c, character height = H
at the character in the string at ((w/c)-1) appending "-"+"/n" if mid word or if a space just "/n" at that position, then after h/H lines cutting the text at the space nearest to the end of the line and starting from there in the next box.
I'm not entirely sure how to implement this though, does anyone have any suggestions?
Thank you for your time.
[EDIT] I did some more reading through the manual and found the wordWrap and lineHeight functions, which are a step in the right direction. Still unsure as to how to cut the string after a set number of lines and start a new box though.
public class Story : MonoBehaviour
{
public Texture boxTexture;
const int BOX_HEIGHT = 64;
const int BOX_WIDTH = 256;
const int SPEAKER_BOX_WIDTH = 64;
public int storyCounter = 0;
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
storyCounter++;
}
}
void OnGUI()
{
if (storyCounter == 0)
{
GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT, boxTexture);
GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Welcome (click to continue) ");
}
if (storyCounter == 1)
{
GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height -BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Text");
}
}
}
[SECOND EDIT] I think I'm making some headway.
public class Story : MonoBehaviour {
public Texture boxTexture;
const int BOX_HEIGHT = 64;
const int BOX_WIDTH = 256;
const int SPEAKER_BOX_WIDTH = 64;
public int storyCounter = 0;
public int maxLinesInBox;
string story = "text";
int storyLineLength;
public int LinesInBox(int lineH)
{
int linesInBox = BOX_HEIGHT / lineH;
return linesInBox;
}
void Update () {
if(Input.GetMouseButtonDown(0))
{
storyCounter++;
}
}
void OnGUI()
{
if (storyCounter == 0)
{
GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), "Welcome (click to continue) ");
Debug.Log(GUI.skin.box.lineHeight);
Debug.Log(GUI.skin.box.wordWrap = true);
Debug.Log(GUI.skin.box.clipping);
maxLinesInBox = LinesInBox((int)GUI.skin.box.lineHeight);
storyLineLength = story.Length / maxLinesInBox;
Debug.Log("story line length:" + storyLineLength + ", story string length: " + story.Length + ", max box lines:" + maxLinesInBox);
}
}
}
From here, I tried placing the below after the (storyCounter == 0) statement
for(int i = 0; i <= storyCounter; i++) {
char[] arr = story.ToCharArray((storyCounter-1) * (storyLineLength-1), (storyCounter) * (storyLineLength-1));
string temp = new string(arr);
GUI.Box(new Rect(0, Screen.height - BOX_HEIGHT, SPEAKER_BOX_WIDTH, BOX_HEIGHT), boxTexture);
GUI.Box(new Rect(SPEAKER_BOX_WIDTH, Screen.height - BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT), temp);
if (storyCounter * storyLineLength >= story.Length)
{
break;
}
It's sort of working, but it's throwing two argument out of range errors for the index, the first loop throws a "<0" error, the last throws "must be greater than the length of the string". It's showing the first loop's block of text, but not the last.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081910im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move the text in a GUI Button/Box 1 Answer
How do I get my GUI text to open on awake? 1 Answer
Trailing effect with GUI 0 Answers
GUI Text Score counter 3 Answers
Gui Text Script 4 Answers