AkiraPixelWind/Assets/Scripts/Main/UI/UIYYControl/UILongClickButton.cs
2022-12-29 18:20:40 +08:00

122 lines
3.1 KiB
C#

using Axibug;
using Game;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public enum BtnInputNumChangeType
{
AddBtn,
ReduceBtn,
}
public class UILongClickButton : Button
{
public BtnInputNumChangeType mBtnType = BtnInputNumChangeType.AddBtn;
public bool IsLongClick = false;//是否长按
private bool IsDown = false;
private float Delay = 1f;//延迟相当于按下持续时间
private float LastDownTime;
//事件发送判断,如已发送增加或者减少事件,则停止发送下一次相同事件
private bool IsSendEvent = false;
public System.Action NumberChange;
public System.Action NumberChangeCancel;
public System.Action ButtonDownAction;
private void Update()
{
//判断是否属于长按
if (IsDown)
{
if (Time.time - LastDownTime >= Delay)
{
//AxibugLog.Debug("长按");
IsLongClick = true;
LastDownTime = Time.time;
}
}
else
{
IsLongClick = false;
}
if(!IsSendEvent && IsLongClick)
{
NumberChange?.Invoke();
/*
switch (mBtnType)
{
case BtnInputNumChangeType.AddBtn:
//AppEntry.Event.Fire(this, NumChangeAddEvent.Create());
break;
case BtnInputNumChangeType.ReduceBtn:
NumChangeReduceEvent?.Invoke();
//AppEntry.Event.Fire(this, NumChangeReduceEvent.Create());
break;
default:
break;
}
*/
IsSendEvent = true;
}
}
/// <summary>
/// 单击按钮一次
/// </summary>
/// <param name="eventData"></param>
public override void OnPointerDown(PointerEventData eventData)
{
//AxibugLog.Debug("按钮被按下");
base.OnPointerDown(eventData);
IsDown = true;
LastDownTime = Time.time;
ButtonDownAction?.Invoke();
}
/// <summary>
/// 按钮抬起
/// </summary>
/// <param name="eventData"></param>
public override void OnPointerUp(PointerEventData eventData)
{
//AxibugLog.Debug("按钮抬起");
base.OnPointerUp(eventData);
IsDown = false;
IsSendEvent = false;
NumberChangeCancel?.Invoke();
/*
switch (mBtnType)
{
case BtnInputNumChangeType.AddBtn:
AppEntry.Event.Fire(this, NumChangeAddCancelEvent.Create());
break;
case BtnInputNumChangeType.ReduceBtn:
AppEntry.Event.Fire(this, NumChangeReduceCancelEvent.Create());
break;
default:
break;
}*/
}
/// <summary>
/// 鼠标离开按钮区域
/// </summary>
/// <param name="eventData"></param>
public override void OnPointerExit(PointerEventData eventData)
{
//AxibugLog.Debug("鼠标离开按钮");
base.OnPointerExit(eventData);
IsDown = false;
IsSendEvent = false;
}
}