Factory/Syntriax.Modules.Factory/FactorySingleton.cs

26 lines
771 B
C#
Raw Normal View History

2022-11-21 13:33:46 +03:00
using UnityEngine;
namespace Syntriax.Modules.Factory
{
2022-11-21 13:48:58 +03:00
public abstract class FactorySingleton<T> : MonoBehaviour where T : FactoryBase
2022-11-21 13:33:46 +03:00
{
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);
2022-11-21 13:48:58 +03:00
_instance = new GameObject(typeof(T).Name).AddComponent<T>();
2022-11-21 13:33:46 +03:00
_instance.transform.SetParent(factoriesGO.transform);
DontDestroyOnLoad(factoriesGO);
}
return _instance;
}
}
}
}