using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.UI; public class HoverText : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler, IPointerClickHandler { [HideInInspector] public Text currText; [HideInInspector] public Color normalColor; public Color hoverColor; public bool useSelected = true; public bool selectOnStart; public bool isSelected; public int groupId; private AudioSource hoverAudioSource; [Header("Script already has sound events, dont add more")] [Space(10f)] public UnityEvent OnUITextHover; public static event Action OnUIHoverTextGlobal; private void Start() { currText = GetComponent(); hoverAudioSource = GetComponent(); normalColor = currText.color; if (selectOnStart) { isSelected = true; currText.color = hoverColor; } } private void UnCheckAllTextInParent() { HoverText[] componentsInChildren = base.transform.parent.GetComponentsInChildren(); foreach (HoverText hoverText in componentsInChildren) { if (hoverText.groupId == groupId && hoverText.isSelected) { hoverText.isSelected = false; hoverText.currText.color = hoverText.normalColor; } } } public void OnPointerClick(PointerEventData pointerEventData) { SelectItem(); } public void OnPointerEnter(PointerEventData pointerEventData) { currText.color = hoverColor; HoverText.OnUIHoverTextGlobal?.Invoke(); } public void OnPointerExit(PointerEventData pointerEventData) { if (!isSelected) { currText.color = normalColor; } } public void SelectItem() { if (useSelected) { if (!currText) { currText = GetComponent(); } UnCheckAllTextInParent(); currText.color = hoverColor; isSelected = true; } } }