If you are using the default unity UI, one way to do that is by implementing some interfaces that unity provides for those callbacks.
ISelectHandler: Called when an object is selected.
IDeselectHandler: Called when an object is deselected.
IPointerEnterHandler: Called when the pointer enters an object's area (highlight).
IPointerExitHandler: Called when the pointer exits an object's area.
using UnityEngine;
using UnityEngine.EventSystems;
public class MenuCallbacks : MonoBehaviour, ISelectHandler, IDeselectHandler
{
// Called when the object is selected
public void OnSelect(BaseEventData eventData)
{
Debug.Log("Selected: " + gameObject.name);
// Custom callback logic
}
// Called when the object is deselected
public void OnDeselect(BaseEventData eventData)
{
Debug.Log("Deselected: " + gameObject.name);
// Custom callback logic
}
}