- Home /
Disable a GUI while left mouse button is Down?
Hello Everyone,
I'm trying to disable a GUI button while my left mouse button is down and dragging. I made this script but is not working.
Any idea what is wrong?
Thanks in advance!
static var showOff : int;
var Skin_360 : GUISkin;
var button360 = true;
function OnGUI(){
GUI.enabled = button360 ;
GUI.skin = Skin_360;
if (GUI.Button(Rect(365,490,150,90),"")){
}
if (Input.GetButtonDown("Fire1")){
button360 = false;
showOff = 1;
}else{
button360 = true;
showOff = 0;
}
}
Answer by DeveshPandey · Feb 04, 2013 at 05:25 AM
GetButtonDown() called once when button down.
Try this :
function OnGUI(){
GUI.enabled = button360 ;
GUI.skin = Skin_360;
if (GUI.Button(Rect(365,490,150,90),"")){
}
if(Input.GetMouseButton(0)){
button360 = false;
showOff = 1;
}else{
button360 = true;
showOff = 0;
}
}
"Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop."
http://docs.unity3d.com/Documentation/ScriptReference/Input.html
"For each event OnGUI is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call."
http://docs.unity3d.com/Documentation/ScriptReference/Event.html
Answer by iwaldrop · Feb 04, 2013 at 03:58 AM
While in the OnGUI function you need to look at input events, not the input class.
Event e = Event.current;
if (e.keyCode == KeyCode.Mouse0)
// return, gui == false, etc...
You could also just move your if (Input...) line to the update function and set a flag.
Hi guys
Iwaldrop, Thanks for your help! I did try to put your script in place, but I'm newbie in Java.
Please take a Look to my script.
static var showOff : int;
var Skin_360 : GUISkin;
var button360 = true;
function Update(){
if (Input.GetButtonDown("Fire1")){
Event e = Event.current;
}
}
function OnGUI(){
GUI.enabled = button360 ;
GUI.skin = Skin_360;
if (GUI.Button(Rect(365,490,150,90),"")){
if (e.keyCode == $$anonymous$$eyCode.$$anonymous$$ouse0)
show();
}
}
function show(){
if(button360){
button360 = false;
}else{
button360 = true;
You need to put that line in OnGUI, not Update.
function OnGUI()
{
GUI.enabled = button360 ;
GUI.skin = Skin_360;
if (GUI.Button(Rect(365,490,150,90),""))
{
Event e = Event.current;
if (e.keyCode == $$anonymous$$eyCode.$$anonymous$$ouse0)
show();
}
}
But now that I take a closer look at the rest of your code, I'm not sure this is going to do what you're hoping for. The button is always going to draw, and the only way you're going to be able to check if the mouse button is down is when you are clicking the button... I doubt this is the behavior you're trying to achieve.
You either need to check it further up in OnGUI, or you need to set a flag in Update to not draw your GUI while the mouse button is down. :)
Your answer
Follow this Question
Related Questions
Unity GUI Button Options 0 Answers
Button being pressed but an other button gets the effect. 0 Answers
Right-click button using GUI class? 3 Answers
Menu button needs to be double clicked. 4 Answers