mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-30 12:32:55 +00:00
45 lines
964 B
C#
45 lines
964 B
C#
using UnityEngine;
|
|
|
|
namespace QSB.Utility
|
|
{
|
|
public delegate void EnableDisableEvent();
|
|
|
|
public class OnEnableDisableTracker : MonoBehaviour
|
|
{
|
|
public event EnableDisableEvent OnEnableEvent;
|
|
public event EnableDisableEvent OnDisableEvent;
|
|
|
|
public MonoBehaviour AttachedComponent;
|
|
|
|
private ComponentState _wasEnabled = ComponentState.NotChecked;
|
|
|
|
private void Update()
|
|
{
|
|
if (AttachedComponent == null)
|
|
{
|
|
DebugLog.DebugWrite($"Attached component is null! Attached to {gameObject.name}", OWML.Common.MessageType.Error);
|
|
return;
|
|
}
|
|
var state = AttachedComponent.isActiveAndEnabled ? ComponentState.Enabled : ComponentState.Disabled;
|
|
if (_wasEnabled != state)
|
|
{
|
|
_wasEnabled = state;
|
|
if (state == ComponentState.Enabled)
|
|
{
|
|
OnEnableEvent?.SafeInvoke();
|
|
}
|
|
else
|
|
{
|
|
OnDisableEvent?.SafeInvoke();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
internal enum ComponentState
|
|
{
|
|
NotChecked = 0,
|
|
Enabled = 1,
|
|
Disabled = 2
|
|
}
|
|
} |