mikdog

HoverScale

Oct 20th, 2025 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3.  
  4. public class UIButtonHoverScalerReset : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
  5. {
  6. [Header("Hover Scaling")]
  7. public float hoverScale = 1.2f;
  8. public float scaleSpeed = 10f;
  9.  
  10. private Vector3 originalScale;
  11. private bool isHovered;
  12.  
  13. void Awake()
  14. {
  15. originalScale = transform.localScale;
  16. }
  17.  
  18. void OnEnable()
  19. {
  20. // Reset to original when menu reappears
  21. transform.localScale = originalScale;
  22. isHovered = false;
  23. }
  24.  
  25. void Update()
  26. {
  27. Vector3 target = isHovered ? originalScale * hoverScale : originalScale;
  28. transform.localScale = Vector3.Lerp(transform.localScale, target, Time.deltaTime * scaleSpeed);
  29. }
  30.  
  31. public void OnPointerEnter(PointerEventData eventData) => isHovered = true;
  32. public void OnPointerExit(PointerEventData eventData) => isHovered = false;
  33.  
  34. // Reset scale on click (so it doesn’t stay big)
  35. public void OnPointerClick(PointerEventData eventData)
  36. {
  37. isHovered = false;
  38. transform.localScale = originalScale;
  39. }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment