将以下脚本挂载到需要此效果的按钮组件上即可
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonZoom : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private Vector3 originalScale; // 按钮原始的缩放比例
public float zoomScale = 1.1f; // 放大倍数
private void Start()
{
originalScale = transform.localScale; // 记录按钮的原始缩放比例
}
public void OnPointerEnter(PointerEventData eventData)
{
// 鼠标指针进入按钮区域时触发
// 放大按钮
transform.localScale = originalScale * zoomScale;
}
public void OnPointerExit(PointerEventData eventData)
{
// 鼠标指针离开按钮区域时触发
// 还原按钮原始的缩放比例
transform.localScale = originalScale;
}
}