- Home /
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");
}
}
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"
I don't believe the Input() functions are called in the editor (without running the game) please correct me if I am wrong.
You may be right, never tried to use them inside the editor.
Okay so I thought about this a bit, check out my edited answer and I hope it helps :)
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.
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!
Is it required to get a new controlID in every call to OnSceneGUI()?
EDIT: Yes.
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...
ARGH! Everyone solving these problems links to that video and it's disappeared from YouTube, haha. Still, this was an extremely helpful answer. Thanks!
Thank you so much for this! Every second answer on Unity Editor is wrong in some way, but your answer is really helpful.
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;
}
}
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");
}
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");
}
}
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");
}
Your answer
Follow this Question
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