using UnityEngine; namespace MetaCity.Core.Common { public abstract class MustCallDestroy { private bool _destroyCalled; private readonly bool _hasBeenConstructed; protected MustCallDestroy() { _hasBeenConstructed = true; } ~MustCallDestroy() { if (!_hasBeenConstructed || _destroyCalled || !ActuallyNeedsDestroyCalled()) return; ReportError(); } public void RestoreFromSave() { } protected virtual bool ActuallyNeedsDestroyCalled() { return true; } private void ReportError() { var toString = ToString(); var type = GetType().FullName; ThreadingUtils.EnqueueActionForMainThreadOrRunRightNow(() => { Debug.LogError($"Destroy hasn't been called on {toString}: {type}"); }); } public virtual void Destroy() { _destroyCalled = true; } public bool HasBeenDestroyed() { return _destroyCalled; } } }