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
2
Question by stevethorne · Nov 11, 2014 at 02:15 PM · editoreventcursorguilayoutmouse position

Get mouse position in Editor based on screen coordinates

I have a button in an editor window, and when I click the button, I'd like to open a new editor window on top of that at the position of the button I clicked on. As far as I know the Event.current.mousePosition gives me the mouse position relative to the editor window I'm in and also relative to the guilayout. So, similar to GetLastRect, if I'm inside of a beginhorizontal or two, then the mouse position isn't even going to be relative to my current editor window.

My question is, how can I either get the mouse position or the button position in absolute coordinates ( not relative to the guilayout ) so that I can open the editor window at the location of the button I clicked.

If neither of these questions have answers, does anyone have any ideas for me to accomplish this problem a different way? Maybe finding the location of the button or mouse cursor isn't the way to solve this.

Please note that this is in the editor, needs to be able to run on mac and windows, and must work on multi monitor displays.

Comment
Add comment · Show 1
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 stevethorne · Nov 11, 2014 at 08:02 PM 0
Share

Perhaps there's a way to find the mouse position in .Net that doesn't rely on the System.Windows dll?

2 Replies

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

Answer by NiklasBorglund · Nov 11, 2014 at 08:10 PM

Edit 2:

Did some tests and this line of code seems to work for me, even when I have two monitors.

 GUIUtility.GUIToScreenPoint(Event.current.mousePosition)


Original: First thing that I can think of is that you could calculate the offset of the button rect from the window rectangle.

http://docs.unity3d.com/ScriptReference/EditorWindow-position.html

If you know the relative offset within the window, you should be able to calculate the screen position from the window's X & Y coordinates.

Edit: This might also help - http://docs.unity3d.com/ScriptReference/Event-mousePosition.html

Comment
Add comment · Show 6 · 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 stevethorne · Nov 11, 2014 at 08:17 PM 1
Share

That's definitely a good thought, but the button rect is relative to any beginareas beginhorizontals and what not. I have many of those nested in my window.

avatar image NiklasBorglund · Nov 11, 2014 at 08:34 PM 1
Share

I've updated my answer.

avatar image stevethorne · Nov 12, 2014 at 01:56 PM 0
Share

Awesome! That updated answer works with both multiple monitors and with guilayout begin horizontals and areas. Thank you!

avatar image stevethorne · Nov 12, 2014 at 02:15 PM 0
Share

It's also worth noting that GUIUtility.GUIToScreenPoint takes the current layout into account, so using this with GUILayoutUtility.GetLastRect will help you get the screen coordinates of the last rect even if it's within a beginarea or beginhorizontal. REALLY AWESO$$anonymous$$$$anonymous$$

avatar image Hassan-Kanso · Feb 22, 2016 at 10:29 AM 2
Share

Hello, what if there's no current event (it's null) ? is "mouse moving" an event? if you have a solution for that too please update your question, thank you:)

Show more comments
avatar image
0

Answer by TranquilMarmot · May 13, 2020 at 07:36 AM

I tried out GUIUtility.GUIToScreenPoint but could never get it to work right.

This is what ended up doing it for me:

 var actualScreenPosition = new Vector2(
    Event.current.mousePosition.x,
 
    // the Y position is flipped, so we have to account for that
    // we also have to account for parts above the "Scene" window
    Screen.height - (Event.current.mousePosition.y + 25)
 )

(This actually came from here: https://forum.unity.com/threads/getting-mouse-coordinates-in-editor-mode.41585/ )

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

30 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 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

GUI & GUILayout methods no longer work in OnSceneGUI() 1 Answer

How do I repaint/refresh/focus without calling Key Events multiple times? 1 Answer

check if 'B' key is being pressed while clicking in Editor window 3 Answers

How do I make an in-game object editor that can save them as assets? 0 Answers

How to add UnityEvent using Custom Inspector? 1 Answer


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