- Home /
Getting Event.current in the editor all the time
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?
You can access the scene methods only inside an Editor
class. What kind of side effects are you talking about?
Answer by jonas-echterhoff · Oct 13, 2010 at 09:01 AM
I believe that currently the Editor class is the only way to hook into Scene events. What side effects are you seeing from make a CustomEditor for GameObjects? (Other then not being called when no object is selected, that is).
Some of Unity's editable flags for game objects (Tags, Static flags) don't appear if you create an Editor for GameObjects. This could be a bug.
Answer by Ashkan_gc · Oct 13, 2010 at 09:20 AM
use OnSceneGUI it solves the problem of being in gameview instead of sceneview but the other problem is not solvable till unity exposes more of it's editor functionality
OnSceneGUI does not get called on normal objects. The idea of using OnGUI was to use $$anonymous$$onoBehaviors ins$$anonymous$$d of Editors.