UGUI—UIButtonMouseover

作者:追风剑情 发布于:2024-3-12 18:36 分类:Unity3d

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. /// <summary>
  4. /// 鼠标滑过放大效果
  5. /// </summary>
  6. public class UIButtonMouseover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  7. {
  8. [SerializeField]
  9. private RectTransform rectTransform;
  10. [Tooltip("鼠标滑过的缩放值")]
  11. [SerializeField]
  12. private Vector3 overScale = new Vector3(1.1f, 1.1f, 1.1f);
  13. public float speed = 10;
  14. private bool isAmplify;
  15. private float t = 0;
  16. private Vector3 localScale;
  17.  
  18. private void Awake()
  19. {
  20. localScale = rectTransform.localScale;
  21. }
  22.  
  23. public void OnPointerEnter(PointerEventData eventData)
  24. {
  25. IsAmplify = true;
  26. }
  27.  
  28. public void OnPointerExit(PointerEventData eventData)
  29. {
  30. IsAmplify = false;
  31. }
  32.  
  33. private bool IsAmplify
  34. {
  35. get { return isAmplify; }
  36. set
  37. {
  38. isAmplify = value;
  39. t = 0;
  40. localScale = rectTransform.localScale;
  41. }
  42. }
  43.  
  44. private void LateUpdate()
  45. {
  46. t += speed * Time.deltaTime;
  47. if (isAmplify)
  48. rectTransform.localScale = Vector3.Lerp(localScale, overScale, t);
  49. else
  50. rectTransform.localScale = Vector3.Lerp(localScale, Vector3.one, t);
  51. }
  52. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号