mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-30 12:32:55 +00:00
50 lines
1.1 KiB
C#
50 lines
1.1 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace QSB.Utility
|
|
{
|
|
public class OnEnableDisableTracker : MonoBehaviour
|
|
{
|
|
public event Action OnEnableEvent;
|
|
public event Action OnDisableEvent;
|
|
|
|
public MonoBehaviour AttachedComponent;
|
|
|
|
private ComponentState _wasEnabled = ComponentState.NotChecked;
|
|
|
|
public OnEnableDisableTracker()
|
|
=> QSBSceneManager.OnSceneLoaded += (OWScene scene, bool inUniverse) => Destroy(this);
|
|
|
|
private void OnDestroy()
|
|
=> QSBSceneManager.OnSceneLoaded -= (OWScene scene, bool inUniverse) => Destroy(this);
|
|
|
|
private void Update()
|
|
{
|
|
if (AttachedComponent == null)
|
|
{
|
|
DebugLog.ToConsole($"Attached component is null!", 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
|
|
}
|
|
} |