- Home /
How can I replace the behaviour of a GUILayout.scrollView on MouseWheel event?
Hey there.
I'm writing an editor using an image displayed inside a scrollview. I already can Pan the image by clicking and draging the mouse inside the scrollview, but now I also want to be able to zoom in/out the image using the mouse wheel.
But I have a problem with that.
If I try to get the MouseWheel after GUILayout.BeginScrollView, nothing happens because the BeginScrollview intercept the MouseWheel event.
GUILayout.EndScrollView();
if(Event.current.type == EventType.MouseWheel)
{
// do stuff
Event.current.Use();
}
If I try to get the MouseWheel before GUILayout.BeginScrollView, I can get my MouseWheel event BUT it will be effective anywhere inside the window because I can't have the ScrollView rect until I called GUILayout.EndScrollView.
if(Event.current.type == EventType.MouseWheel)
{
// do stuff
Event.current.Use();
}
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
On solution is to recreate what happens inside the two ScrollView function, but I have no idea how it works, how the rects are managed, how the coordinates are sets, how to manage the scrollbars, etc.
So, what can I do?
Answer by Jamora · Aug 06, 2013 at 05:23 PM
You can use the normal GUI calls instead of GUILayout. That way you'll have your Scrollview Rect beforehand, because you can't display the scrollview without the Rect. You'll also get better performance if you then disable the useGUILayout flag for that script.
On the down side you must assign all Rects in your script, or take chances on everything displaying correctly by mixing and matching GUI and GUILayout calls.
If you want to make your own scrollview, the Unity's GUI provides Horizontal‖ical scrollbars which you can use.
Answer by Spidyy · Aug 06, 2013 at 11:29 PM
And it works using GUI. Defining the rect of the view is not easy but once it's done, it works like a charm.