Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by thraxST · Jul 09, 2013 at 03:57 PM · guieditoreditorwindow

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thraxST · Jul 09, 2013 at 04:53 PM 0
Share

So OnSceneGUI is only available if I inherit from Editor and I am trying to do this from an EditorWindow.

avatar image Brian@Artific · Jul 09, 2013 at 05:03 PM 0
Share

Sorry there - I've edited my answer to include EditorWindow.

avatar image thraxST · Jul 09, 2013 at 05:22 PM 0
Share

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.

avatar image Brian@Artific · Jul 09, 2013 at 06:09 PM 0
Share

Quite right - OnGUI will only get events from the current window, unlike OnSceneGUI. Your solution with SceneView below is very nice.

avatar image yoyo · Aug 20, 2013 at 08:45 PM 0
Share

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.)

avatar image
0

Answer by slake_it · Mar 05, 2016 at 04:54 AM

here are two scripts to solve this:

  1. it is auto initialized when unity editor starts (even when you don't select the game object of the script)

  2. it requires a game object in the scene tagged with "EditorMousePos"

  3. 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;
                 
             }
         }
    
    
    
    
    
    
    
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges