Using GUI.BeginScrollView in OnGUI with ExecuteInEditMode

The example taken directly from the Unity Docs

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
  public Vector2 scrollPosition = Vector2.zero;
  void OnGUI() {
    scrollPosition = GUI.BeginScrollView(new Rect(10, 300, 100, 100), scrollPosition, new Rect(0, 0, 220, 200)); 
    GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
    GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
    GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
    GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
    GUI.EndScrollView();
  }
}

Add ExecuteInEditMode and it stops working. The scroll bars don’t work

Any idea how to get them to work?

Maybe a bug in Unity but It makes little sense why adding ExecuteInEditMode would do that since that essentially just calls update and the other callback when the game is not in playmode. Try and insert a Debug.Log into the OnGUI and see if OnGUI gets called at al… Maybe your problem lies somewhere else in the project by your configuration and not in the Example class.

If I had to guess, I would say that the scrollPosition Vector is reset every frame, since it is initialized outside the scope of any function, hence it remains 0,0. You can try printing out the value of the variable at the beginning of OnGUI, or move the initialization to Start() and see what happens.

OnGUI is getting called because I see the ScrollArea, it just doesn’t work. I can’t scroll. As for the rest of my project, just make a new project, make one empty GameObject, make a new Script, paste in the code above and you’ll see the issue

Moving the initialization to Start didn’t help. Also it’s not always 0,0. It’s effectively some random-ish value. It just never changes once things have compiled. So I change a line in the script and save, Unity will recompile, the values will change but never change after that until the next recompile

So I took the code and tried this out, and I think the issue is a problem with the event pump in Unity.
Since the scrollPosition is a public var, you can alter it in the inspector, and notice how the position does change.
If you try and click the buttons however, you’ll see that they get focused, but do not react to a click. Same applies for the bars. So, either OnGUI was never meant to work in edit mode (which kind of makes sense), or it is broken, or the Unity event pump routes all input into editor classes first, and one of them eats up the input before OnGUI gets it.

In either case, you can’t get this to work. I’m not really sure why you would even want to have an OnGUI in edit time.
There are plenty of editor tools to get a GUI working in either windows, inspector, or the scene view.

Yes, I know my use case is an edge case. I’m trying to get a relevant “readme” to auto appear in each sample scene in a library/plugin I’m writing. I want it to appear in both the scene and game view because I don’t know which view the user will have open. I only want it to appear in edit mode. The user can delete the readme object or disable it if they don’t want to see the readme.

This way when I distribute a library with sample scenes opening a scene will present a relevant readme that is hard to ignore (because lots of people ignore readme’s if they have to go find them). This means happier users because they don’t have to go digging for docs and happier developers because of less support questions of the type “go read the docs here”

This was mostly working now except (1) the scroll areas aren’t working in the game view (2) no idea how to get a scroll area to appear in the scene view.


I switched to using a custom editor window.

which also has issues but different issues

Ok. To get a GUI working within the scene view, you can use Handles.BeginGUI, I’ve never tried it myself, but you should be able to get any gui item rendered and active that way.

As far as your game view, I haven’t been successful in getting input to work on GUI elements in edit mode. You could just make a call to the SceneView and make sure that window is visible.

Not sure if there is a question there…

I tried that I got an error about there being > 1 level or something or other. Anyway, thank you for the suggestion.