37 lines
797 B
C#
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;
|
|
}
|
|
}
|