That sounds like it would be more resource intensive than calling a method in OnGUI.
We’ll start with a simple calculation in the Editor block of Dialogue.cs
public Rect CalculateNeededWindowSize()
{
Rect rect = new Rect(0,0,Screen.width, Screen.height);
rect.xMax =
foreach (var dialogueNode in GetAllNodes())
{
rect.xMax = Mathf.Max(rect.xMax, dialogueNode.GetRect().xMax);
rect.yMax = Mathf.Max(rect.yMax, dialogueNode.GetRect().yMax);
}
rect.xMax += newNodeOffset.x;
rect.yMax += newNodeOffset.x;
return rect;
}
Unless you have like 1000 nodes, this will execute fast. Note that I started with a space at least screen.Width/height, and I add the newNodeOffset.x at the end to ensure that there is some padding and that it is never too small. I originally used both the x and y, but this didn’t grow the window enough in the y direction when neede.
Then in DialogueEditor.cs, I made a change to the canvas calculation:
Rect bounds = selectedDialogue.CalculateNeededWindowSize();
Rect canvas = GUILayoutUtility.GetRect(bounds.width, bounds.height);
Texture2D backgroundTex = Resources.Load("background") as Texture2D;
Rect texCoords = new Rect(0, 0, canvas.xMax / backgroundSize , canvas.yMax / backgroundSize);
GUI.DrawTextureWithTexCoords(canvas, backgroundTex, texCoords);
That’s it. Growing and shrinking canvas.