Unity自定义Button实现点击缩小松开放大的功能
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using System.Threading.Tasks;
/// 事件类型
public enum UIButtonEvent
{
CLICK,
DOWN,
UP,
EXIT,
PRESS,
}
public class UIButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
private event Action<UIButton> onClick;
private event Action<UIButton> onDown;
private event Action<UIButton> onUp;
private event Action<UIButton> onExit;
private event Action<UIButton> onPress;
private bool isSelected;
private bool isDisabled;
public float scaleDuration = 0.1f; //缩放动画的持续时间
public float clickTime = 0.2f; //点击的最短时间
public float longPressTime = 1f; //长按的最短时间
private bool isPressed = false; //是否按下
private bool isLongPressed = false; //是否长按
private bool isClicked = false; //是否点击
private float pressTime = 0f; //按下的时间
public void AddListener(Action<UIButton> action, UIButtonEvent buttonEvent = UIButtonEvent.CLICK)
{
switch (buttonEvent)
{
case UIButtonEvent.CLICK: onClick += action; break;
case UIButtonEvent.DOWN: onDown += action; break;
case UIButtonEvent.UP: onUp += action; break;
case UIButtonEvent.EXIT: onExit += action; break;
case UIButtonEvent.PRESS: onPress += action; break;
}
}
public void RemoveAllListeners()
{
onClick = null;
onDown = null;
onExit = null;
onPress = null;
onUp = null;
}
private async Task ScaleDown() //缩小按钮的方法
{
float scale = 0.9f; //缩小的比例
Vector3 originalScale = transform.localScale; //记录原始的比例
Vector3 targetScale = originalScale * scale; //计算目标比例
float startTime = Time.time; //记录开始时间
while (Time.time - startTime < scaleDuration) //在规定的时间内循环执行
{
transform.localScale = Vector3.Lerp(originalScale, targetScale, (Time.time - startTime) / scaleDuration); //根据时间差计算当前比例
await Task.Yield(); //等待一帧
}
}
private async Task ScaleUp() //放大按钮的方法
{
Vector3 originalScale = transform.localScale; //记录原始的比例
Vector3 targetScale = Vector3.one; //计算目标比例
float startTime = Time.time; //记录开始时间
while (Time.time - startTime < scaleDuration) //在规定的时间内循环执行
{
transform.localScale = Vector3.Lerp(originalScale, targetScale, (Time.time - startTime) / scaleDuration); //根据时间差计算当前比例
await Task.Yield(); //等待一帧
}
}
public void OnPointerClick(PointerEventData eventData) //点击事件的回调函数
{
if (isPressed && !isLongPressed) //如果是点击事件
{
isClicked = true; //标记为点击
//Debug.Log("Button Clicked!");
}
onClick?.Invoke(this);
}
public void OnPointerDown(PointerEventData eventData) //按下事件的回调函数
{
isPressed = true; //标记为按下
pressTime = Time.time; //记录按下时间
isLongPressed = false; //重置长按标记
ScaleDown(); //缩小按钮
}
public void OnPointerUp(PointerEventData eventData) //松开事件的回调函数
{
isPressed = false; //标记为松开
if (Time.time - pressTime > clickTime && !isClicked) //如果按下时间超过最短点击时间且不是点击事件
{
isLongPressed = true; //标记为长按
//Debug.Log("Button Long Pressed!");
}
ScaleUp(); //放大按钮
isClicked = false; //重置点击标记
}
public void OnPointerEnter(PointerEventData eventData) //进入按钮区域的回调函数
{
//无需实现
}
public void OnPointerExit(PointerEventData eventData) //离开按钮区域的回调函数
{
isPressed = false; //标记为松开
ScaleUp(); //放大按钮
isClicked = false; //重置点击标记
}
}
使用方法
在可以被射线检测到的UI组件上添加这个UIButton脚本,然后代码获取到UIButton,就可以添加点击事件然后使用了。文章来源地址https://www.toymoban.com/news/detail-802452.html
文章来源:https://www.toymoban.com/news/detail-802452.html
到了这里,关于Unity自定义Button实现点击缩小松开放大的功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!