- Home /
Disable Selection on Left button Mouse Click
Hi all,
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?
Thanks in advance
Answer by Deni35 · Mar 22, 2012 at 05:03 PM
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
Answer by ModLunar · Jul 22, 2018 at 04:50 PM
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
.
Your answer
Follow this Question
Related Questions
Clickable GUI.Window 1 Answer
detecting if the mouse is inside any gui element 1 Answer
Detect a click outside a GUI/object 4 Answers
Detecting a GUI element under the mouse? 1 Answer
Clicks from a track pad works, but not from a mouse 0 Answers