Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using GUI.BeginScrollView in OnGUI with ExecuteInEditMode

Discussion in 'Immediate Mode GUI (IMGUI)' started by greggman, May 17, 2016.

  1. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    The example taken directly from the Unity Docs

    http://docs.unity3d.com/ScriptReference/GUI.BeginScrollView.html

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6.   public Vector2 scrollPosition = Vector2.zero;
    7.   void OnGUI() {
    8.     scrollPosition = GUI.BeginScrollView(new Rect(10, 300, 100, 100), scrollPosition, new Rect(0, 0, 220, 200));
    9.     GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
    10.     GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
    11.     GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
    12.     GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
    13.     GUI.EndScrollView();
    14.   }
    15. }
    16.  
    Add ExecuteInEditMode and it stops working. The scroll bars don't work

    Any idea how to get them to work?
     
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    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.
     
  3. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    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.
     
  4. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    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
     
  5. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    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
     
  6. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    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.
     
  7. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    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
     
  8. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    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...
     
  9. greggman

    greggman

    Joined:
    Nov 30, 2013
    Posts:
    24
    I tried that I got an error about there being > 1 level or something or other. Anyway, thank you for the suggestion.