Files
Simulation/Assets/Scripts/UI/UIButtonToggle.cs
2025-06-05 17:54:18 +09:00

37 lines
797 B
C#

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class UIButtonToggle : MonoBehaviour
{
public bool isPressed = false;
public UnityEvent onPressed;
public UnityEvent onReleased;
void Awake()
{
Button button = GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(
() =>
{
isPressed = !isPressed;
if (isPressed) onPressed?.Invoke();
else onReleased?.Invoke();
});
}
}
public void OnPressed()
{
onPressed?.Invoke();
isPressed = true;
}
public void OnReleased()
{
onReleased?.Invoke();
isPressed = false;
}
}