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 /
avatar image
2
Question by logan4179 · Feb 26, 2021 at 06:14 PM · editor-scriptingclickonsceneguiondrawgizmos

How do you detect clicks in scene view without losing object focus?

I'm working on an inspector/editor tool in Unity but I'm having difficulty detecting left mouse clicks, while simultaneously not allowing that left-clicking to take focus away from the object that the script is attached to...while simultaneously updating what is happening in OnSceneGUI continuously. I don't fully understand all of this editor code, it's mostly stuff that I've found through online examples, so I'm sure there's something I'm doing wrong.

My main script is where I'm trying to detect the clicks inside of OnDrawGizmos. (I'm also drawing some gizmo stuff in this block that needs to be updated constantly, but I've left it out as it's not relevant...)

 public class myScript
 {
     private void OnDrawGizmos()
     {
         if (Application.isPlaying)
             return;
     
         Event e = Event.current;
         if (e.button == 0 && e.isMouse)
         {
             Debug.Log("Left Click");
         }
     }
 }

...this works fine by itself, but it removes focus from the script within a few clicks, which makes its OnDrawGizmos stop updating until I click back onto the object. I've made a custom inspector script for that first script where I'm repainting the scene view so that the gizmo stuff inside OnDrawGizmos updates quickly...

 [CustomEditor(typeof(myScript))] 
 public class myScript_editor : Editor
 {
     myScript _myScript;

     public void OnSceneGUI()
     {
        _myScript = (myScript)target;

         Event e = Event.current;
     
         if (!Application.isPlaying && e.type == EventType.MouseMove)
         {
             SceneView.RepaintAll(); //This is so that the refreshing is quick for OnDrawGizmos
         }
          _myScript.v_mousePos_projected = Event.current.mousePosition;
 
         if (e.button == 0 && e.isMouse)
         {
             //Debug.Log("Left Click"); //For some reason, this always fires when my mouse moves even when 
             //I'm not clicking....
         }
     
         if ( _elm.amDrawing )
         {
             HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); //This is necessary 
             //to put in onscenegui in order to prevent focus from being taken away when clicking in the 
             //scene during drawing...
         }
     }
 }

...but implementing this makes it so that I can't detect mouse clicks from within my script's OnDrawGizmos... I think this probably has something to do with the line "HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); " because when I take this away, my script can detect left clicks in OnDrawGizmos. As you can see, I've therefor tried to detect clicks in my editorscript's OnSceneGUI, but that just always fires when my mouse moves. Is there some way to only detect clicks and not movement from within OnSceneGUI? is there an alternative like detecting mouse input from inside OnInspectorGUI()? Any help would be appreciated.

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

124 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

Related Questions

Accessing OnSceneGUI with EditorWindow in JS 1 Answer

Is there a way to draw on the scene view from an asset being inspected by the Inspector? 0 Answers

Why does the SceneView camera return jittery values? 0 Answers

There is a way to check if a handle is highlighted? 1 Answer

Support multiple selection with OnSceneGUI handles (The targets array should not be used inside OnSceneGUI) 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