2025-04-11 17:26:28 +08:00
|
|
|
using System;
|
2025-07-28 13:04:49 +08:00
|
|
|
using System.Collections;
|
2025-04-11 17:26:28 +08:00
|
|
|
using System.Collections.Generic;
|
2025-12-19 20:26:22 +08:00
|
|
|
using AlicizaX.UI;
|
2025-04-11 17:26:28 +08:00
|
|
|
using AlicizaX.UI.Extension;
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
#if INPUTSYSTEM_SUPPORT
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
#endif
|
2025-10-13 20:20:01 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
namespace UnityEngine.UI
|
2025-04-11 17:26:28 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
[AddComponentMenu("UI/UXButton", 30)]
|
|
|
|
|
public class UXButton : UXSelectable, IPointerClickHandler, ISubmitHandler, IButton
|
|
|
|
|
#if INPUTSYSTEM_SUPPORT
|
|
|
|
|
, IHotkeyTrigger
|
|
|
|
|
#endif
|
2025-04-11 17:26:28 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
#if INPUTSYSTEM_SUPPORT
|
2025-10-14 17:07:30 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
InputActionReference IHotkeyTrigger.HotkeyAction
|
2025-04-11 17:26:28 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
get => _hotkeyAction;
|
|
|
|
|
set => _hotkeyAction = value;
|
2025-04-11 17:26:28 +08:00
|
|
|
}
|
2025-12-03 17:26:55 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
EHotkeyPressType IHotkeyTrigger.HotkeyPressType
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
get => _hotkeyPressType;
|
|
|
|
|
set => _hotkeyPressType = value;
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
void IHotkeyTrigger.HotkeyActionTrigger()
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
if (interactable)
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
OnSubmit(null);
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
[SerializeField] internal InputActionReference _hotkeyAction;
|
|
|
|
|
[SerializeField] internal EHotkeyPressType _hotkeyPressType;
|
2025-04-11 17:26:28 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
public InputActionReference HotKeyRefrence
|
2025-04-11 17:26:28 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
get { return _hotkeyAction; }
|
2025-04-11 17:26:28 +08:00
|
|
|
}
|
2025-12-19 20:26:22 +08:00
|
|
|
#endif
|
|
|
|
|
[SerializeField] private AudioClip hoverAudioClip;
|
|
|
|
|
[SerializeField] private AudioClip clickAudioClip;
|
2025-04-11 17:26:28 +08:00
|
|
|
|
2025-12-03 17:26:55 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
protected UXButton()
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
public override void OnPointerEnter(PointerEventData eventData)
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
base.OnPointerEnter(eventData);
|
|
|
|
|
PlayAudio(hoverAudioClip);
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
public virtual void OnPointerClick(PointerEventData eventData)
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
|
|
|
return;
|
2025-12-03 17:26:55 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
Press();
|
|
|
|
|
PlayAudio(clickAudioClip);
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
public virtual void OnSubmit(BaseEventData eventData)
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
Press();
|
|
|
|
|
PlayAudio(clickAudioClip);
|
2026-03-18 16:01:18 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
if (!IsActive() || !IsInteractable())
|
|
|
|
|
return;
|
2025-12-15 20:54:23 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
DoStateTransition(SelectionState.Pressed, false);
|
|
|
|
|
StartCoroutine(OnFinishSubmit());
|
2025-12-17 14:23:24 +08:00
|
|
|
}
|
2025-12-15 20:54:23 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
private IEnumerator OnFinishSubmit()
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
var fadeTime = colors.fadeDuration;
|
|
|
|
|
var elapsedTime = 0f;
|
2025-12-15 20:54:23 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
while (elapsedTime < fadeTime)
|
|
|
|
|
{
|
|
|
|
|
elapsedTime += Time.unscaledDeltaTime;
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
2025-12-03 17:26:55 +08:00
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
DoStateTransition(currentSelectionState, false);
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
private void PlayAudio(AudioClip clip)
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
if (clip && UXComponentExtensionsHelper.AudioHelper != null)
|
|
|
|
|
UXComponentExtensionsHelper.AudioHelper.PlayAudio(clip);
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
[SerializeField] private Button.ButtonClickedEvent m_OnClick = new Button.ButtonClickedEvent();
|
2025-12-03 17:26:55 +08:00
|
|
|
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
public Button.ButtonClickedEvent onClick
|
2025-12-03 17:26:55 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
get { return m_OnClick; }
|
|
|
|
|
set { m_OnClick = value; }
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 20:26:22 +08:00
|
|
|
private void Press()
|
2025-12-17 13:41:50 +08:00
|
|
|
{
|
2025-12-19 20:26:22 +08:00
|
|
|
if (!IsActive() || !IsInteractable())
|
|
|
|
|
return;
|
2025-04-11 17:33:58 +08:00
|
|
|
|
2025-04-17 16:03:39 +08:00
|
|
|
UISystemProfilerApi.AddMarker("Button.onClick", this);
|
2025-12-19 20:26:22 +08:00
|
|
|
m_OnClick.Invoke();
|
2025-12-03 17:26:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-11 17:26:28 +08:00
|
|
|
}
|