I thought it would be easy to dynamically lay out some windows in scene view, but the inner workings of Unity have me stymied.
I think the root of my problem stems from OnSceneGUI running twice before rendering the windows.
To see what’s happening:
- put a breakpoint at “CurWindow = GUILayout.Window (2, n…”
- select a gameobject that does NOT have OnSceneGUITester attached
- start debugging
- select the gameobject with OnSceneGUITester attached.
- notice how the windows are not yet rendered
- also note the value of WindowTop (150)
- continue execution
- on second break, note the value of WindowTop (231)
- continue execution
- the windows are rendered, but with tops at the first run of OnSceneGUI
I think it makes sense that the first Rects are rendered, since the second time GUILayout.Window is called, it will be ignored (or something) because the IDs are the same as first run.
What appears to be happening, to me, is Unity is initializing the window Rects and then calling OnSceneGUI again to change the Rect based on its contents.
I could hard code the Rect, but then I’d have to keep track of the heights of everything I put into each window.
Can anyone shed some light on why OnSceneGUI gets called twice?
Any workarounds that might let me dynamically layout the windows without having to manually track everthing in them?
****** Empty script OnSceneGUITester.cs:
using UnityEngine;
using System.Collections;
public class OnSceneGUITester : MonoBehaviour {
}
****** OnSceneGUITesterEditor.cs in Editor folder:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(OnSceneGUITester))]
public class OnSceneGUITesterEditor : Editor
{
float WindowTop;
public void OnSceneGUI ()
{
WindowTop = 100f;
Rect CurWindow = GUILayout.Window (1, new Rect (10, WindowTop, 50, 50), (id) => {
GUILayout.Label ("Some Label Text");
GUILayout.Label ("Some Label Text");
GUILayout.Label ("Some Label Text");
GUILayout.Label ("Some Label Text");
GUILayout.Label ("Some Label Text");
GUILayout.Label ("Some Label Text");
}, "title");
WindowTop += CurWindow.height;
CurWindow = GUILayout.Window (2, new Rect (10, WindowTop, 50, 50), (id) => {
GUILayout.Label ("Some Other Label Text");
}, "title");
WindowTop += CurWindow.height;
GUILayout.Window (3, new Rect (10, WindowTop, 50, 50), (id) => {
GUILayout.Label ("Some Other Label Text");
}, "title");
}
}