52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class DoubleCheckButton : MonoBehaviour, IPointerExitHandler
|
|
{
|
|
private Button button;
|
|
private bool isConfirmState = false;
|
|
public UnityAction onConfirm;
|
|
|
|
void Awake()
|
|
{
|
|
button = GetComponent<Button>();
|
|
button.onClick.AddListener(OnButtonClick);
|
|
}
|
|
|
|
private void OnButtonClick()
|
|
{
|
|
if (!isConfirmState)
|
|
{
|
|
// 第一次点击,进入确认状态(变红)
|
|
isConfirmState = true;
|
|
button.image.color = Color.red;
|
|
}
|
|
else
|
|
{
|
|
// 第二次点击,执行命令
|
|
ExecuteCommand();
|
|
ResetButtonState();
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
// 鼠标移出时,重置按钮状态
|
|
ResetButtonState();
|
|
}
|
|
|
|
private void ExecuteCommand()
|
|
{
|
|
onConfirm?.Invoke();
|
|
}
|
|
|
|
private void ResetButtonState()
|
|
{
|
|
isConfirmState = false;
|
|
button.image.color = Color.white;
|
|
}
|
|
}
|
|
|