How to stop inspector losing focus when clicking in scene view?

I have a custom editor script where i am clicking in the scene view to select objects. But when i click I lose focus of the editor from inspector so i no longer have scene interactions, how do i stop this from happening? I tried the following:

if (Event.current.type == EventType.MouseUp)
{
    if (Event.current.button == 0)
        Event.current.Use();
}

This of course did absolutely nothing so does any one know how to stop the inspector focus being losed when clicking in the scene window?

There is a lock on the top right of the inspector next to the three dots, click that to prevent the inspector from losing focus.

Shouldn’t need to do that. There has been tools out there that lets you click in scene and doesn’t lose focus on the inspector without locking it. Plus that would be tedious to keep doing.

If, in some way, you prevent selecting other objects in the scene, how would you ever deselect your initially selected object? I assume you should be looking into the OnSceneGUI of a custom editor (or custom window).

It would be even better if you could describe what you are specifically doing. That might help provide a more concrete answer.

In order for your snippet to keep your inspector in focus, you’d need to set the Selection.activeGameObject back to your initial object. But that doesn’t sound like what you want to do?

If you need to select other objects in the scene view, you might be better off creating an editor window.

Another option is to open a separate inspector tab and lock it as per the suggestion above.

But its not a gameobject its a scriptable object. I don’t need to select anything in scene view, just click in it to get world point on the XZ plane from scene camera via raycast.

I’ve attempted this before for a ‘Scene-picker’ type tool but couldn’t figure it out myself. I know I’ve seen tools out there that do it, so honestly why not take a look at our source code and see what they do?

Well, we need to see a bit more code to be sure of the issue. The right lines in the wrong place won’t do anything.

From memory, the steps are:

  • Implement an OnSceneGUI method in your custom Inspector. I believe that this is how things such as the Terrain component’s custom editor works, and that definitely overrides the default Scene view input behaviours.
  • Within that, intercept the event for MouseDown.
  • Do whatever you need to do, then call Use() on the UI event so that other parts of the UI don’t also respond to it.

Yes. And then follow my suggestion above to force Selection.activeObject back to your scriptable object.

I’ve done this in OnSceneGUI before, and then i just listen for a modifier key (eg ALT) to allow deselection of the object.

Or, only capture input that would prevent deselection when the user prompts it, e.g. on pressing a button, or with a mode toggle.

Going to bring this thread back as I found some stuff out that I think would be helpful, that I learnt while working on this: Editor scene objects pipette - #19 by spiney199

One, you can prevent objects being selected in the scene if you have this running in any OnSceneGUI code:

private void OnSceneGUI()
{
    int id = GUIUtility.GetControlID(FocusType.Passive);
    HandleUtility.AddDefaultControl(id);
}

And two, you can still ‘pick’ game objects using a number of methods available in the HandleUtility class: Unity - Scripting API: HandleUtility

In my linked example, I registered a callback to the SceneView.rootVisualElement’s and used that to determine where the click happened.