- Home /
GUILayout Window consume mouse events
Hey guys
I'm making a GUILayout Window in the scene view for my custom editor:
GUILayout.Window(s_WindowId, guiRect, DrawWindow, GetTitle(), windowStyle);
This is working great:
My problem is that it doesn't quite behave like the window drawn by the Camera.
For example, the Camera Window never receives focus. Additionally, clicking the title in the Camera Window does not deselect the GameObject.
I suspect the solution to this problem is to somehow consume the mouse events, but I have little experience in this area.
Can anyone point me in the correct direction as to how I could implement this?
Thanks, Ves
Answer by VesuvianPrime · Oct 02, 2014 at 04:37 PM
Alrighty, my friend .NET Reflector came to the rescue. I ended up stealing this from SceneViewOverlay:
private void EatMouseInput(Rect position)
{
int controlID = GUIUtility.GetControlID(s_WindowId, FocusType.Native, position);
switch (Event.current.GetTypeForControl(controlID))
{
case EventType.MouseDown:
if (position.Contains(Event.current.mousePosition))
{
GUIUtility.hotControl = controlID;
Event.current.Use();
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == controlID)
{
GUIUtility.hotControl = 0;
Event.current.Use();
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == controlID)
Event.current.Use();
break;
case EventType.ScrollWheel:
if (position.Contains(Event.current.mousePosition))
Event.current.Use();
break;
}
}
Furthermore, to lose the "bevelled" style I needed to make my own GUIStyle:
s_WindowStyle = new GUIStyle(GUI.skin.window);
s_WindowStyle.onNormal = s_WindowStyle.normal;
Your answer
![](https://koobas.hobune.stream/wayback/20220613164240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Main window mouse event 2 Answers
GUI.Window error. InvalidOperationException: Hashtable.Enumerator: snapshot out of sync. 0 Answers
Problem resizing a GUILayout window 1 Answer
OnMouseEnter() over environment GameObject during the game 1 Answer
Difference between OnPointerEnter and OnMouseEnter 3 Answers