Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Button Hovered Loses Focus

Discussion in 'UGUI & TextMesh Pro' started by RadRedPanda, Jul 29, 2022.

  1. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,644
    I have a Button - TextMeshPro using Sprite Swap, and I wanted behavior where when the button is selected or hovered, an icon would appear next to it, like this.



    I did this by having two images, a blank one for the regular states, and one with the icon in it. Here's the images.





    As you can see, the top one is quite literally a blank png, I don't think that part is related, but I included it anyways.

    When I hover the mouse over the button, the icon appears fine, but if I move from the icon to over the text, it stops being registered as hovered. Does anyone know why this is happening, and how to fix it?

    The text is a child of the Button.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    578
    What version of unity are you using? There is a bug in unity 2021.2.17-2021.3.5 (and probably a few other versions including 2022 series) breaking how pointer enter exit events work for child elements. It got fixed in 2021.3.6. See https://issuetracker.unity3d.com/is...-image-which-is-set-as-buttons-target-graphic

    If you can't update to 2021.3.6 or 2021.3.7 (both of which have buggy nested canvases), there is relatively simple workaround. Disable raycast target property for all the child Images and Text elements, leave it enabled only for top element of the button. In case of TextMeshPro components raycast target property is hidden under advanced properties or something like that (don't remember the exact names).
     
    RadRedPanda likes this.
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,644
    Thanks for the response! I am on version 2021.3.0f1. Your workaround seems to have worked! Some easy solution I didn't have the brain power at the time to process. Still giving you brownie points.