Unity Tips - New Input System - Mouse Events
Unity Tips - New Input System - Mouse Events

Unity Tips - New Input System - Mouse Events

Created on:
Team Size: 2
Time Frame: 5 days
Tool Used: Unity/C#

Working with Unityโ€™s new Input System can sometimes feel frustrating at first, even though the long-term benefits in terms of cross-platform support are significant.
A useful tip I want to share is how to update Unityโ€™s older approach for handling mouse events so that it remains compatible with the new Input System.

Previously, this could be done with simple MonoBehaviour methods such as:

private void OnMouseEnter()
{
    // Example: Debug.Log($"Mouse is over {this.name}");
}

private void OnMouseExit()
{
    // Example: Debug.Log($"Mouse has exited {this.name}");
}

With the new Input System, a few additional steps are required.

First, the script must include using UnityEngine.EventSystems so that the pointer events can be detected.

Second, the class needs to implement the interfaces IPointerEnterHandler and IPointerExitHandler.

For example:

using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// Represents an item that exists in the game world and can be picked up.
/// </summary>
[RequireComponent(typeof(Collider))] // Ensures this object always has a collider
public class WorldItem : MonoBehaviour, ICollectable, IPointerEnterHandler, IPointerExitHandler
{
    // Class implementation
}

These interfaces require the following methods to be implemented:

public void OnPointerEnter(PointerEventData eventData)
{
    // Example: Debug.Log($"Mouse is over {this.name}");
}

public void OnPointerExit(PointerEventData eventData)
{
    // Example: Debug.Log($"Mouse has exited {this.name}");
}

Finally, make sure the camera has a PhysicsRaycaster component attached and that you have an EventSystems in your hierarchy.

Once these steps are completed, the script will recognize mouse interactions in the same way it did before the new Input System was introduced.

I hope this proves useful.

© 2025 Samuel Styles