• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by paco morales · Feb 04, 2013 at 03:44 AM · guibuttonmousepressed

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; 
      } 
      }
  
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

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; 
     } 
     }
Comment
paco morales
erdavis

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Feb 04, 2013 at 05:33 AM 0
Share

"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

avatar image

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.

Comment
paco morales

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paco morales · Feb 04, 2013 at 05:42 AM 0
Share

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 == KeyCode.Mouse0)
 show();
 }
 } 
 
 
 function show(){
 
 if(button360){
 button360 = false;
 
     }else{
 button360 = true;
avatar image iwaldrop · Feb 04, 2013 at 05:46 AM 0
Share

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 == KeyCode.Mouse0)
             show();
     }
 }
avatar image iwaldrop · Feb 04, 2013 at 05:48 AM 1
Share

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. :)

avatar image paco morales · Feb 04, 2013 at 05:56 AM 0
Share

OK, Thanks I will :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Menu button needs to be double clicked. 4 Answers

executing As long as button still pressed 1 Answer

How to detect if mouse over a button 2 Answers

Unity UI Button touching from far distance 0 Answers

How to make a Hover event on GUI.Button 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges