Can we configure BeginScrollView to detect the dialogue node positions?

So I just got through part of the course where we set the massive 4000 by 4000 size for the scroll view. But is there not a way to modify the beginscrollview to detect the dialogue nodes? As you said the nodes have an auto layout with the rect setup. Is there no way to connect that information and avoid using this long expand?

Then if so we don’t run into problems of having the change the size each time if our dialogue tree gets to long and we need more or less space.

You could calculate the bounds of the area taken by the nodes by finding the farthest Rect.xMax and the farthest rect.yMax and use that (plus maybe a little padding) to determine the size of the scroll view. You would need to do this each frame.

If I do this then I could call the method in an update function. Though, thinking about it, that sounds like it could be more resource heavy were as having a set size as we have now would be easier on the system.

Editors do not have Update() callbacks. Neither do ScriptableObjects.

Then I would need to have a call to reach the editor scripts somewhere else where update can be called then.

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.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms