Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by DoubleDouble · May 06, 2011 at 01:48 AM · editormouseeventhandlesguiutility

Left Click Up Event in Editor

The EventType.MouseUp type of event only seems to call if I click on a handle. This only seems to happen for the left mouse button. (like after clicking an object and I also happen to have the mouse over the Movement x-axis handle, the event is called)

This is different from if I click the right mouse button, which seems to call the MouseUp event every time the right mouse is clicked.

I'm wondering how to fix this so the MouseUp event is called every time I click the left mouse; like the right mouse button.

http://answers.unity3d.com/questions/9898/making-a-tilemap-editor-within-the-unity-editor

The answer to that question seems to be an answer for me but I'm having problems understanding it if anybody can explain it to me, it being in boo makes it harder for me to understand.

This is what I played with to know when the events are called:

void OnSceneGUI()
    {
        if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
        {
          Debug.Log("Left-Mouse Down");
        }
        if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
        {
          Debug.Log("Left-Mouse Up");
        }
        if (Event.current.type == EventType.MouseUp && Event.current.button == 1)
        {
          Debug.Log("Right-Click Up");
        }
    }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Joshua · May 06, 2011 at 02:15 AM

No the answer to that question won't help you, that's concerning how to stop the editor screen window from doing things when you click inside it ( for instance select the object you clicked on ), but that's irrelevant here.

Your problem here seems strange though, have you tried using Input.GetMouseButtonUp(0) and Input.GetMouseButtonDown(0) instead? Because those have always worked to me like you described, get called if your button goes up regardless of mouse position.


Okay so if you cannot get the mouseUp event to be registered, fake it:

 var wasMouseDown : boolean = false;

 if(Event.current.type == EventType.MouseDown && Event.current.button == 0) {

     Debug.Log("Left-Mouse Down");

     wasMouseDown = true;
 }

 else if (wasMouseDown) {

     Debug.Log("Left-Mouse Up");

     wasMouseDown = false;
 }

This may also be of use:

"If call Event.current.Use(), the event will be "eaten" by the editor and not be used by the scene view its using"

Comment
Add comment · Show 4 · Share
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
avatar image DoubleDouble · May 06, 2011 at 02:26 AM 0
Share

I don't believe the Input() functions are called in the editor (without running the game) please correct me if I am wrong.

avatar image Joshua · May 06, 2011 at 02:32 AM 0
Share

You may be right, never tried to use them inside the editor.

avatar image Joshua · May 06, 2011 at 02:15 PM 0
Share

Okay so I thought about this a bit, check out my edited answer and I hope it helps :)

avatar image DoubleDouble · May 11, 2011 at 10:16 PM 0
Share

I actually did use the answer from the other question. After converting it to C#, as long as my object is selected in the scene-view it doesn't take any additional input to the sceneview but the mouseup was fixed. Your modified answer seems like a good idea too, but I think it would call mouseup right after the mousedown event - which wouldnt be bad for a single click but I'm trying to do a drag-click.

avatar image
9

Answer by ocimum · Oct 21, 2015 at 09:37 AM

Try it this way and you should be able to access all the clicks you want:

 void OnSceneGUI (){

     Event e = Event.current;
     int controlID = GUIUtility.GetControlID (FocusType.Passive);

     switch (e.GetTypeForControl (controlID)) {

     case EventType.MouseDown:
         GUIUtility.hotControl = controlID;
     
         CheckForPositions(e.mousePosition);
         e.Use ();

         break;

     case EventType.MouseUp:
         GUIUtility.hotControl = 0;
         e.Use();
         break;

     case EventType.MouseDrag:
         GUIUtility.hotControl = controlID;
         CheckForPositions(e.mousePosition);
 
         e.Use ();
         break;

     case EventType.KeyDown:
         if( e.keyCode == KeyCode.Escape ){
             // Do something on pressing Escape
         }

         if( e.keyCode == KeyCode.Space ){
             // Do something on pressing Spcae
         }

         if( e.keyCode == KeyCode.S ){
             // Do something on pressing S
         }

         break;
     }

     AllDrawingStuff();
 }

And here the drawing if you want

 void AllDrawingStuff(){
     Handles.BeginGUI();

     DrawCircleOnMousePositions();
     DrawLine();
 
     Handles.EndGUI();
 }

Here is a small tutorial which helped me to understand all the stuff!

Comment
Add comment · Show 5 · Share
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
avatar image Zyl · Feb 06, 2016 at 10:06 AM 1
Share

