Abstract generic singleton component base class. It stores a static Instance of type T, sets it on awake if active, clears it on destroy, and preserves active-state across hotload via IHotloadManaged Created/Destroyed methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forp.Util;
public abstract class SingletonComponent<T> : Component, IHotloadManaged
where T : SingletonComponent<T>
{
public static T Instance { get; private set; }
protected override void OnAwake()
{
if (Active)
{
Instance = (T)this;
}
}
void IHotloadManaged.Destroyed(Dictionary<string, object> state)
{
state["IsActive"] = Instance == this;
}
void IHotloadManaged.Created(IReadOnlyDictionary<string, object> state)
{
if (state.GetValueOrDefault("IsActive") is true)
{
Instance = (T)this;
}
}
protected override void OnDestroy()
{
if (Instance == this)
{
Instance = null;
}
}
}