Right Clicking on a Button

Discussion in 'Extensions & OnGUI' started by Laurie Athey, Mar 27, 2013.

  1. Laurie Athey

    Laurie Athey

    New Member

    Joined:
    Jan 28, 2013
    Messages:
    14
    Hi all,

    Trying to plan out some courswork that's entirely made of UI, no 3D shizzle going on. Im using unity to try and learn its GUI functionality in preperation for a project in a few months. I do realise its not ideal for this.

    I need/want a button that i can left or right click on to increase or decrease a value and the issue is the right click since it only returns true/false.
    I have read http://answers.unity3d.com/questions/17477/right-click-button-using-gui-class.html
    but thats 3 years old and one of the things they do is save the button as a variable for later reference. That is now impossible because GUI.Button is a function not a type.

    The answer says to use a GUILayout but the overload they use on the .Button call don't exist anymore.
    I have tried searching the unityGUI forums but it crashes everytime i hit search no matter what I search for. I even tried boobies.

    So, im left with a question but no answer.
    How, with Unity 4's GUI do I get a button that I can say if they right click button x do this if they left click button x do that?

    Thank You All,
    Hope there is a solution cause I got 3 courseworks to do and all are using unity and need UI.
    P.S. Whats up with the forum search crashes?
     
  2. Avalion

    Avalion

    Member

    Joined:
    May 2, 2012
    Messages:
    24
    Using GUILayout.Button you can display the button

    You can use after this GUILayoutUtility.GetLastRect() to get the Rect of your Button.

    After this, you just have to check your input positions to check if this rect contains your mouse.

    Here is the code
    Code (csharp):
    1.  
    2. public void OnGUI() {
    3.    Rect myRect;
    4.    // If you use layout
    5.    GUILayout.Button("MyButton");
    6.    myRect = GUILayoutUtility.GetLastRect();
    7.    // If you use GUI
    8.    myRect = new Rect(x,y,width,height)
    9.    GUI.Button(myRect, "MyButton");
    10.  
    11.    // Check Left Click
    12.       //Actions on Left Click
    13.    }
    14.  
    15.    // Check Right Click
    16.       //Actions on Right Click
    17.    }
    18. }
    19.  
    Code not tested and for Unity 3. I don't know if it works on Unity 4.

    Good luck
     
  3. Laurie Athey

    Laurie Athey

    New Member

    Joined:
    Jan 28, 2013
    Messages:
    14
    Oooooh

    Took me a sec (okay, like 10 minutes) to understand what you were doing, but now I c it. Yeah that should do it. The overload you used in GUILayout.Button("MyButton"); does not exist anymore. they now want you to put butt loads of other variables in as well and i dont know what half of them do.
    However, using GUI works exactly the same way. So yes your code works in unity 4 but only the GUI bit, not he GUILayout part.

    Figured I should try it before posting. It works. Cheers, Made my day.
     
  4. Laurie Athey

    Laurie Athey

    New Member

    Joined:
    Jan 28, 2013
    Messages:
    14
    Just did some debugging cause of funky results.

    Evertime I increment a run of the mill int using Avalion's method, (on click) it gets called twice.
    I did some more checking and found out that this is being called once on the frame it gets clicked, and again the frame after that. I counted frames and outputted the number of the frame on each click, each click got two outputs instead of one.

    Ideas?

    Did some more testing. Turns out this does not happen if I run the int++ code in Update() rather than OnGUI()

    So, can someone tell me why I have to split my code up like that? I dont understand why

    if (Input.GetMouseButtonDown(1) myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {

    //Actions on Right Click

    }

    gets called twice in OnGUI() and once in Update()
     
    Last edited: Mar 27, 2013
  5. dkozar

    dkozar

    Member

    Joined:
    Nov 30, 2009
    Messages:
    1,398
    You might wanna try eDriven. It has a right-click, mousedown, mouseup, mouseover, mouseout and many more.

    Usage:

    Code (csharp):
    1. yourButton.RightClick += delegate (Event e) {
    2.     // do your stuff
    3. }
     
  6. Laurie Athey

    Laurie Athey

    New Member

    Joined:
    Jan 28, 2013
    Messages:
    14
    I pretty much solved all my issues, but give me a few days to play with eDriven and I might have to hug you.
    If this works in unity 4 I might just cry (watching tutorials as I type)
    Unity 4 is a must for me because the boss will want us to us its pathfinding and stuff to cut dev time.
    P.S. Are you the guy that actually made it? :O
     
    Last edited: Mar 28, 2013
  7. dkozar

    dkozar

    Member

    Joined:
    Nov 30, 2009
    Messages:
    1,398
    Yes, I made it. :)

    It works both with Unity 3.5.x and Unity 4.

    [​IMG]
     
  8. maasteriti

    maasteriti

    New Member

    Joined:
    Apr 3, 2013
    Messages:
    1
    OnGUI gets called every time it needs to handle an event, which can (and often does) happen more than once per frame. So if you need to have your code sync with the frame count, Update or LateUpdate is the place