Unable to get mouse or key events in ExecuteInEditMode
Hello, I would like to get mouse (and keys) event in Editor mode in real time. I've tried three different ways and none of them works. I must be missing something obvious. Can someone help ? In the following code, the logs are never displayed (only the Update - OnGUI when I leave then go back to Unity editor window.
Thanks by advance for any help (the following script is attached to a game object in an empty scene with only camera and light).
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class EventTest : MonoBehaviour
{
[HideInInspector] public Vector3 mousePosition = new Vector3();
[HideInInspector] public bool wasClickedOnGUI = false;
[HideInInspector] public bool wasClickedCustom = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Update - Input - click bt0" + Input.mousePosition.x + ", " + Input.mousePosition.y);
}
if(wasClickedOnGUI)
{
Debug.Log("Update - OnGUI - click bt0 " + mousePosition.x + "," + mousePosition.y);
wasClickedOnGUI = false;
}
if (wasClickedCustom)
{
Debug.Log("Update - Custom - click bt0 " + mousePosition.x + "," + mousePosition.y);
wasClickedCustom = false;
}
}
void OnGUI()
{
Event e = Event.current;
if (e.isMouse)
{
if (e.type == EventType.MouseDown && e.button == 0)
{
wasClickedOnGUI = true;
mousePosition.x = e.mousePosition.x;
mousePosition.y = e.mousePosition.y;
e.Use();
Debug.Log("OnGUI - click bt0 " + mousePosition.x + "," + mousePosition.y);
}
}
}
}
[CustomEditor(typeof(EventTest))]
public class EventTestEditor : Editor
{
SerializedProperty lookAtPoint;
public void OnSceneGUI()
{
Event e = Event.current;
if (e.isMouse)
{
if (e.type == EventType.MouseDown && e.button == 0)
{
EventTest evt = (EventTest)target;
evt.wasClickedCustom = true;
evt.mousePosition.x = e.mousePosition.x;
evt.mousePosition.y = e.mousePosition.y;
e.Use();
Debug.Log("CustomEditor - click bt0 " + evt.mousePosition.x + "," + evt.mousePosition.y);
}
}
}
}
Note: also posted the question here but did not have any answer: Unity Forum : Unable to get mouse or key events in ExecuteInEditMode
Your answer

Follow this Question
Related Questions
Elegant Solution to the Problem of Dealing With Multiple Key/Mouse Presses at the Same Time 0 Answers
Match 3 Movement Using Keyboard Controls,Match 3 movement using keyboard controls 1 Answer
Camera Script remove a part. 0 Answers
How do I use an event trigger on an instantiated prefab? 1 Answer
How to change the color of the each phase of the cube separately ? 0 Answers