Files
2026-03-04 10:03:45 +08:00

91 lines
1.8 KiB
C#

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<Text>();
hoverAudioSource = GetComponent<AudioSource>();
normalColor = currText.color;
if (selectOnStart)
{
isSelected = true;
currText.color = hoverColor;
}
}
private void UnCheckAllTextInParent()
{
HoverText[] componentsInChildren = base.transform.parent.GetComponentsInChildren<HoverText>();
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<Text>();
}
UnCheckAllTextInParent();
currText.color = hoverColor;
isSelected = true;
}
}
}