Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
2
Question by wlad_s · Jan 20, 2016 at 11:37 PM · inputmousekeyboardstandalonehover

Clearing mouse hover effect when Cursor is hidden

Hi,

I've made my game to hide the cursor in menu when the user starts using keyboard or gamepad/controller. I also disable the mouse input for the UI. If the user moves the mouse, the cursor appears and mouse control is enabled.

There's only one thing that's bugging me. If a cursor/pointer is hovering over a button and then if I start using keyboard or gamepad for input, the pointer hides just fine, but the hovered button still has a hovered/selected appearance. So it just sits there looking selected even though I'm selecting other buttons with keyboard/gamepad.

It just looks silly, as if two buttons are selected.

Does anyone know how can I clear the effect of mouse hover once I hide and disable the cursor?

I didn't even manage to get the gameobject that is hovered.

(PS Similarly, if a button is selected by keyboard/gamepad, and i start using the mouse, then again I have the same silly appearance as if two buttons are selected (one remains selected and the other is hovered). I've solved this problem easily by calling EventSystem.current.SetSelectedGameObject(null) when mouse control is activated. Then again, if I start using keyboard/controller, I manually set SelectedGameObject to some default button for that menu panel.)

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
6
Best Answer

Answer by Marceta · Jan 21, 2016 at 11:22 AM

 using UnityEngine;
 using UnityEngine.EventSystems;
 using System.Collections;
 using System.Collections.Generic;
 
 public class FirstMacScript : MonoBehaviour 
 {
     void Update () 
     {
             PointerEventData pointer = new PointerEventData(EventSystem.current);
             pointer.position = Input.mousePosition;
 
             List<RaycastResult> raycastResults = new List<RaycastResult>();
             EventSystem.current.RaycastAll(pointer, raycastResults);
 
             if(raycastResults.Count > 0)
             {
                 Debug.Log (raycastResults [raycastResults.Count-1].gameObject);
             }
     }
 }
 
Comment
Add comment · 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 wlad_s · Jan 21, 2016 at 12:01 PM 4
Share

Thanks!

So, using $$anonymous$$arceta's answer, I was able to unhover the button once keyboard/controller is used, like this:

 void $$anonymous$$eyboardOrControllerUsed(){
 
    if (Cursor.visible) {
        Cursor.visible = false;
        allow$$anonymous$$ouseInput = false;
        ClearPointer$$anonymous$$ess();
    }
 
 }
 
 
 public void ClearPointer$$anonymous$$ess() {

     PointerEventData pointer = new PointerEventData(EventSystem.current);
     pointer.position = Input.mousePosition;

     List<RaycastResult> raycastResults = new List<RaycastResult>();
     EventSystem.current.RaycastAll(pointer, raycastResults);

     if (raycastResults.Count > 0) {

         foreach (RaycastResult raycastResult in raycastResults) {
             Debug.Log(raycastResult.gameObject.name);
             GameObject hoveredObj = raycastResult.gameObject;

             if (hoveredObj.GetComponent<Button>()) {
                 hoveredObj.GetComponent<Button>().OnPointerExit(pointer);
             } else {//sometimes in my setup the child image is interactable
                 hoveredObj = hoveredObj.transform.parent.gameObject;
                 if (hoveredObj.GetComponent<Button>()) {
                     hoveredObj.GetComponent<Button>().OnPointerExit(pointer);
                 }
             }
         }

     }

 }
 
avatar image PsyDev wlad_s · Jan 20, 2017 at 08:00 PM 0
Share

Hey Thanks, it's a year later, but I had the exact same problem, and this solution worked.

avatar image $$anonymous$$ wlad_s · Jan 28, 2019 at 03:17 AM 0
Share

still useful, thanks a lot

avatar image Charlivi wlad_s · Mar 02 at 02:50 PM 0
Share

Still useful in 2022! Thanks!

avatar image
0

Answer by Charlivi · Mar 02 at 03:56 PM

Old thread, but I ended up here for the same question in 2022.

The answer from wlad_s is still working (thanks again), but I found another solution that works for me and wanted to share it in case it can help someone else.

I'm using Rewired, but pretty sure it can be done without it too.


     // Put this code in a Controller changed callback
     if (controller.type != ControllerType.Mouse)
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;    
     }
     else if (controller.type == ControllerType.Mouse)
     {
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
     }
Comment
Add comment · 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

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

37 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

Related Questions

Multiple Players on one Computer/Console 5 Answers

Standalone borderless window doesnt get keyboard input (Mac) 0 Answers

Issues with mouse input after resizing Standalone 1 Answer

New Input System: Event on input type changed (between mouse, keyboard and controller) 1 Answer

Input System Can't Catch Event on Update 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