Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by _Morgenstern_ · Jun 16, 2016 at 08:37 PM · physics2dcustom editormouseclickcustom-inspector

Detect Mouse Click Over BoxCollider2D in Scene View (Custom Editor)

Hey guys I've seen a bunch of similar topics to mine but none seem to solve my issue. I've got a bunch of BoxCollider2D objects (I'm creating a tile-based game) in my scene and am trying to use a custom editor script to detect mouse clicks on them. Here's what I have:

 private void OnSceneGUI()
 {
     Event e = Event.current;
 
     if (e != null && e.type == EventType.MouseDown && e.button == 0)
     {
             Debug.Log("Mouse down!");
 
             Ray r = Camera.current.ScreenPointToRay(e.mousePosition);
 
             RaycastHit2D hitInfo = Physics2D.GetRayIntersection(r, 10f, 1 << LayerMask.NameToLayer("Tile"));
             if (hitInfo.collider != null)
             {
                     Tile t = hitInfo.collider.GetComponent<Tile>();
                     if (t != null)
                     {
                         // Do stuff...
 
                         e.Use();
                     }
                 }
             }
         }

Problem is that it's detecting mouse clicks on tiles NOWHERE NEAR the mouse. Any idea what I'm doing wrong? I'm sure it's something super obvious but I've been here for 2 nights now and still can't see it.

Comment
Add comment · Show 2
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 _Morgenstern_ · Jun 16, 2016 at 08:39 PM 0
Share

For the record I've also tried:

 Collider2D col = Physics2D.OverlapPoint(Camera.current.ScreenToWorldPoint(e.mousePosition), 1 << Layer$$anonymous$$ask.NameToLayer("Tile"));
                 if (col)
                 {
                     Debug.Log("Hit: " + col.name, col);
 
                     Tile t = col.GetComponent<Tile>();
                     if (t != null)
                     {
                         //Debug.DrawRay(r.origin, r.direction, Color.red, 100f);
                         t.selected = true;
                         e.Use();
                     }
                 }

With no more success :/

avatar image Neo_kim · Jun 17, 2016 at 12:24 AM 0
Share

You can use "EventTrigger".

1 Reply

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

Answer by _Morgenstern_ · Jun 16, 2016 at 09:12 PM

As expected, as soon as I post something I figure it out. Posting here in case anyone else is having the same issue.

Ray should have been calculated like so:

 Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);
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

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

Unity 2019 LTS can't find scripts in "Editor" Folder. 0 Answers

Objects in Custom Editor Resetting on Play 1 Answer

How to make dynamic list custom inspector 0 Answers

EditorGUILayout.ObjectField on a ScriptableObject's inspector doesn't update properly 0 Answers

How do I draw information, extending the "Project/Assets/" Window? 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