Positioning GUI Button based on length of dialogue

I am making this fairly simple dialogue system, simple for now at least, and i want it so that regardless of the length of what needs to be said, the choice buttons are positioned correctly. By this i mean when i have the NPC say just, “Yes” The button is placed accordingly, while if i have the NPC go into a rant that requires scrolling i want the Buttons to be at the bottom of the rant. I have it set up so that the whole dialogue takes place in a window. Inside the window is a ScrollView, and inside the scrollview is the GUI Label i am using to display the text, and a GUI Button that closes the conversation. Here is the code i am working with:

private var windowRect : Rect = Rect (Screen.width/2 -400, Screen.height/2+200, 250, 300);
var scrollViewVector : Vector2 = Vector2.zero;
static var display : boolean = false;

function OnGUI () {
	if(display) {
		windowRect = GUI.Window (2, windowRect, WindowFunction, "NPC Name");
	}
}

function WindowFunction (windowID : int) {
		
		// Begin the ScrollView
		scrollViewVector = GUI.BeginScrollView (Rect (25, 43, 220,200), scrollViewVector, Rect (0, 0,0, 600));
		GUI.Label (Rect (25,0,170,550), "Dialogue");

		
		if(GUI.Button (Rect (35,96,80,30),"Close")) {
			display = false;
		}
		
		// End the ScrollView
		GUI.EndScrollView();
		GUI.DragWindow (Rect (0,0,10000,100));
		
}

It’s pretty simple, when the player hits the ‘Interact’ button (e), a script shoots a ray forward to see if there is a object tagged ‘NPC’. If it does, i plan on using scripts to go into the NPC that was hit, get a script titled something like ‘npcDialogue’ and that will contain the information that will send both the Dialogue, and the Name to this script to render it on screen.

For now, i have it set up so when i talk to NPC it simply tells this script to render the Window, inside is just some test dialogue. Now, what i want is the button to close it (which in the future may be more then one button that will lead to branching conversations) to be just below the text regardless of the length. This is my first time at truly using Unity’s GUI features so i do not know the best way to approach this. Sorry for it being so long winded!

Short Version: Have the button be just below the text contained inside of the GUI Label, regardless of what is inside the label.

That should be what you have now, given the code you posted. What do you see?

Perhaps you meant that you want the button to be below the label/text, but not within the scroll area? In that case, just move the Button() call after the EndScrollView().

What i am talking about is, i want the button in the scroll area, and i want to have it so that the player doesn’t have to scroll when it is just a one line conversation, while i also don’t want the button to be in the middle of the text when it does happen to be really long. The way it is coded now is, the button is roughly in the middle of the window, so it can have about 4 lines of dialogue before it is in the way.

Ah, I see; in that case, you need to adjust your button placement according to the size of the label required to hold the text. The easiest way to do that would be to switch from GUI to GUILayout, and let Unity figure out the size and placement of both for you.

function OnGUI () {
   if(display) {
      windowRect = GUILayout.Window (2, windowRect, WindowFunction, "NPC Name");
   }
}

function WindowFunction (windowID : int) {
      
      // Begin the ScrollView
      scrollViewVector = GUILayout.BeginScrollView (scrollViewVector);
      GUILayout.Label ("Dialogue");

      
      if(GUILayout.Button ("Close")) {
         display = false;
      }
      
      // End the ScrollView
      GUILayout.EndScrollView();
      GUI.DragWindow (Rect (0,0,10000,100));
      
}

(or something like that; untested…)