7

Event.Use() and Scroll Views

 3 years ago
source link: https://matthewminer.com/2009/08/07/event-use-and-scroll-views
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Matthew Miner

Event.Use() and Scroll Views

August 7, 2009

Today’s Unity discovery: calling Event.Use() every time the OnGUI() function runs will cause scroll views to not work. At least, they won’t respect any dimensions you set and will stretch to accommodate the elements contained within it.

In code form, I had something like this:

Vector2 scroll;

void OnGUI () {
    scroll = EditorGUILayout.BeginScrollView(scroll, GUILayout.Width(50), GUILayout.Height(200));
        GUILayout.Label("I like big labels and I cannot lie. You other brothers can't deny.");
    EditorGUILayout.EndScrollView();

    if (Event.current.type == EventType.KeyDown) {
        Debug.Log("You've pressed a key. Righteous.");
    }

    Event.current.Use();
}

The scroll view is most certainly not 50 pixels wide. Moving Event.current.Use() inside the if statement fixes it.

if (Event.current.type == EventType.KeyDown) {
    Debug.Log("You've pressed a key. Righteous.");
    Event.current.Use();
}

And all is well.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK