Hi,
I'm trying to create a script that places objects in the scene based on my mouse position. The main function is this :
[MenuItem("GameObject/Place object via raycast _&r")]
public static void PlaceObjectViaRaycast()
{
if (Selection.activeGameObject && Camera.current)
{
Ray camRay = lastRay;
RaycastHit hitInfo;
if (Physics.Raycast(camRay, out hitInfo))
{
Selection.activeGameObject.transform.position = hitInfo.point;
}
}
}
The question is how to I populate lastRay? I started of by deriving from Editor and using this :
public void OnSceneGUI()
{
lastRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
}
However, this forces me to have the editor edit all gameobjects ([CustomEditor(typeof(GameObject)] etc), which is a bad thing IMO (has some side effects). If I do the same call in OnGUI, even if I add the ExecuteInEditMode attribute, I only get the event when I'm in game view, while I want to be in scene view.
Is it possible to write code that will get the Event.current in its context that will be active regardless of the object that is currently selected in the scene, without writing a custom editor for all gameobjects?