From 7545ab9fded7abc72b557087b3892393408d61f8 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Wed, 1 Feb 2023 15:10:40 +0000 Subject: [PATCH] Added ResetSelectableHighlight component --- .../Utilities/ResetSelectableHighlight.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Runtime/Scripts/Utilities/ResetSelectableHighlight.cs diff --git a/Runtime/Scripts/Utilities/ResetSelectableHighlight.cs b/Runtime/Scripts/Utilities/ResetSelectableHighlight.cs new file mode 100644 index 0000000..19b9590 --- /dev/null +++ b/Runtime/Scripts/Utilities/ResetSelectableHighlight.cs @@ -0,0 +1,37 @@ +/// Credit SimonDarksideJ + +namespace UnityEngine.UI.Extensions +{ + /* + Handy Selectable script to un-highlight 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 component to revert to un-highlighted. + */ + + using UnityEngine; + using UnityEngine.EventSystems; + using UnityEngine.UI; + + [AddComponentMenu("UI/Extensions/ResetSelectableHighlight", 31)] + [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(); + } + } + + public void OnPointerExit(PointerEventData eventData) + { + attachedSelectable.targetGraphic.CrossFadeColor(attachedSelectable.colors.normalColor, 0f, true, true); + } + } +} \ No newline at end of file