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

Can't get IPointer Enter/Exit to work on a Sprite with Collider

Discussion in 'Input System' started by Bobbypin, May 5, 2021.

  1. Bobbypin

    Bobbypin

    Joined:
    Oct 2, 2019
    Posts:
    4
    Hello.

    Yesterday I changed from the old input system to the new one. In my game scene, everything works great, and I'm very happy.

    But, it's broken my Character Creation Screen.

    I used to have two different types of Pointer/Mouse handlers for tooltips.

    One for event Data (on UI elements)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class OnHoverMakeActiveUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    7. {
    8.     [SerializeField]
    9.     public GameObject _gameObjectToMakeActive;
    10.  
    11.     public void OnPointerEnter(PointerEventData eventData)
    12.     {
    13.         _gameObjectToMakeActive.SetActive(true);
    14.     }
    15.     public void OnPointerExit(PointerEventData eventData)
    16.     {
    17.         _gameObjectToMakeActive.SetActive(false);
    18.     }
    19. }
    And one that used OnMouseEnter() (on things like sprites with a capsule Collider)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class OnHoverMakeActive : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     public GameObject _gameObjectToMakeActive;
    11.     public void OnMouseEnter()
    12.     {
    13.         _gameObjectToMakeActive.SetActive(true);
    14.     }
    15.     public void OnMouseExit()
    16.     {
    17.         _gameObjectToMakeActive.SetActive(false);
    18.     }
    19. }
    I ported across to the new Input system, and bam, the second one didn't work. After investigating, I found out this is a common issue, and OnMouseOver doesn't work with the new Input system.

    Never fear, I thought. I still have my other script I was using for UI elements, so I'll just do that.
    The suggestion Ive seen is to:

    1) make sure you have a 2D raycaster on your camera. I do
    2) make sure you have a InputSystemUIInputModule on your Event System. I do
    3) Collider on your GameObject (I do)

    - I already had a lot of these things set up because I had these two types of Hover scripts.

    Anyway. Long story short, it isn't working. Anyone know how I can get this functionality to work?
    I know I could just convert my sprite to an image, or add a button, and the other script will work, but for a number of reasons, those two options weren't very attractive.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,055
    You have two major ways to get "OnMouseOver"-ish thing to work:
    - Use "Both" InputSystem setting in your player, so the old system will fire the OnMouse* methods (I don't really recommend it)
    - Put colliders on it and fire a raycast per frame or per every N frame (whatever your use case is) from the mouse position into the scene and check if you hit a thing you're looking for. I recommend this, although obviously it's not that lazy and simple like an OnMouseOver method on a MonoBehaviour, but it's working exactly like that. You can do that from a manager-like class, so you aren't raycasting from every object separately, but once and you can sort through the results in the method you use for raycasting.
     
    Bobbypin likes this.
  3. Bobbypin

    Bobbypin

    Joined:
    Oct 2, 2019
    Posts:
    4
    I'll give this (the raycasting) a go, thanks