Unity Community


Results 1 to 8 of 8

  1. Location
    Dundee, Scotland
    Posts
    14

    Right Clicking on a Button

    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...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. Posts
    19
    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:  
    1. public void OnGUI() {
    2.    Rect myRect;
    3.    // If you use layout
    4.    GUILayout.Button("MyButton");
    5.    myRect = GUILayoutUtility.GetLastRect();
    6.    // If you use GUI
    7.    myRect = new Rect(x,y,width,height)
    8.    GUI.Button(myRect, "MyButton");
    9.  
    10.    // Check Left Click
    11.       //Actions on Left Click
    12.    }
    13.  
    14.    // Check Right Click
    15.       //Actions on Right Click
    16.    }
    17. }

    Code not tested and for Unity 3. I don't know if it works on Unity 4.

    Good luck


  3. Location
    Dundee, Scotland
    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. Location
    Dundee, Scotland
    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 by Laurie Athey; 03-27-2013 at 06:43 AM.


  5. Location
    Zagreb, Croatia
    Posts
    1,377
    You might wanna try eDriven. It has a right-click, mousedown, mouseup, mouseover, mouseout and many more.

    Usage:

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


  6. Location
    Dundee, Scotland
    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 by Laurie Athey; 03-28-2013 at 12:58 AM.


  7. Location
    Zagreb, Croatia
    Posts
    1,377
    Quote Originally Posted by Laurie Athey View Post
    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
    Yes, I made it.

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



  8. 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •