Unity中,按钮按下抬起的缩放效果
示意按钮点击效果.gif
相关配置
在Button组件同级下挂上该脚本组件:UIBtnScaleEffect
image.png
代码
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;public class UIBtnScaleEffect : MonoBehaviour, IPointerDownHandler, IPointerUpHandler{ private float _downScale = 0.85f; private float _downDuration = 0.2f; private float _upDuration = 0.15f; private RectTransform RectTransform { get { if (_rectTransform == null) { _rectTransform = GetComponent<RectTransform>(); } return _rectTransform; } } private RectTransform _rectTransform; public void OnPointerDown(PointerEventData eventData) { StopAllCoroutines(); StartCoroutine(ChangeScaleCoroutine(1, _downScale, _downDuration)); } public void OnPointerUp(PointerEventData eventData) { StopAllCoroutines(); StartCoroutine(ChangeScaleCoroutine(RectTransform.localScale.x, 1, _upDuration)); } private IEnumerator ChangeScaleCoroutine(float beginScale, float endScale, float duration) { float timer = 0f; while (timer < duration) { RectTransform.localScale = Vector3.one * Mathf.Lerp(beginScale, endScale, timer / duration); timer += Time.fixedDeltaTime; yield return null; } RectTransform.localScale = Vector3.one * endScale; }}
页:
[1]