x


Right-click button using GUI class?

Hey, I am trying to use the standard GUI class to implement buttons... As far as just clicking them, everything was pretty easy:

function OnGUI {
   var myButton = GUI.Button(Rect(), myString)

   if(myButton) {
      //Do something crazy
   }
}

However, if I want something different to happen when the user right-clicks the button, how could that be done? I'm thinking something like:

function OnGUI {
   var myButton = GUI.Button(Rect(), myString)

   if(myButton) {
      if(mouseButton1) {
         //Action 1
      } else {
         //Action 2
      }
   }
}

Does that make sense? If it does, how do I implement it "correctly"?

more ▼

asked May 17 '10 at 10:36 AM

TheMorten gravatar image

TheMorten
128 8 8 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You'll want to look at the current GUI event to see which button was pressed. Try this code:


    if (GUILayout.Button("MyButton")
    {
        if (Event.current.button == 0)
            Debug.Log ("MyButton was clicked with left mouse button.");
        else if (Event.current.button == 1)
            Debug.Log ("MyButton was clicked with right mouse button.");
    }
more ▼

answered May 18 '10 at 01:27 AM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

Can I also add keyboard modifiers to that? (Like holding ctrl)? Well, I guess I can, but how? :P

May 18 '10 at 06:46 AM TheMorten

if (Event.current.control) Debug.Log ("Ctrl was held");

May 18 '10 at 05:01 PM jonas echterhoff ♦♦

Awesome, thanks!

May 19 '10 at 09:37 AM TheMorten
(comments are locked)
10|3000 characters needed characters left

Maybe sth like that?

private var actives = false;
private var actives2= false;

function OnMouseDown  () {
     if(Input.GetMouseButton(1)){
     actives = actives ? false : true;
     }
     if(Input.GetMouseButton(0)){
     actives2= actives2 ? false : true;
}
}

function OnGUI () {
     if (actives) {
          GUI.Label (Rect (50,50,100,50), "test");
      }
     if (actives2) {
          GUI.Label (Rect (50,50,100,50), "test");
      }
} 
more ▼

answered May 17 '10 at 11:48 AM

Tobias gravatar image

Tobias
378 50 54 66

That seems to require that I create every button as a separate object in the hierarchy, and attach this script to them individually? I'm going to have 20+ buttons in the final application, so that seems a bit excessive... :/

Or am I misunderstanding this completely?

May 17 '10 at 03:18 PM TheMorten

Why you have buttons in your hierarchy? This script creates the buttons. Just attach this to your main camera and add enough buttons into the javascript code. Or do I misunderstand sth?

May 17 '10 at 04:35 PM Tobias

Ah, I see now... But it is still a kind of "backwards" way of doing it, compared to what I've been scripting so far. Thanks for your answer though. :)

May 18 '10 at 06:44 AM TheMorten
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3689
x984
x788
x5

asked: May 17 '10 at 10:36 AM

Seen: 4637 times

Last Updated: May 17 '10 at 10:36 AM