Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Right Clicking on a Button

Discussion in 'Immediate Mode GUI (IMGUI)' started by Laurie-Athey349, Mar 27, 2013.

  1. Laurie-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    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

    Joined:
    May 2, 2012
    Posts:
    34
    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.    if (Input.GetMouseButtonDown(0)  myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
    13.       //Actions on Left Click
    14.    }
    15.  
    16.    // Check Right Click
    17.    if (Input.GetMouseButtonDown(1)  myRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
    18.       //Actions on Right Click
    19.    }
    20. }
    21.  
    Code not tested and for Unity 3. I don't know if it works on Unity 4.

    Good luck
     
  3. Laurie-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    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-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    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

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    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

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Yes, I made it. :)

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

     
  8. maasteriti

    maasteriti

    Joined:
    Apr 3, 2013
    Posts:
    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