Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by lugduweb · Oct 18, 2020 at 09:52 AM · mouseclickkeypresseditor scripting

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

OnMouseDown function not working on a gameObject 1 Answer

How do I make a rigid body move towards position of touch 0 Answers

How to make spawned in game objects pause with mouse click. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges