- Home /
How do you detect clicks in scene view without losing object focus?
I'm working on an inspector/editor tool in Unity but I'm having difficulty detecting left mouse clicks, while simultaneously not allowing that left-clicking to take focus away from the object that the script is attached to...while simultaneously updating what is happening in OnSceneGUI continuously. I don't fully understand all of this editor code, it's mostly stuff that I've found through online examples, so I'm sure there's something I'm doing wrong.
My main script is where I'm trying to detect the clicks inside of OnDrawGizmos. (I'm also drawing some gizmo stuff in this block that needs to be updated constantly, but I've left it out as it's not relevant...)
public class myScript
{
private void OnDrawGizmos()
{
if (Application.isPlaying)
return;
Event e = Event.current;
if (e.button == 0 && e.isMouse)
{
Debug.Log("Left Click");
}
}
}
...this works fine by itself, but it removes focus from the script within a few clicks, which makes its OnDrawGizmos stop updating until I click back onto the object. I've made a custom inspector script for that first script where I'm repainting the scene view so that the gizmo stuff inside OnDrawGizmos updates quickly...
[CustomEditor(typeof(myScript))]
public class myScript_editor : Editor
{
myScript _myScript;
public void OnSceneGUI()
{
_myScript = (myScript)target;
Event e = Event.current;
if (!Application.isPlaying && e.type == EventType.MouseMove)
{
SceneView.RepaintAll(); //This is so that the refreshing is quick for OnDrawGizmos
}
_myScript.v_mousePos_projected = Event.current.mousePosition;
if (e.button == 0 && e.isMouse)
{
//Debug.Log("Left Click"); //For some reason, this always fires when my mouse moves even when
//I'm not clicking....
}
if ( _elm.amDrawing )
{
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); //This is necessary
//to put in onscenegui in order to prevent focus from being taken away when clicking in the
//scene during drawing...
}
}
}
...but implementing this makes it so that I can't detect mouse clicks from within my script's OnDrawGizmos... I think this probably has something to do with the line "HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); " because when I take this away, my script can detect left clicks in OnDrawGizmos. As you can see, I've therefor tried to detect clicks in my editorscript's OnSceneGUI, but that just always fires when my mouse moves. Is there some way to only detect clicks and not movement from within OnSceneGUI? is there an alternative like detecting mouse input from inside OnInspectorGUI()? Any help would be appreciated.