I’ve been wrestling with trying to get Raycasting to work in Edit mode for a while now. I finally managed to access the camera through which the editor “Scene” view is rendered (using Camera.current and making sure it’s not null before using it, it seems to give me what I want only when my mouse is within the bounds of the Scene view.)
The problem I’m having currently is that I can’t find a way to get the position of the mouse/cursor in Edit mode. Input.mousePosition doesn’t work in Edit mode. I tried grabbing Event.mousePosition during OnGUI() of my class that extends from EditorWindow but it gives me negative values ?!? (relative to my editorwindow maybe?) I just want to use ScreenPointToRay to create a Ray that I can cast through my scene in Edit mode.
Also, I believe Raycasting only works when I have Colliders on my objects correct? How does the Unity Editor itself handle picking objects with the mouse, even when they don’t have colliders attached to them? There is this HandleUtility.PickGameObject function but it has no documentation.
I did find a solution. You can use the Editor class to create a custom Editor for a specific type of Object (eg. one of your own scripts) and the custom Editor will show up in the Inspector view when you click on a Game Object that has your script attached to it. (It’s a little complicated, I know )
The Editor class has access to the OnSceneGUI() function which allows you to grab Mouse events from the Scene view. Then, you can obtain the Scene camera by using Camera.current within OnSceneGUI() and you can cast rays from that camera just like you would with an in-game/main camera.
The EditorWindow class has access to the OnSelectionChange() function which will get called whenever the selection within the Editor changes. So in your class that derives from EditorWindow, add the OnSelectionChange() function and respond to the change there. You can find some documentation on these in the Unity Script Reference.
Let me know if you have more questions regarding this. I’ve been playing around with this stuff for the last month and I’ve got a good measure of success with it 8).
Exactly! The Event object also gives you access to things such as mousePosition and flags for whether Shift, Ctrl, or Alt keys have been pressed. What I usually do is grab a reference to the Event early in the OnSceneGUI call like this:
Event currentEvent = Event.current;
And then I get all the information I need from it later on.
I’ve stumbled upon an undocumented feature relating to ray casting in the editor view. (I’m using it in Unity 2.6.1)
Here’s a pointless C# example showing the minimum code required to use the feature.
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(MyBehaviour))]
public class MyBehaviourEditor : Editor
{
void OnSceneGUI()
{
if (Event.current.type == EventType.mouseMove)
Physics.Raycast(Event.current.mouseRay);
}
}
Please note that although this feature is currently available it is undocumented and may change without notice.
I found it useful so I’ve posted it here in the hope that it may prove useful to others.