17 lines
469 B
C#
17 lines
469 B
C#
|
using System;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public abstract class LazySingleton<T> : MonoBehaviour where T: MonoBehaviour
|
|||
|
{
|
|||
|
public static T Instance => _lazyInstance.Value;
|
|||
|
|
|||
|
private static readonly Lazy<T> _lazyInstance = new Lazy<T>(CreateSingleton);
|
|||
|
|
|||
|
private static T CreateSingleton()
|
|||
|
{
|
|||
|
var ownerObject = new GameObject($"{typeof(T).Name} (singleton)");
|
|||
|
DontDestroyOnLoad(ownerObject);
|
|||
|
return ownerObject.AddComponent<T>();;
|
|||
|
}
|
|||
|
}
|