Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Issue with Mouse Hover Detection

Discussion in 'Accessibility' started by steeeeve, Nov 28, 2023.

  1. steeeeve

    steeeeve

    Joined:
    Jul 22, 2020
    Posts:
    7
    I'm currently working on an inventory system in Unity and have encountered an issue with mouse hover detection. I'm using the following code to track the slot that the mouse is hovering over:


    public static class MouseData
    {
    public static GameObject SlotHoveredOver;
    }

    public abstract class UserInterface : MonoBehaviour
    {
    public void OnExit(GameObject obj)
    {
    MouseData.SlotHoveredOver = null;
    }

    public void OnEnter(GameObject obj)
    {
    MouseData.SlotHoveredOver = obj;
    }
    }

    public class DynamicInterface : UserInterface
    {
    public int X_START;
    public int Y_START;
    public int X_SPACE_BETWEEN_ITEM;
    public int Y_SPACE_BETWEEN_ITEM;
    public int NUMBER_OF_COLUMNS;
    public GameObject inventoryPrefab;

    public override void CreateSlots()
    {

    slotsOnInterface = new Dictionary<GameObject, InventorySlot>();
    for (int i = 0; i < inventory.GetSlots.Length; i++)
    {
    var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
    obj.GetComponent<RectTransform>().localPosition = GetPosition(i);

    AddEvent(obj, EventTriggerType.PointerEnter, delegate { OnEnter(obj); });
    AddEvent(obj, EventTriggerType.PointerExit, delegate { OnExit(obj); });
    AddEvent(obj, EventTriggerType.BeginDrag, delegate { OnDragStart(obj); });
    AddEvent(obj, EventTriggerType.EndDrag, delegate { OnDragEnd(obj); });
    AddEvent(obj, EventTriggerType.Drag, delegate { OnDrag(obj); });
    AddEvent(obj, EventTriggerType.PointerClick, delegate { OnPointerClick(obj); });
    inventory.GetSlots.slotDisplay = obj;
    slotsOnInterface.Add(obj, inventory.GetSlots);
    }
    }
    }

    The SlotHoveredOver is updated in the OnEnter and OnExit methods as the mouse enters and exits a slot. Despite this implementation, I'm occasionally experiencing issues with the correct detection of the hovered slot.
    I have a gif of a screen recording to show the error. To explain the Gif, in the scene view you can make out a small green circle correctly tracking my mouse movement on the UI canvas. Also in my Scene view you will see a red outline in each slot that signifies the "hit box" of the slot to detect my mouse. In the game scene you can see my inventory slots change color to purple when they think they are being hovered over and red when I click them. You will see some slots turn purple even though my mouse is not hovered over them. So I think it has to do with something mucking up my OnEnter() since it is being called for the wrong slot at times.

    GifURL: https://media.giphy.com/media/v1.Y2...ZfYnlfaWQmY3Q9Zw/94mG2Hw4Oii2TG4SxT/giphy.gif
    (Gif also attached)

    I think it could also be related to my ui canvas being much larger than my Main Camera. However, I do have the Canvas set to scale with Screen Size. It might be worth noting that the Canvas is also set to Screen Space Overlay.

    Any help or suggestions is greatly appreciated!!
     

    Attached Files:

  2. Since you just dumped your code in text, I won't read it through.
    Please use code tags. Here is how: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Here is a recent tutorial video how to do it (it's mainly about scrolling, but also covers hovering and selection too, just make sure you read the comments too, I have commented there how to ~halve the work):
     
  3. steeeeve

    steeeeve

    Joined:
    Jul 22, 2020
    Posts:
    7
    Thanks for the feedback and reference material. I have been stuck on this bug for quite some time now and am looking forward to getting it sorted. Appreciate the help.
     
  4. mcdvoice-survey

    mcdvoice-survey

    Joined:
    May 27, 2024
    Posts:
    1