- Home /
"You can only call GUI functions from inside OnGUI." Each time I try to open a window after a recompilation when the window was opened?
This is really annoying - Say I have a EditorWindow
opened - and then make something that triggers a recompilation - I start to get "You can only call GUI functions from inside OnGUI." about my window (also for each time I try to open it from this moment on). The only way to fix this is to close the window and then trigger a recompilation again.
Why does this happen? -__-
Thanks!
EDIT: Code
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.Linq;
using System.Text.RegularExpressions;
public class SelectSceneWindow : EditorWindow
{
/* a bunch of GUIStyles... */
private SceneTransition transition;
private Object[] scenes;
private Object[] filteredResult;
public static void Show(SceneTransition transition)
{
var window = CreateInstance<SelectSceneWindow>();
window.Init(transition);
window.ShowUtility();
}
public void Init(SceneTransition transition)
{
this.transition = transition;
}
Vector2 scrollPosition;
string search = "";
private const float INDENT_WIDTH = 20f;
void OnGUI()
{
GUIHelper.HorizontalBlock(() =>
{
GUILayout.Label("Scenes", ScenesLabel);
search = EditorGUILayout.TextField("", search);
filteredResult = scenes.Where(s => Regex.IsMatch(s.name, search, RegexOptions.IgnoreCase)).ToArray();
if (GUILayout.Button(new GUIContent("↶", "Reload scenes"), RefreshButton, GUILayout.Width(20), GUILayout.Height(20)))
ReloadScenes();
});
scrollPosition = IUnifiedGUIHelper.ScrollViewBlock(scrollPosition, false, false, () =>
{
foreach (var scene in filteredResult) {
bool selected = scene.name == transition.scene;
var nextStyle = GetNextStyle();
IUnifiedGUIHelper.HorizontalBlock(selected ? SelectedStyle : nextStyle, () =>
{
GUILayout.Space(INDENT_WIDTH);
var spaceRect = GUILayoutUtility.GetLastRect();
GUILayout.Label(scene.name, selected ? SelectedLabel : UnselectedLabel);
var labelRect = GUILayoutUtility.GetLastRect();
var buttonRect = IUnifiedGUIHelper.CombineRects(spaceRect, labelRect);
if (!selected) {
EditorGUIUtility.AddCursorRect(buttonRect, MouseCursor.Link);
if (GUI.Button(buttonRect, "", GUIStyle.none)) {
transition.scene = scene.name;
}
}
});
}
});
}
void ReloadScenes()
{
scenes = Utils.GetAssets<Object>("Assets", "*.unity", SearchOption.AllDirectories);
filteredResult = scenes;
}
void OnEnable()
{
ReloadScenes();
}
}
What it looks like
Answer by Bunny83 · Feb 01, 2014 at 04:05 AM
You probably using some GUI stuff in OnEnable or some other callback. However, like the error tells you, you can only use GUI stuff inside OnGUI. Without seeing your code nobody can tell you what you've done wrong since all we know is what you've written here.
Feel free to **edit** your question and include more information.
Do you create your "bunch of GUIStyles" in the field initializers of your class? That's most likely the cause of your errors. You should initialize the GUIStyles inside OnGUI like this.
Can you add one or two example GUIStyles? Thanks