From b5aed9abe8974ed301f1f0de20b0223a00233c82 Mon Sep 17 00:00:00 2001 From: Steven Lai <2061455536@qq.com> Date: Fri, 29 Nov 2024 15:32:28 +0800 Subject: [PATCH] add InteractableUIElement --- EasyInteractive/InteractableUIElement.cs | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 EasyInteractive/InteractableUIElement.cs diff --git a/EasyInteractive/InteractableUIElement.cs b/EasyInteractive/InteractableUIElement.cs new file mode 100644 index 0000000..72cd83f --- /dev/null +++ b/EasyInteractive/InteractableUIElement.cs @@ -0,0 +1,50 @@ +using UnityEngine; +using UnityEngine.EventSystems; + +namespace HalfDog.EasyInteractive +{ + public abstract class InteractableUIElement : MonoBehaviour, + IPointerEnterHandler, + IPointerExitHandler + { + public bool enableInteract = true; + public void OnPointerEnter(PointerEventData eventData) + { + if (!enableInteract) return; + if ((this as IFocusable) != null && (this as IFocusable).enableFocus) + { + EasyInteractive.Instance.SetCurrentFocused(this as IFocusable,true); + } + if ((this as ISelectable) != null && (this as ISelectable).enableSelect) + { + EasyInteractive.Instance.readySelect = (this as ISelectable); + } + if ((this as IDragable) != null && (this as IDragable).enableDrag) + { + EasyInteractive.Instance.readyDrag = (this as IDragable); + } + } + + public void OnPointerExit(PointerEventData eventData) + { + if (!enableInteract) return; + if ((this as IFocusable) != null && (this as IFocusable).enableFocus) + { + if (EasyInteractive.Instance.currentFocused == (this as IFocusable)) + { + EasyInteractive.Instance.SetCurrentFocused(null,true); + } + } + if ((this as ISelectable) != null && (this as ISelectable).enableSelect) + { + if((this as ISelectable) == EasyInteractive.Instance.readySelect) + EasyInteractive.Instance.readySelect = null; + } + if ((this as IDragable) != null && (this as IDragable).enableDrag) + { + if ((this as IDragable) == EasyInteractive.Instance.readyDrag) + EasyInteractive.Instance.readyDrag = null; + } + } + } +}