2023-03-20 22:15:21 +03:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.State
|
|
|
|
{
|
|
|
|
public class StateEnableMonoBehaviour : MonoBehaviour, IStateEnable
|
|
|
|
{
|
|
|
|
private bool _isEnabled = true;
|
|
|
|
public bool IsEnabled
|
|
|
|
{
|
|
|
|
get => _isEnabled;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == _isEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_isEnabled = value;
|
|
|
|
OnEnabledChanged?.Invoke(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Action<bool> OnEnabledChanged { get; set; } = null;
|
|
|
|
|
|
|
|
protected void Awake()
|
|
|
|
=> OnEnabledChanged += (state) => enabled = state;
|
|
|
|
|
|
|
|
protected void OnEnable() => IsEnabled = true;
|
2023-03-23 22:00:33 +03:00
|
|
|
protected void OnDisable()
|
|
|
|
{
|
|
|
|
IsEnabled = false;
|
|
|
|
|
|
|
|
if (!gameObject.activeInHierarchy) // Just so we can get OnEnable called when the object is back active again in the hierarchy
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
2023-03-20 22:15:21 +03:00
|
|
|
}
|
|
|
|
}
|