Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by uldall · Nov 21, 2012 at 04:13 PM · editorguilayout.button

React on button down in Editor GUI

I am looking for a way to determine when a GUI button in the Editor is pressed down and when it is released.

I have used the following code for testing GUILayout.Button:

 void OnGUI () {
    bool buttonValue = GUILayout.Button("Button");
    Debug.Log("Button value: " + buttonValue);
 }

The boolean returned by GUILayout.Button seems to only return TRUE once whenever the button is released, and FALSE at all other times.

Does anyone know how I can determine if the button has been pressed down?

Comment
Add comment
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
0
Best Answer

Answer by SkillDev · Nov 21, 2012 at 08:43 PM

you have use static variable 'Event.current' to whether 'isMouse' property is true and then check for 'button' property to check witch mouse button is down. also you have to know rectangle of your button and check whether mousePosition is inside rectangle of button.

something like this :

 // assume this is area of your button
 Rect buttonArea = new Rect(5,5,100,30);
 Event e = Event.current;
 if(e != null)
 {
     if(e.isMouse && e.button  == 0 && buttonArea.Contains(e.mousePosition))
     {
         // then left mouse button is down
     }
 }
Comment
Add comment · 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 uldall · Nov 22, 2012 at 07:38 AM 0
Share

For reference I ended up with the following code:

 void OnGUI () {
     Event e = Event.current;
     
     GUIContent buttonText = new GUIContent("Button");
     Rect buttonRect = GUILayoutUtility.GetRect(buttonText, GUIStyle.none); 
     
     if (e.is$$anonymous$$ouse && buttonRect.Contains(e.mousePosition)) { 
         Debug.Log("$$anonymous$$ouse pressed: " + (e.type == EventType.$$anonymous$$ouseDown));
     } 
     
     GUI.Button(buttonRect, buttonText);
 }
avatar image
0

Answer by uldall · Nov 22, 2012 at 08:31 AM

For reference I ended up with the following code:

 void OnGUI () {
     Event e = Event.current;
     
     GUIContent buttonText = new GUIContent("Button");
     Rect buttonRect = GUILayoutUtility.GetRect(buttonText, GUIStyle.none); 
     
     if (e.isMouse && buttonRect.Contains(e.mousePosition)) { 
         Debug.Log("Mouse pressed: " + (e.type == EventType.MouseDown));
     } 
     
     GUI.Button(buttonRect, buttonText);
 }
Comment
Add comment · 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 DwarfinChief · Apr 30, 2015 at 06:35 AM 0
Share

Brilliant! Thanks!

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Public variables set with custom editor buttons reset to prefab values on play? 2 Answers

Unity Editor crashes when I click on a .mat 3 Answers

Colldier wireframe and navmesh overlay not showing. 0 Answers

object list not serializing 2 Answers

Trouble downloading iOS Platform editor support 0 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