Search Unity

[Solved] GUI.Button...'which' mouse button clicked ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zug, Nov 25, 2009.

  1. Zug

    Zug

    Joined:
    Nov 25, 2009
    Posts:
    10
    if (GUI.Button (Rect (20,130,50,30), fortuneIcon)) {
    Application.LoadLevel (4);
    }


    Looking through the GUI api, I don't see a way to grab the exact cause of the above event. I tried using Input.Mouse functions within the above snippet, but apparently GUI.Button eats the event. I also assume that the Event.* functions eat the event?

    Anyone have any ideas on how to determine which mouse button caused the button click ? (without resorting to Rect checks + mouse events)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Maybe you could use Input.GetMouseButton. This doesn't consume an event, but returns true as long as the button is down.
     
  3. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    You could also do it manually, ie. check if mouse button has been clicked AND if the mouse is over the Rect used for displaying the button. If both conditions are true, that means your button has been clicked.
     
  4. Zug

    Zug

    Joined:
    Nov 25, 2009
    Posts:
    10
    Thanks for the tips! I found out:
    Code (csharp):
    1.  
    2. if  (GUI.Button (Rect (20,40,50,30), expertiseIcon)) {
    3.     if (Event.current.button==0) {
    4.         expertCount=expertCount+1;
    5.         Debug.Log(expertCount);
    6.     }
    7.     if (Event.current.button==1) {
    8.         expertCount=expertCount-1;
    9.         Debug.Log(expertCount);
    10.     }
    11. }
    The Event.current does what it sounds like ;). For some reason I was thinking it was looking at the next event in the queue.

    Thanks, guys![/code]