Event.Use() and Scroll Views

“A scroll view that tries to reveal its content, how utterly pointless.” — Matthew Miner

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 simply stretch to accommodate the elements contained within it. “A scroll view that tries to reveal its content, how utterly pointless.” Those were my exact words.

In code form, I had something like this:

private Vector2 scroll = Vector2.zero;

void OnGUI () {
	scroll = EditorGUILayout.BeginScrollView(scroll, GUILayout.Width(50), GUILayout.Height(200));
		GUILayout.Label("I like big labels and I can not 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 though.

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

And all is well.