- Home /
Get Mouse Events with EditorWindow
I am trying to write a custome EditorWindow to handle adding a variety of objects and connected structures to my game environment. However, I have run into a problem where I can not figure out how to detect when and where the user clicks in the scene view when my editor window is active.
Is there anyway to get mouse events from an EditorWindow?
Answer by thraxST · Jul 09, 2013 at 05:45 PM
I was able to get the mouse events from the scene window by using the mostly undocumented SceneView class. In the OnEnable function I had to register a delegate with SceneView so I believe it is being called with each onSceneGUI from the Editor. This allows the EditorWindow to be called on scene events.
void OnEnable()
{
SceneView.onSceneGUIDelegate += SceneGUI;
}
void SceneGUI(SceneView sceneView)
{
// This will have scene events including mouse down on scenes objects
Event cur = Event.current;
}
Answer by Brian@Artific · Jul 09, 2013 at 04:48 PM
Get the current editor event within Editor.OnSceneGUI() or EditorWindow.OnGUI(). You can get the mouse position from the event directly, or use HandleUtility.GUIPointToWorldRay(Vector2) to check for collision with scene objects.
So OnSceneGUI is only available if I inherit from Editor and I am trying to do this from an EditorWindow.
Sorry there - I've edited my answer to include EditorWindow.
Strange last time I tired in in the OnGUI() method I was not getting any of the events, maybe it was not compiling, but it seems to be working now.
EDIT: Actually, it is still not working. I only get the $$anonymous$$ouseDown event for when I click on the actual EditorWindow, I can't get $$anonymous$$ouseDown events for when the scene is clicked on.
Quite right - OnGUI will only get events from the current window, unlike OnSceneGUI. Your solution with SceneView below is very nice.
Slightly off-topic, but note that if you need regularly updated Event.current.mousePosition inside the OnGUI method of an EditorWindow then you need to set your window's wants$$anonymous$$ouse$$anonymous$$ove field to true, and also issue a Repaint() call each time you get EventType.$$anonymous$$ouse$$anonymous$$ove. (See docs for details.)
Answer by slake_it · Mar 05, 2016 at 04:54 AM
here are two scripts to solve this:
it is auto initialized when unity editor starts (even when you don't select the game object of the script)
it requires a game object in the scene tagged with "EditorMousePos"
that game object must have the script EdotorMousePos.cs
public class EditorMousePos:MonoBehaviour { public Vector3 editorMousePos; public Vector3 editorMouseWorldPos; } [InitializeOnLoad] public class EditorMousePosEditor { static EditorMousePosEditor(){ SceneView.onSceneGUIDelegate += SceneGUI; Debug.Log("constructor created"); } static void SceneGUI(SceneView sceneView) { // This will have scene events including mouse down on scenes objects Event cur = Event.current; Vector3 mousePos=(Vector3)cur.mousePosition; mousePos.y = Camera.current.pixelHeight - mousePos.y; Vector3 worldPos=sceneView.camera.ScreenToWorldPoint(mousePos); EditorMousePos editorMoseuPos=GameObject.FindGameObjectWithTag("EditorMousePos").GetComponent<EditorMousePos>(); editorMoseuPos.editorMousePos=mousePos; editorMoseuPos.editorMouseWorldPos=worldPos; } }
Your answer
Follow this Question
Related Questions
Editor GUI Foldout header style customization 0 Answers
Custom Curve Editor? 0 Answers
How to draw button in editor window rotated by 90 degrees? 0 Answers
display editor script in the game window 0 Answers
Using Handles in EditorWindow 0 Answers