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
0
Question by Boolet · Sep 10, 2020 at 10:12 AM · uiraycasteditor-scriptingmouseover

Checking UI element beneath cursor in editor

I have been attempting to write a method that will return the object beneath the cursor in the Scene View of the editor. Using the following very neatly works for common game objects in the scene, such as models and primitives.

 Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
 object foundObject = HandleUtility.RaySnap(ray);

or

 GameObject foundObject = HandleUtility.PickGameObject(Event.current.mousePosition, false);


HOWEVER it does NOT work for finding canvases and UI elements in the scene. As far as I can tell, the Graphic Raycaster does not work for this purpose either.

When you use the scene view normally, you can click on elements of a canvas from the scene view camera and select them just fine. This implies that detecting them in an editor script is possible, but no search that I've done has come up with anything that suggests how to do it!

Here is my test code, as well as an image of the simple scene that it's supposed to work on.

 [InitializeOnLoad]
 public class RaycastTest{
     static Event e { get { return Event.current; } }
 
     static RaycastTest() {
         SceneView.duringSceneGui += GUIUpdate;
     }
 
     static void GUIUpdate(SceneView sceneview) {
         switch (e.type) {
             //case EventType.Repaint:
             //    break;
             case EventType.MouseDown:
                 TestCast();
                 break;
             //case EventType.MouseUp:
             //    break;
             //case EventType.MouseDrag:
             //    break;
             //case EventType.Layout:
             //    break;
         }
     }
 
     static void TestCast() {
         //Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);
         //HandleUtility.RaySnap(ray);
         //HandleUtility.PickGameObject(Event.current.mousePosition, false);
 
         RaycastHit2D hit = Physics2D.Raycast(Camera.current.ScreenToWorldPoint(e.mousePosition), Vector2.zero);
         Debug.Log(hit.collider);
     }
 }

alt text

Please help!

screen-shot-2020-09-10-at-103756-am.png (71.9 kB)
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 Gabe_TTT · Oct 05, 2021 at 05:52 AM 0
Share

Were you able to find a solution? I have the same issue

2 Replies

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

Answer by Gabriel_Ris · Oct 28, 2021 at 07:40 PM

I came up with a solution for this based on how the GraphicsRaycast works. You can use check which elements are underneath the mouse and print the one on the front. Cheers!

 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.UI;
 
 [InitializeOnLoad]
 public class RaycastTest
 {
     static Event e { get { return Event.current; } }
     private readonly static Canvas canvas;
 
     static RaycastTest()
     {
         canvas = Object.FindObjectOfType<Canvas>();
         SceneView.duringSceneGui += GUIUpdate;
     }
 
     static void GUIUpdate(SceneView sceneview)
     {
         switch (e.type)
         {
             //case EventType.Repaint:
             //    break;
             case EventType.MouseDown:
                 TestCast();
                 break;
                 //case EventType.MouseUp: 
                 //    break;
                 //case EventType.MouseDrag:
                 //    break;
                 //case EventType.Layout:
                 //    break;
         }
     }
 
     static void TestCast()
     {
         Camera sceneCamera = SceneView.lastActiveSceneView.camera;
 
         Graphic frontGraphic = null;
         // Get mouse screen position
         Vector3 mouseScreenPosition = HandleUtility.GUIPointToScreenPixelCoordinate(e.mousePosition);
         // Get the list of all graphics in the scene
         IList<Graphic> graphics = GraphicRegistry.GetRaycastableGraphicsForCanvas(canvas);
         // Get the front-most graphic on the position you clicked
         for (int i = 0; i < graphics.Count; i++)
         {
             Graphic graphic = graphics[i];
 
             if (!graphic.raycastTarget || graphic.canvasRenderer.cull || graphic.depth == -1)
                 continue;
 
             if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, mouseScreenPosition, sceneCamera, graphic.raycastPadding))
                 continue;
 
             if (sceneCamera != null && sceneCamera.WorldToScreenPoint(graphic.rectTransform.position).z > sceneCamera.farClipPlane)
                 continue;
 
             if (graphic.Raycast(mouseScreenPosition, sceneCamera))
             {
                 if (frontGraphic == null || frontGraphic.depth < graphic.depth)
                 {
                     frontGraphic = graphic;
                 }
             }
         }
         if (frontGraphic != null)
         {
             Debug.Log("You are clicking on " + frontGraphic.name);
         }
     }
 }
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 Boolet · Oct 28, 2021 at 08:26 PM 0
Share

This looks excellent! I will give it a shot later today and verify that it works.

avatar image
0

Answer by Steyo · Sep 10, 2020 at 12:55 PM

Try with a raycasthit2D :

  RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(eventData.position), Vector2.zero);

It worked fine for me.

EDIT : eventData being the event got from the OnDrag fonction in my case.

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 Boolet · Sep 10, 2020 at 05:40 PM 0
Share

This doesn't work for my purpose, since I'm trying to pick a UI element, rather than an object with a Collider2D component. Editing my question with some code too.

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

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

How to make a crosshair move with the player 1 Answer

physics.raycast troubles with onClick UI Events 2 Answers

UI Text showing weird string value? 1 Answer

Raycast from 2D canvas object to world space 1 Answer

IsPointOverGameObject() not working as intended!!! 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