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 reissgrant · Sep 02, 2010 at 02:23 AM · editorpositionmouse

Tracking the Mouse Position in the Editor

Debug.Log(Input.mousePosition);

does not work inside the editor. Is there a way to track the mouse in the Editor? Specifically, I'm trying to detect a mouse hit on an object in the scene, while in the Editor and I need the mouse position to do this. Any insight would be helpful. Thanks.

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

2 Replies

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

Answer by Jesse Anders · Sep 02, 2010 at 05:26 AM

The 'Input' class is not active in editor mode. Based on the examples I've seen, accessing of events in editor scripts is typically done by accessing 'Event.current' directly.

Also, if you're going to be doing picking in editor mode, be sure to use HandleUtility.GUIPointToWorldRay() to generate the pick ray, as using ScreenPointToRay() with the scene view camera doesn't produce the correct results (or at least that's what I've found).

Comment
Add comment · Show 3 · 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 reissgrant · Sep 02, 2010 at 01:41 PM 0
Share

Thanks, I'll look into this.

avatar image Whiteleaf · Jul 21, 2016 at 03:13 AM 0
Share

Been messing around with a custom terrain editor and this is the only solution I've found for this, thank you so much! I've been looking for half an hour!!

avatar image KnightWhoSaysNi · Aug 29, 2018 at 08:52 PM 0
Share

Thank you so much for this. I have been trying to figure this out for over 2hours now. HandleUtility.GUIPointToWorldRay(Event.current.mousePosition) works like a charm!

avatar image
1

Answer by Darkgaze · Aug 18, 2020 at 03:14 PM

I add some more info to the previous correct answer:

You can attach to the event:

 SceneView.onSceneGUIDelegate += OnSceneGUI;

Then write your own function:

 private void OnSceneGUI(SceneView sceneView)

Inside, take the mousePos using Event.current, instead of Input, which is disabled in Editor mode.

 Vector3 mousePosition = Event.current.mousePosition;
 Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);

Others say using ScreenPointToRay instead. This happens in other scenarios. In that case you have to flip Y.

 Vector3 mousePosition = Event.current.mousePosition;
 mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y; // Flip y
 Ray ray = sceneView.camera.ScreenPointToRay(mousePosition);
Comment
Add comment · Show 1 · 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 Bunny83 · Aug 18, 2020 at 03:47 PM 0
Share

That's actually a necessary update since a lot of those things you've mentioned didn't even exists when this question was asked / answered. Though be careful with na$$anonymous$$g your method OnSceneGUI because inside a custom editor (class derived from UnityEditor.Editor) this is already a callback that is called by Unity.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get position of the closest visible vertex to mouse in scene view? 1 Answer

Editor like position and rotation change of a game object 0 Answers

GameObject position not matching up to its position in the inspector even though they are clearly linked [answered] 1 Answer

Rotating object towards mouse point 1 Answer

Forcing all new GameObjects to a specific Position z-value 2 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