Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Mrroundtree22 · Dec 08, 2020 at 06:33 AM · inputmouseonmouseoveronmouseexit

OnMouse menu

I have an idea for a menu where when you click on a button a bunch of other buttons appears:

 private void OnMouseOver()
     {
         if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
         {
             //Do stuff
         } 
     }

But I want it so that if you click again outside of the buttons the menu closes. I've tried:

 private void OnMouseExit()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 //Do stuff
             } 
         }

But this doesn't work as it only registers when the mouse leaves the object and nothing else.

Comment
Add comment · Show 2
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 Llama_w_2Ls · Dec 08, 2020 at 07:46 AM 0
Share

I'm not sure if this works in Unity but the control might have an event handler for when it loses focus? e.g. LostFocus()

avatar image Mrroundtree22 Llama_w_2Ls · Dec 08, 2020 at 08:35 AM 0
Share

$$anonymous$$aybe but how is it different from On$$anonymous$$ouseExit()?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Dec 08, 2020 at 10:05 AM

I think you need to store a state somewhere. This can be done simply with a bool:

 bool isMouseOver = false;
 
 void OnMouseEnter()
 {
     isMouseOver = true;
 }
 void OnMouseExit()
 {
     isMouseOver = false;
 }
 void Update()
 {
     if(!isMouseOver)
     {
         //Mouse is ooff the object
     }
 }
Comment
Add comment · Show 8 · 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 Mrroundtree22 · Dec 08, 2020 at 02:09 PM 0
Share

Yes I tried this but there are two problems with this:

void Update() { if(Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver) { Debug.Log("d"); } }

The first problem is that eventhough I click once the Log message shows up 2-3 times in the console. I don't know why this is. The second problem is that if I try to disable the buttons like this: void Update() { if(Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver) { //Disable menu buttons } }

Then they never appear. I don't think there's any way to do this using On$$anonymous$$ouseExit at all.

avatar image highpockets Mrroundtree22 · Dec 08, 2020 at 04:18 PM 0
Share

The function Get$$anonymous$$ouseButtonDown() should return true during the first frame that the button is pressed. If you are getting multiple log messages, maybe you have more than one instance?? I would really have to see more of your logic to see what’s happening here. There will certainly be a reasonable explanation to why you aren’t getting the expected results

avatar image Mrroundtree22 highpockets · Dec 08, 2020 at 04:50 PM 0
Share

I'll post my logic below.

avatar image Mrroundtree22 · Dec 08, 2020 at 04:53 PM 0
Share
     private void On$$anonymous$$ouseExit()
         {
             is$$anonymous$$ouseOver = false;
         }
     
     private void On$$anonymous$$ouseOver()
         {
     if (Input.Get$$anonymous$$ouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
             {
                 is$$anonymous$$ouseOver = true;
                 //Enable buttons
             }
             else if (Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver)
             {
                 //Disable buttons
             }
     }

avatar image highpockets Mrroundtree22 · Dec 08, 2020 at 06:33 PM 0
Share

I converted your answer to a comment because it’s clearly not an answer. This logic doesn’t seem to make much sense. I think this should be more or less what is needed.

 private void On$$anonymous$$ouseEnter()
 {
     is$$anonymous$$ouseOver = true;
 }
 private void On$$anonymous$$ouseExit()
 {
     is$$anonymous$$ouseOver = false;
 }
      
 private void Update()
 {
      if (Input.Get$$anonymous$$ouseButtonDown(0))
      {
          if(is$$anonymous$$ouseOver && !button.gameObject.activeInHierarchy)
          {
                 //Enable buttons
          }
          else if(!is$$anonymous$$ouseOver && button.gameObject.activeInHierarchy)
          {
                  //Disable buttons
          }
      }
 }
avatar image Mrroundtree22 highpockets · Dec 08, 2020 at 07:07 PM 0
Share
 else if (!is$$anonymous$$ouseOver && EventSystem.current.IsPointerOverGameObject())
             {
                 //Disable buttons
             }
 
 This is the part that doesn't activate. 
Show more comments

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

182 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The OnMouseOver and OnMouseEnter functions cease to work when screen.lockcursor = true 0 Answers

How to get GameObject that is clicked by a mouse? 2 Answers

Projection to mouse position in isometric game 1 Answer

Input: Missing mouse button events 2 Answers

How to handle Key + Mouse Wheel simultaneously? 1 Answer


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