How to make a Raycast in the editor window?

Hello dear experts.
I do Pribluda in your custom inspector which would display the data(information) on the object which is currently the mouse pointer in the editor window of the scene. The objects are all 2D sprites.

I scraped together the following code but it doesn't work for me:

Ray ray = Camera.main.ScreenPointToRay (Event.current.mousePosition );
Debug.Log(ray);

RaycastHit hit;
// Shoot this ray. check in a distance of 10000.
if (Physics.Raycast(ray, out hit, 10000))
{ 
 Debug.Log ("Raycast hit name:" + hit.collider.gameObject.name);
}


Tell me where I have mistake?
or I started to dig in the wrong direction?
July 2nd 19 at 17:19
1 answer
July 2nd 19 at 17:21
Read the documentation about 2D raycast . then will work)
https://docs.unity3d.com/ScriptReference/Physics2D...
https://docs.unity3d.com/ScriptReference/UI.Graphi...
https://docs.unity3d.com/ScriptReference/UI.Graphi...

to heap more to this.) - earline.Fad commented on July 2nd 19 at 17:24
Not working raycast, anyway, tried everything. Saved thing .GetComponent().bounds.Contains(mouseWorldPos)), through it all is. - alison.Hahn67 commented on July 2nd 19 at 17:27
Oh) then the most obvious forgotten. you have a Collider on the object that is? layer?

and just to check object frame) Yes you can, but all the objects to iterate over to find out "not did I you" instead of "to whom it may I now touch the beam")) - earline.Fad commented on July 2nd 19 at 17:30
Oh and you can screen window inspector and the scene hierarchy) to see what is there in the scene that his rakasta humanly do not get it. - earline.Fad commented on July 2nd 19 at 17:33
: where does the Collider? I just need information about the object and the object is 2D and tile. And colliders, it is not mandatory, although I have in the scene they hung on the tiles. And I note that in my written question, a situation occurs in the editor mode! And not when you press play. - alison.Hahn67 commented on July 2nd 19 at 17:36
: I have tried a mountain of ways to sarinastiti objects at the mouse cursor, but to no avail, why, and climbed a toaster. Join the adventure for some reason recast in editor mode, or it is by definition not the right approach, in General, I don't know why and asked the question. - alison.Hahn67 commented on July 2nd 19 at 17:39
heh, missed the point about the fact that the editor not launching.
you then perhaps this too may need https://docs.unity3d.com/ScriptReference/ExecuteIn...

And Collider though - not raycast Collider and so to speak in a void - nothing to return)) - earline.Fad commented on July 2nd 19 at 17:42
: okay, colliders forget. But your link also it will not work because I once again clarify!!! Not monobike situation occurs.....and :Editor - alison.Hahn67 commented on July 2nd 19 at 17:45
now clearly "is editor" is the editor mode (and not care if monobed it or whatever , what you forgot to mention the example script then the whole would be laid out, who knows you where you are trying to call) or occurs in the script inherits from EditorWindow or something? - earline.Fad commented on July 2nd 19 at 17:48
It's fine to do Raycasting in the editor; remember if you do screen point to ray to use Camera.current instead of Camera.main and it will use the editor camera as long as the editor screen has focus. - earline.Fad commented on July 2nd 19 at 17:51
if suddenly bad with English, of problema is that the main camera in the editor as such does not exist. need CURRENT camera. - earline.Fad commented on July 2nd 19 at 17:54
if you have unity 5 there is HandleUtility.GUIPointToWorldRay - earline.Fad commented on July 2nd 19 at 17:57
: Yes You are right, in detail I apparently screwed up....I'm new so not obessudte. A little later I'll try to go into details, but the whole script I have ran in 64K, I try to collect brief. - alison.Hahn67 commented on July 2nd 19 at 18:00
Ray ray = HandleUtility.GUIPointToWorldRay( Event.current.mousePosition );

RaycastHit hit;
if( Physics.Raycast( ray, out hit ) )
{
// do stuff
}

something like this) say works fine)) - earline.Fad commented on July 2nd 19 at 18:03
: no , I do not Robit. And where to stick the code should be: OnSceneGUI, OnGUI, Update, etc.? - alison.Hahn67 commented on July 2nd 19 at 18:06
before or after the SceneView.RepaintAll(); - alison.Hahn67 commented on July 2nd 19 at 18:09
the example that I threw it for 3D objects used in OnSceneGUI. You there is more just for future reference just need to remember that the beam for editor scripts, it is necessary to send through such a utility method)

and remember that determination to hit 3D and 2D objects raycast is called different) and that there are in fact three of their leaves Graphic.Raycast, Physics.Raycast, Physics2D.Raycast - earline.Fad commented on July 2nd 19 at 18:12
by : spaibo. - alison.Hahn67 commented on July 2nd 19 at 18:15

Find more questions by tags Unity Game Engine