205 lines
3.7 KiB
C#
205 lines
3.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class Hover : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler, IPointerClickHandler
|
|
{
|
|
public Sprite normalImage;
|
|
|
|
public Color normalColor = Color.white;
|
|
|
|
public Sprite hoverImage;
|
|
|
|
public Color hoverColor = Color.white;
|
|
|
|
public Sprite selectImage;
|
|
|
|
public Color selectColor = Color.white;
|
|
|
|
public bool useSelected = true;
|
|
|
|
public bool selectOnStart;
|
|
|
|
public bool isSelected;
|
|
|
|
public Text[] useHighlightText;
|
|
|
|
public Color normalHighlightTextColor;
|
|
|
|
public Color highlightTextColor;
|
|
|
|
private bool isHover;
|
|
|
|
private Image currImage;
|
|
|
|
private Button currButton;
|
|
|
|
public AudioSource hoverAudioSource;
|
|
|
|
[Header("Script already has sound events, dont add more")]
|
|
[Space(10f)]
|
|
public UnityEvent OnUIHover;
|
|
|
|
public static event Action OnUIHoverGlobal;
|
|
|
|
private void Start()
|
|
{
|
|
currImage = GetComponent<Image>();
|
|
currButton = GetComponent<Button>();
|
|
if ((bool)currButton)
|
|
{
|
|
base.enabled = currButton.interactable;
|
|
}
|
|
if (normalImage != null)
|
|
{
|
|
currImage.sprite = normalImage;
|
|
}
|
|
currImage.color = normalColor;
|
|
if (selectOnStart && useSelected)
|
|
{
|
|
if (selectImage != null)
|
|
{
|
|
currImage.sprite = selectImage;
|
|
}
|
|
currImage.color = selectColor;
|
|
isSelected = true;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isSelected && useSelected)
|
|
{
|
|
if ((bool)selectImage)
|
|
{
|
|
if (selectImage != currImage.sprite)
|
|
{
|
|
currImage.sprite = selectImage;
|
|
currImage.color = selectColor;
|
|
HighLightText(enable: true);
|
|
}
|
|
}
|
|
else if (currImage.color != selectColor)
|
|
{
|
|
currImage.color = selectColor;
|
|
HighLightText(enable: true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (isHover)
|
|
{
|
|
return;
|
|
}
|
|
if ((bool)selectImage)
|
|
{
|
|
if (selectImage == currImage.sprite)
|
|
{
|
|
if ((bool)normalImage)
|
|
{
|
|
currImage.sprite = normalImage;
|
|
}
|
|
currImage.color = normalColor;
|
|
}
|
|
}
|
|
else if (currImage.color != normalColor)
|
|
{
|
|
currImage.color = normalColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HighLightText(bool enable)
|
|
{
|
|
for (int i = 0; i < useHighlightText.Length; i++)
|
|
{
|
|
if (enable)
|
|
{
|
|
useHighlightText[i].color = highlightTextColor;
|
|
}
|
|
else
|
|
{
|
|
useHighlightText[i].color = normalHighlightTextColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData pointerEventData)
|
|
{
|
|
if (useSelected)
|
|
{
|
|
UnCheckAllButtonsInParent();
|
|
if (selectImage != null)
|
|
{
|
|
currImage.sprite = selectImage;
|
|
}
|
|
currImage.color = selectColor;
|
|
isSelected = true;
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData pointerEventData)
|
|
{
|
|
HighLightText(enable: true);
|
|
if (hoverImage != null)
|
|
{
|
|
currImage.sprite = hoverImage;
|
|
}
|
|
currImage.color = hoverColor;
|
|
playHoverSound();
|
|
isHover = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData pointerEventData)
|
|
{
|
|
UnSelect();
|
|
}
|
|
|
|
public void UnSelect()
|
|
{
|
|
if (isSelected && useSelected)
|
|
{
|
|
HighLightText(enable: true);
|
|
}
|
|
else
|
|
{
|
|
HighLightText(enable: false);
|
|
}
|
|
isHover = false;
|
|
if ((!isSelected || !useSelected) && (bool)currImage)
|
|
{
|
|
if (normalImage != null)
|
|
{
|
|
currImage.sprite = normalImage;
|
|
}
|
|
currImage.color = normalColor;
|
|
}
|
|
}
|
|
|
|
private void UnCheckAllButtonsInParent()
|
|
{
|
|
Hover[] componentsInChildren = base.transform.parent.GetComponentsInChildren<Hover>();
|
|
foreach (Hover hover in componentsInChildren)
|
|
{
|
|
if (hover.enabled)
|
|
{
|
|
hover.isSelected = false;
|
|
if (!hover.isHover)
|
|
{
|
|
hover.HighLightText(enable: false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void playHoverSound()
|
|
{
|
|
if (!(hoverAudioSource == null) && GetComponent<Image>().enabled)
|
|
{
|
|
Hover.OnUIHoverGlobal?.Invoke();
|
|
}
|
|
}
|
|
}
|