Is it required to get a new controlID in every call to OnSceneGUI()?

EDIT: Yes.

avatar image ocimum Zyl · Apr 13, 2016 at 02:27 PM 0
Share

It is all quite confusing, also took me some time to understand the .use(). But everything connected to the UI is a little bit complicated in the unity editor...

avatar image Artifact-Jesse · Dec 08, 2016 at 08:44 PM 0
Share

ARGH! Everyone solving these problems links to that video and it's disappeared from YouTube, haha. Still, this was an extremely helpful answer. Thanks!

avatar image Nemo · Jul 12, 2017 at 10:19 PM 0
Share

Thank you so much for this! Every second answer on Unity Editor is wrong in some way, but your answer is really helpful.

avatar image ABerlemont · Feb 07, 2020 at 09:09 PM 1
Share

Stumble upon a ton of deprecated answers for that release stuff. This is the right answer for 2019.3f1. Thanks !

  void checkForRelease()
   {
     Event e = Event.current;
     int controlID = GUIUtility.GetControlID(FocusType.Passive);
     switch (e.GetTypeForControl(controlID))
     {
       case EventType.$$anonymous$$ouseDown:
         GUIUtility.hotControl = controlID;
         e.Use();
         break;
       case EventType.$$anonymous$$ouseUp:
         GUIUtility.hotControl = 0;
         e.Use();
         Debug.Log("up !");
         break;
       case EventType.$$anonymous$$ouseDrag:
         GUIUtility.hotControl = controlID;
         e.Use();
         break;
     }
   }
avatar image
0

Answer by dalakgames · Feb 20, 2021 at 11:14 AM

after reading this, this is the solution I've came up with( works on 2019.4.18f1)

 if(Event.current.type == EventType.Layout) return; 

 // Without this you cant get mouse up events dont know why, it also prevents unity to handle some inputs so dont handle layout events otherwise you can move the selected object but cant select a different one
 HandleUtility.AddDefaultControl(-1);
 
 var e = Event.current;
 
 // Prevents duplicate key and mouse events
 if (e.isMouse || e.isKey)
 {
     if (e.commandName == "Used")
     {
         return;
     }
     e.commandName = "Used";
 }
 
 
 if (e.type == EventType.MouseDown)
 {
     Debug.Log("Down");
 }
 
 if (e.type == EventType.MouseUp)
 {
     Debug.Log("Mouse Up");
 }

 


Comment
Add comment · Show 1 · Share
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
avatar image chillaxpure · May 20, 2021 at 10:46 AM 0
Share

Running your code made my CPU run from 8% to 40%. I opened task manager when I heard my cooling fans kick on. However, it does exactly what I need. Not sure if it is because I'm on 2020.3.8f1 but I ended up fixing the problem, here it is. NOTE: "Mouse Up" will only appear in console after dragging an object if you leave GUIUtility.hotControl = 0; commented out. When using GUIUtility.hotControl = 0; you will get "Mouse Up" from any click in Scene, but will not be able to select objects.

 void OnSceneGUI(SceneView sceneView)
 {
     Event e = Event.current;
     if (e.type == EventType.Layout) return;
     if (e.type == EventType.MouseDown)
     {
             Debug.Log("Down");
         //GUIUtility.hotControl = 0; add this if you want to prevent any action in the scene
     }
 
     // Prevents duplicate key and mouse events
     if (e.isMouse || e.isKey)
     {
         if (e.commandName == "Used")
         {
             return;
         }
         e.commandName = "Used";
     }
 
     if (e.type == EventType.MouseUp)
     {
         Debug.Log("Mouse Up");
     }
 }
avatar image
0

Answer by Mariusz-Born7 · Dec 01, 2021 at 06:29 PM

If you don't want to interfere with editor events and be able to select objects and just be notified about MouseUp event, you can check it this way:

 Event current = Event.current;
 if (current.type == EventType.MouseUp && current.button == 0)
 {
 Debug.Log("LMB up");
 }
 else if (current.type == EventType.Used && current.button == 0 && current.pointerType == PointerType.Mouse)
 {
 Debug.Log("LMB up used");
 }
Comment
Add comment · Share
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

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

8 People are following this question.

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

Related Questions

Draw a handle in editor when key is pressed 2 Answers

How to "paint" objects in the editor? 3 Answers

OnGUI Event 0 Answers

Detecting moment when GameObiect is created in hierarchy. 1 Answer

How do I check clicking and dragging in the Editor? 0 Answers


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