Factory/Syntriax.Modules.Factory/FactorySingleton.cs

26 lines
771 B
C#

using UnityEngine;
namespace Syntriax.Modules.Factory
{
public abstract class FactorySingleton<T> : MonoBehaviour where T : FactoryBase
{
public const string ParentName = "Factories";
private static T _instance = null;
public static T Instance
{
get
{
if (_instance == null)
{
GameObject factoriesGO = GameObject.Find(ParentName) ?? new GameObject(ParentName);
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
_instance.transform.SetParent(factoriesGO.transform);
DontDestroyOnLoad(factoriesGO);
}
return _instance;
}
}
}
}