Vladyslav Khromykh
Vladyslav Khromykh

Reputation: 787

Why Unity Event work in wrong way ? Or is it me?

I try to get an MouseUp event in OnGUI Function in my MonoBehabiour script.

But it always returns "Layout" and "Repaint".

    private void OnGUI() {
    Event e = Event.current;
    Debug.Log (e.type);
}

My debug like a: Layout Repaint Layout Repaint Layout Repaint and so on

Why is it so ? How can I know get event MouseUp ?

Edit1:

Event e = Event.current;
    int s = e.button;

I can know when I click on mouse button 0, but how can I Know when I get MouseUp Event ?

Edit2:

I want to work it in editor and not in play mode. Is it possible ?

Upvotes: 2

Views: 1221

Answers (1)

Programmer
Programmer

Reputation: 125245

You can get event MouseUp event with if (Event.current.type == EventType.MouseUp).

Here is a complete code sample:

 void OnGUI()
 {
     Event mEvent = Event.current;

     if (mEvent != null && mEvent.isMouse)
     {
         //Get Mouse Down
         if (mEvent.type == EventType.MouseDown)
         {

         }

         //Get Mouse Up
         if (mEvent.type == EventType.MouseUp)
         {

         }

         //Get Mouse Move
         if (mEvent.type == EventType.MouseMove)
         {

         }
     }
 }

Now, to detect which mouse button:

 //Left Mouse button
 if (mEvent.button == 0)
 {

 }

 //Right  Mouse button
 if (mEvent.button == 1)
 {

 }

 //Middle  Mouse button
 if (mEvent.button == 2)
 {

 }

You can put those inside your if (mEvent.type == EventType.MouseDown) and if (mEvent.type == EventType.MouseUp) code.

Upvotes: 1

Related Questions