It is very common issue when you are creating some editor staffs.
So, you need to “add a point” or “paint on surface”, etc using LEFT click.
But when you click, Unity selects a game object underneath the cursor. How to avoid this?
For example, in Unity’s Terrain. When you edit heights clicking on a scene, no objects are selected. You just edit heights or paint using some splat texture.
How to implement this? How it is done in Terrain?
Not the best solution, but I found that manually setting the current selection to my target GameObject helped in my case, on every “OnSceneGUI” callback for my custom inspector.
Good news is you can use SceneView.onSceneGUIDelegate from any editor class including EditorWindows as well – and you can set the current selection to whatever the object is you’d like to constrain the selection to be.
public class KeepSelectionExampleInspector : Editor {
public void OnEnable() {
SceneView.onSceneGUIDelegate += CustomOnSceneGUI;
}
public void OnDisable() {
SceneView.onSceneGUIDelegate -= CustomOnSceneGUI;
}
private void CustomOnSceneGUI(SceneView s) {
Selection.activeGameObject = ((Component) target).gameObject; //Here! Manually assign the selection to be your object
}
}
Note that…
• I don’t use custom Inspector’s “OnSceneGUI” Unity message because it had some serialization bugs one time I tried to use it, so I use SceneView.onSceneGUIDelegate now haha – does the same thing basically, but it works!
• In the example, I used the “target” property from the Editor class. In custom Inspectors, you use the “target” property to get the thing you’re inspecting, and you can cast it to its actual (more specific) type.
• You can set Selection.activeGameObject to any object – whatever it is you want to keep the selection to! Thus, you don’t have to set the current selection to exactly ((Component) target).gameObject.