Skip to content

Instantly share code, notes, and snippets.

@SimonDarksideJ
Created February 1, 2023 14:52
Show Gist options
  • Save SimonDarksideJ/985353c93b57f2e9b5970c29a3fa1b07 to your computer and use it in GitHub Desktop.
Save SimonDarksideJ/985353c93b57f2e9b5970c29a3fa1b07 to your computer and use it in GitHub Desktop.
Handy Selectable script to unhighlight a selectable component in Unity (e.g. a Button) when the user moves away from it, EVEN IF the user has holding a button on it
/*
Handy Selectable script to unhighlight a selectable component in Unity (e.g. a Button) when the user moves away from it, EVEN IF the user has holding a button on it.
Resolves the situation where Unity UI Components remain in a highlighted state even after the pointer has moved away (e.g. user holding a button, mouse, pointer down).
Now whenever the cursor leaves the component, it will force the UI compinent to revert to unhighlighted
*/
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Selectable))]
public class ResetSelectableHighlight : MonoBehaviour, IPointerExitHandler
{
[SerializeField]
private Selectable attachedSelectable;
// Start is called before the first frame update
void Awake()
{
if (!attachedSelectable)
{
attachedSelectable = GetComponent<Selectable>();
}
}
public void OnPointerExit(PointerEventData eventData)
{
attachedSelectable.targetGraphic.CrossFadeColor(attachedSelectable.colors.normalColor, 0f, true, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment