230 lines
6.0 KiB
C#
230 lines
6.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ShopIconHandler : MonoBehaviour
|
|
{
|
|
[Header("Shop Icon")]
|
|
public Button shopIconButton;
|
|
public Image shopIconImage;
|
|
|
|
[Header("Shop Manager")]
|
|
public ShopUIManager shopManager;
|
|
|
|
[Header("Visual Feedback")]
|
|
public Color normalColor = Color.white;
|
|
public Color hoverColor = Color.yellow;
|
|
public Color pressedColor = Color.red;
|
|
|
|
[Header("Animation")]
|
|
public bool enableHoverAnimation = true;
|
|
public float hoverScale = 1.1f;
|
|
public float animationSpeed = 5f;
|
|
|
|
private Vector3 originalScale;
|
|
private bool isHovering = false;
|
|
|
|
void Start()
|
|
{
|
|
InitializeShopIcon();
|
|
SetupEventListeners();
|
|
}
|
|
|
|
void InitializeShopIcon()
|
|
{
|
|
// Store original scale for animation
|
|
originalScale = transform.localScale;
|
|
|
|
// Setup button if not already assigned
|
|
if (shopIconButton == null)
|
|
{
|
|
shopIconButton = GetComponent<Button>();
|
|
}
|
|
|
|
// Setup image if not already assigned
|
|
if (shopIconImage == null)
|
|
{
|
|
shopIconImage = GetComponent<Image>();
|
|
}
|
|
|
|
// Set initial color
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = normalColor;
|
|
}
|
|
}
|
|
|
|
void SetupEventListeners()
|
|
{
|
|
if (shopIconButton != null)
|
|
{
|
|
shopIconButton.onClick.AddListener(OnShopIconClicked);
|
|
}
|
|
|
|
// Setup hover events if we have an EventTrigger component
|
|
var eventTrigger = GetComponent<UnityEngine.EventSystems.EventTrigger>();
|
|
if (eventTrigger != null)
|
|
{
|
|
SetupHoverEvents(eventTrigger);
|
|
}
|
|
}
|
|
|
|
void SetupHoverEvents(UnityEngine.EventSystems.EventTrigger eventTrigger)
|
|
{
|
|
// Pointer Enter event
|
|
var pointerEnter = new UnityEngine.EventSystems.EventTrigger.Entry();
|
|
pointerEnter.eventID = UnityEngine.EventSystems.EventTriggerType.PointerEnter;
|
|
pointerEnter.callback.AddListener((data) => OnPointerEnter());
|
|
eventTrigger.triggers.Add(pointerEnter);
|
|
|
|
// Pointer Exit event
|
|
var pointerExit = new UnityEngine.EventSystems.EventTrigger.Entry();
|
|
pointerExit.eventID = UnityEngine.EventSystems.EventTriggerType.PointerExit;
|
|
pointerExit.callback.AddListener((data) => OnPointerExit());
|
|
eventTrigger.triggers.Add(pointerExit);
|
|
|
|
// Pointer Down event
|
|
var pointerDown = new UnityEngine.EventSystems.EventTrigger.Entry();
|
|
pointerDown.eventID = UnityEngine.EventSystems.EventTriggerType.PointerDown;
|
|
pointerDown.callback.AddListener((data) => OnPointerDown());
|
|
eventTrigger.triggers.Add(pointerDown);
|
|
|
|
// Pointer Up event
|
|
var pointerUp = new UnityEngine.EventSystems.EventTrigger.Entry();
|
|
pointerUp.eventID = UnityEngine.EventSystems.EventTriggerType.PointerUp;
|
|
pointerUp.callback.AddListener((data) => OnPointerUp());
|
|
eventTrigger.triggers.Add(pointerUp);
|
|
}
|
|
|
|
void OnShopIconClicked()
|
|
{
|
|
Debug.Log("Shop icon clicked - Opening shop");
|
|
|
|
if (shopManager != null)
|
|
{
|
|
shopManager.OpenShop();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("ShopManager not assigned to ShopIconHandler");
|
|
}
|
|
|
|
// Play click animation
|
|
PlayClickAnimation();
|
|
}
|
|
|
|
void OnPointerEnter()
|
|
{
|
|
isHovering = true;
|
|
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = hoverColor;
|
|
}
|
|
|
|
if (enableHoverAnimation)
|
|
{
|
|
StartCoroutine(ScaleAnimation(originalScale * hoverScale));
|
|
}
|
|
}
|
|
|
|
void OnPointerExit()
|
|
{
|
|
isHovering = false;
|
|
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = normalColor;
|
|
}
|
|
|
|
if (enableHoverAnimation)
|
|
{
|
|
StartCoroutine(ScaleAnimation(originalScale));
|
|
}
|
|
}
|
|
|
|
void OnPointerDown()
|
|
{
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = pressedColor;
|
|
}
|
|
}
|
|
|
|
void OnPointerUp()
|
|
{
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = isHovering ? hoverColor : normalColor;
|
|
}
|
|
}
|
|
|
|
void PlayClickAnimation()
|
|
{
|
|
if (enableHoverAnimation)
|
|
{
|
|
StartCoroutine(ClickAnimation());
|
|
}
|
|
}
|
|
|
|
System.Collections.IEnumerator ScaleAnimation(Vector3 targetScale)
|
|
{
|
|
Vector3 startScale = transform.localScale;
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < 1f / animationSpeed)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
float progress = elapsedTime * animationSpeed;
|
|
transform.localScale = Vector3.Lerp(startScale, targetScale, progress);
|
|
yield return null;
|
|
}
|
|
|
|
transform.localScale = targetScale;
|
|
}
|
|
|
|
System.Collections.IEnumerator ClickAnimation()
|
|
{
|
|
Vector3 clickScale = originalScale * 0.9f;
|
|
|
|
// Scale down
|
|
yield return StartCoroutine(ScaleAnimation(clickScale));
|
|
|
|
// Scale back up
|
|
yield return StartCoroutine(ScaleAnimation(originalScale));
|
|
}
|
|
|
|
public void SetShopManager(ShopUIManager manager)
|
|
{
|
|
shopManager = manager;
|
|
}
|
|
|
|
public void SetShopIcon(Sprite iconSprite)
|
|
{
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.sprite = iconSprite;
|
|
}
|
|
}
|
|
|
|
public void SetInteractable(bool interactable)
|
|
{
|
|
if (shopIconButton != null)
|
|
{
|
|
shopIconButton.interactable = interactable;
|
|
}
|
|
|
|
if (shopIconImage != null)
|
|
{
|
|
shopIconImage.color = interactable ? normalColor : Color.gray;
|
|
}
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (shopIconButton != null)
|
|
{
|
|
shopIconButton.onClick.RemoveListener(OnShopIconClicked);
|
|
}
|
|
}
|
|
}
|