I’m following the Dialogue RPG course and I was wondering how I could make it that the textfield of the dialogue node gradually grows depending on how much text there is.
I tried to make it with a scrollbar, but it ended up affecting all scrollbars of each node in the selected Dialogue SO. Any help/ideas would be really appreciated
Scrollbars are next to impossible to get to work, and no matter what, they really won’t work inside of a Text Area (I’ve tried and tried with no success, there).
I do have a slight workaround that will make the field (and the node) grow automatically without a scrollbar, though it’s slightly tempermental…
First, we need to add a method in DialogueNode.cs to set the size of the Rect, as we’ll be growing/shrinking the rect based on the contents of the text.
public void SetRectHeight(float newHeight)
{
rect.height = newHeight;
}
Now for the tricky stuff… There’s not a tried and true way to tell exactly how many lines a TextArea should take, but we can make some guestimates. Head into the DrawNode() method in the DialogueEditor.cs script
private void DrawNode(DialogueNode node)
{
GUIStyle style = nodeStyle;
if (node.IsPlayerSpeaking())
{
style = playerNodeStyle;
}
string textFieldContent = node.GetText();
int charsPerLine = 20; //Adjust this to taste
int lines = (textFieldContent.Length / charsPerLine) + 1; //Guesses the number of lines
float fixedHeight = lines * GUI.skin.font.lineHeight * 1.2f; //Calculate box height
node.SetRectHeight(60+fixedHeight); //Adjust the node to support new box height
GUILayout.BeginArea(node.GetRect(), style);
//Create a style for the Text Area with the proper dimensions
var textAreaStyle = new GUIStyle(EditorStyles.textArea);
textAreaStyle.wordWrap = true;
textAreaStyle.fixedHeight = lines * GUI.skin.font.lineHeight * 1.2f;
textAreaStyle.fixedWidth = 150;
node.SetText(EditorGUILayout.TextArea(node.GetText(), textAreaStyle));
GUILayout.BeginHorizontal();
//remainder of the method
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.