Skip to content

Instantly share code, notes, and snippets.

@Arakade
Created March 9, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arakade/cb538b80ee7e712c20051951d68426e1 to your computer and use it in GitHub Desktop.
Save Arakade/cb538b80ee7e712c20051951d68426e1 to your computer and use it in GitHub Desktop.
Unity3D Editor OnSceneGUI() code for detecting mouse-press while key held changed in 2020.2.7f1 (at least)
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
// pre C#8
//#nullable enable
public sealed class ButtonAndMouseInEditor : MonoBehaviour {
[SerializeField]
internal KeyCode key = KeyCode.A;
}
#if UNITY_EDITOR
[CustomEditor(typeof(ButtonAndMouseInEditor))]
public sealed class ButtonAndMouseInEditorEditor : Editor {
public void OnSceneGUI() {
var actual = (ButtonAndMouseInEditor) target;
// focus on SceneView. Assumes only 1 open
if (EditorWindow.mouseOverWindow is SceneView && !(EditorWindow.focusedWindow is SceneView)) {
EditorWindow.FocusWindowIfItsOpen<SceneView>();
}
switch (Event.current.type) {
case EventType.MouseDown:
if (keyDown) {
Debug.Log("MouseDown WHILE key is held = success (remember to click back on object with this class)");
} else {
Debug.Log("MouseDown while key is NOT held (remember to click back on object with this class)");
}
break;
case EventType.KeyDown:
if (!keyDown && actual.key == Event.current.keyCode) {
Debug.Log("KeyDown");
keyDown = true;
}
break;
case EventType.KeyUp:
if (keyDown && actual.key == Event.current.keyCode) {
Debug.Log("KeyUp");
keyDown = false;
}
break;
}
var overWindow = EditorWindow.mouseOverWindow;
var focusedWindow = EditorWindow.focusedWindow;
Handles.BeginGUI();
const int NumLines = 4;
GUI.Box(new Rect(0, 0, 400, 20 * NumLines), string.Format("ACTIVE!\nkeyDown:{0}\nover:{1} (SceneView:{2})\nfocus:{3} (SceneView:{4})", keyDown, overWindow, overWindow is SceneView, focusedWindow, focusedWindow is SceneView));
Handles.EndGUI();
}
private bool keyDown;
}
#endif // UNITY_EDITOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment