复制粘贴删除
This commit is contained in:
51
Assets/Scripts/DynamicUI/Hierarchy/DoubleCheckButton.cs
Normal file
51
Assets/Scripts/DynamicUI/Hierarchy/DoubleCheckButton.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user