State/Runtime/ToggleStateMonoBehaviour.cs

31 lines
769 B
C#

using System;
using UnityEngine;
namespace Syntriax.Modules.ToggleState
{
public class ToggleStateMonoBehaviour : MonoBehaviour, IToggleState
{
private bool _isToggled = true;
public bool IsToggled
{
get => _isToggled;
set
{
if (value == _isToggled)
return;
_isToggled = value;
OnToggleStateChanged?.Invoke(value);
}
}
public Action<bool> OnToggleStateChanged { get; set; } = null;
protected void Awake()
=> OnToggleStateChanged += (state) => enabled = state;
protected void OnEnable() => IsToggled = true;
protected void OnDisable() => IsToggled = false;
}
}