Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Hover state control from code

Discussion in 'UI Toolkit' started by Kan15hkSMT, Jun 18, 2020.

  1. Kan15hkSMT

    Kan15hkSMT

    Joined:
    Nov 13, 2019
    Posts:
    37
    I have a button that I have added a hover state class to and it is working perfectly at runtime. Now in certain scenarios I want the mouse to be on the button but the button state should not change to the hover state style? Is there a way to control this behavior from code?
     
  2. stan-osipov

    stan-osipov

    Unity Technologies

    Joined:
    Feb 24, 2020
    Posts:
    31
    Hello, let's say you have:

    Code (CSharp):
    1.  
    2. .my-button {
    3.  
    4. }
    5. .my-button-with-hover:hover {
    6.  
    7. }
    8.  
    Just remove .my-button-with-hover class when you no longer want your button to have a hover styling.
     
  3. Kan15hkSMT

    Kan15hkSMT

    Joined:
    Nov 13, 2019
    Posts:
    37
    Those are two different classes though right?

    If that is the case then the hover styling in the hover class won't take effect on the button because it's name is different from the button style class.

    What if I have .my-button:hover(same name as button style class) and need to control that behavior?
     
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    386
    One thing you could try is to catch the MouseEnter in the TrickleDown phase in order to get it before the button does and cancel everything.
    Code (CSharp):
    1. myButton.RegisterCallback<MouseEnter> ((evt) => {
    2.     evt.PreventDefaultAction();
    3.    evt.StopImmediatePropagation();
    4. }, TrickleDown.TrickleDown);
    More details here:
    https://docs.unity3d.com/Manual/UIE-Events-Handling.html
     
    Kan15hkSMT likes this.
  5. stan-osipov

    stan-osipov

    Unity Technologies

    Joined:
    Feb 24, 2020
    Posts:
    31
    What I mean is, you can assign both styles to your button.

    Code (CSharp):
    1.  
    2. var button = new Button();
    3. button.AddToClassList("my-button");
    4. button.AddToClassList("my-button-with-hover");
    5.            
    6. // Now when you want disable hover styling you defined in my-button-with-hover:hover
    7. button.RemoveFromClassList("my-button-with-hover");
     
    Olmi and Kan15hkSMT like this.
  6. WookieWookie

    WookieWookie

    Joined:
    Mar 10, 2014
    Posts:
    34
    This used to be controlled simply by setting a dropdown option on Event System. The reason this should be more accessible is MOBILE GAME DEVELOPMENT. Hello. No hovers. This is really annoying when we are trying to develop our games in the editor. Not something you should be recommending workarounds for.