UGUI—UIButtonMouseover

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

using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 鼠标滑过放大效果
/// </summary>
public class UIButtonMouseover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField]
    private RectTransform rectTransform;
    [Tooltip("鼠标滑过的缩放值")]
    [SerializeField]
    private Vector3 overScale = new Vector3(1.1f, 1.1f, 1.1f);
    public float speed = 10;
    private bool isAmplify;
    private float t = 0;
    private Vector3 localScale;

    private void Awake()
    {
        localScale = rectTransform.localScale;
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        IsAmplify = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        IsAmplify = false;
    }

    private bool IsAmplify
    {
        get { return isAmplify; }
        set
        {
            isAmplify = value;
            t = 0;
            localScale = rectTransform.localScale;
        }
    }

    private void LateUpdate()
    {
        t += speed * Time.deltaTime;
        if (isAmplify)
            rectTransform.localScale = Vector3.Lerp(localScale, overScale, t);
        else
            rectTransform.localScale = Vector3.Lerp(localScale, Vector3.one, t);
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号