整理 ShopUseCase 的相关脚本
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class CommonButton : Button
|
||||
{
|
||||
[SerializeField] private Image _background = null;
|
||||
|
||||
private Color _originalBackgroundColor;
|
||||
|
||||
[SerializeField] private UnityEvent _onPointerEnterAction = new();
|
||||
|
||||
[SerializeField] private UnityEvent _onPointerExitAction = new();
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
_originalBackgroundColor = _background.color;
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
_background.color = _originalBackgroundColor;
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
_background.color = new Color(0.3f, 0.3f, 0.3f, 0.3f);
|
||||
}
|
||||
|
||||
public override void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EnterHoverState();
|
||||
}
|
||||
|
||||
public override void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
if (eventData.button != PointerEventData.InputButton.Left)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ExitHoverState();
|
||||
}
|
||||
|
||||
public override void OnSelect(BaseEventData eventData)
|
||||
{
|
||||
_background.color = new Color(1f, 0.9f, 0.3f);
|
||||
EnterHoverState();
|
||||
}
|
||||
|
||||
public override void OnDeselect(BaseEventData eventData)
|
||||
{
|
||||
_background.color = _originalBackgroundColor;
|
||||
ExitHoverState();
|
||||
}
|
||||
|
||||
private void EnterHoverState()
|
||||
{
|
||||
if (EventSystem.current.currentSelectedGameObject != gameObject)
|
||||
{
|
||||
EventSystem.current.SetSelectedGameObject(this.gameObject);
|
||||
}
|
||||
|
||||
_onPointerEnterAction.Invoke();
|
||||
}
|
||||
|
||||
private void ExitHoverState()
|
||||
{
|
||||
_onPointerExitAction.Invoke();
|
||||
}
|
||||
|
||||
public void RegisterPointerEnterAction(UnityAction action)
|
||||
{
|
||||
_onPointerEnterAction.AddListener(action);
|
||||
}
|
||||
|
||||
public void RegisterPointerExitAction(UnityAction action)
|
||||
{
|
||||
_onPointerExitAction.AddListener(action);
|
||||
}
|
||||
|
||||
public void UnregisterPointerEnterAction(UnityAction action)
|
||||
{
|
||||
_onPointerEnterAction.RemoveListener(action);
|
||||
}
|
||||
|
||||
public void UnregisterPointerExitAction(UnityAction action)
|
||||
{
|
||||
_onPointerExitAction.RemoveListener(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user