241 lines
7.0 KiB
C#
241 lines
7.0 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using System;
|
||
using UnityEngine.UI;
|
||
using AudioType = AlicizaX.Audio.Runtime.AudioType;
|
||
|
||
namespace AlicizaX.UI.Extension
|
||
{
|
||
public enum ButtonSoundType
|
||
{
|
||
Down,
|
||
Up,
|
||
Click,
|
||
Enter,
|
||
Exit,
|
||
Drag
|
||
}
|
||
|
||
[Serializable]
|
||
public class ButtonSoundCell
|
||
{
|
||
public ButtonSoundType ButtonSoundType = ButtonSoundType.Click;
|
||
public string ButtonUISoundName = "";
|
||
}
|
||
|
||
public delegate void ButtonBeginDragCallback(PointerEventData eventData);
|
||
|
||
public delegate void ButtonDragCallback(PointerEventData eventData);
|
||
|
||
public delegate void ButtonEndDragCallback(PointerEventData eventData);
|
||
|
||
public class UIButtonSuper : Button, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||
{
|
||
public List<ButtonSoundCell> m_ButtonUISounds = new List<ButtonSoundCell>();
|
||
|
||
[Tooltip("是否可以点击")] public bool m_CanClick = true;
|
||
[Tooltip("是否可以双击")] public bool m_CanDoubleClick = false;
|
||
[Tooltip("双击间隔时长")] public float m_DoubleClickIntervalTime = 0.1f;
|
||
[Tooltip("双击事件")] public ButtonClickedEvent onDoubleClick;
|
||
[Tooltip("是否可以长按")] public bool m_CanLongPress = false;
|
||
[Tooltip("长按是否只响应一次")] public bool m_ResponseOnceByPress = false;
|
||
[Tooltip("长按满足间隔")] public float m_LongPressDurationTime = 1;
|
||
|
||
[Tooltip("长按事件")] public ButtonClickedEvent onPress;
|
||
|
||
//public ButtonClickedEvent onClick;
|
||
public ButtonBeginDragCallback onBeginDrag;
|
||
public ButtonDragCallback onDrag;
|
||
public ButtonEndDragCallback onEndDrag;
|
||
|
||
private bool isDown = false;
|
||
private bool isPress = false;
|
||
private bool isDownExit = false;
|
||
private float downTime = 0;
|
||
|
||
private int fingerId = int.MinValue;
|
||
|
||
public bool IsDraging
|
||
{
|
||
get { return fingerId != int.MinValue; }
|
||
} //摇杆拖拽状态
|
||
|
||
public int FingerId
|
||
{
|
||
get { return fingerId; }
|
||
}
|
||
|
||
private float clickIntervalTime = 0;
|
||
private int clickTimes = 0;
|
||
|
||
void Update()
|
||
{
|
||
if (isDown)
|
||
{
|
||
if (!m_CanLongPress)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (m_ResponseOnceByPress && isPress)
|
||
{
|
||
return;
|
||
}
|
||
|
||
downTime += Time.deltaTime;
|
||
if (downTime > m_LongPressDurationTime)
|
||
{
|
||
isPress = true;
|
||
onPress.Invoke();
|
||
}
|
||
}
|
||
|
||
if (clickTimes >= 1)
|
||
{
|
||
if (!m_CanLongPress && !m_CanDoubleClick && m_CanClick)
|
||
{
|
||
onClick.Invoke();
|
||
clickTimes = 0;
|
||
}
|
||
else
|
||
{
|
||
clickIntervalTime += Time.deltaTime;
|
||
if (clickIntervalTime >= m_DoubleClickIntervalTime)
|
||
{
|
||
if (clickTimes >= 2)
|
||
{
|
||
if (m_CanDoubleClick)
|
||
{
|
||
onDoubleClick.Invoke();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (m_CanClick)
|
||
{
|
||
onClick.Invoke();
|
||
}
|
||
}
|
||
|
||
clickTimes = 0;
|
||
clickIntervalTime = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否按钮按下
|
||
/// </summary>
|
||
public bool IsDown
|
||
{
|
||
get { return isDown; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否按钮长按
|
||
/// </summary>
|
||
public bool IsPress
|
||
{
|
||
get { return isPress; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否按钮按下后离开按钮位置
|
||
/// </summary>
|
||
public bool IsDownExit
|
||
{
|
||
get { return isDownExit; }
|
||
}
|
||
|
||
public ButtonSoundCell GetButtonSound(ButtonSoundType buttonSoundType)
|
||
{
|
||
foreach (var buttonSound in m_ButtonUISounds)
|
||
{
|
||
if (buttonSound.ButtonSoundType == buttonSoundType)
|
||
{
|
||
return buttonSound;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private void PlayButtonSound(ButtonSoundType buttonSoundType)
|
||
{
|
||
ButtonSoundCell buttonSound = GetButtonSound(buttonSoundType);
|
||
if (buttonSound == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
GameApp.Audio.Play(AudioType.UISound, buttonSound.ButtonUISoundName, false, GameApp.Audio.UISoundVolume, true);
|
||
}
|
||
|
||
public override void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
base.OnPointerEnter(eventData);
|
||
PlayButtonSound(ButtonSoundType.Enter);
|
||
}
|
||
|
||
public override void OnPointerDown(PointerEventData eventData)
|
||
{
|
||
base.OnPointerDown(eventData);
|
||
if (eventData.pointerId < -1 || IsDraging) return; //适配 Touch:只响应一个Touch;适配鼠标:只响应左键
|
||
fingerId = eventData.pointerId;
|
||
isDown = true;
|
||
isDownExit = false;
|
||
downTime = 0;
|
||
PlayButtonSound(ButtonSoundType.Down);
|
||
}
|
||
|
||
public override void OnPointerUp(PointerEventData eventData)
|
||
{
|
||
base.OnPointerUp(eventData);
|
||
if (fingerId != eventData.pointerId) return; //正确的手指抬起时才会;
|
||
fingerId = int.MinValue;
|
||
isDown = false;
|
||
isDownExit = true;
|
||
PlayButtonSound(ButtonSoundType.Up);
|
||
}
|
||
|
||
public override void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
base.OnPointerExit(eventData);
|
||
if (fingerId != eventData.pointerId) return; //正确的手指抬起时才会;
|
||
isPress = false;
|
||
isDownExit = true;
|
||
PlayButtonSound(ButtonSoundType.Exit);
|
||
}
|
||
|
||
public override void OnPointerClick(PointerEventData eventData)
|
||
{
|
||
if (!isPress)
|
||
{
|
||
clickTimes += 1;
|
||
}
|
||
else
|
||
isPress = false;
|
||
|
||
PlayButtonSound(ButtonSoundType.Click);
|
||
}
|
||
|
||
public void OnBeginDrag(PointerEventData eventData)
|
||
{
|
||
onBeginDrag?.Invoke(eventData);
|
||
}
|
||
|
||
public void OnDrag(PointerEventData eventData)
|
||
{
|
||
PlayButtonSound(ButtonSoundType.Drag);
|
||
onDrag?.Invoke(eventData);
|
||
}
|
||
|
||
public void OnEndDrag(PointerEventData eventData)
|
||
{
|
||
onEndDrag?.Invoke(eventData);
|
||
}
|
||
}
|
||
}
|