Init
This commit is contained in:
30
Assets/Scripts/Runtime/Singleton.cs
Normal file
30
Assets/Scripts/Runtime/Singleton.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class Singleton<T> where T : class, new()
|
||||
{
|
||||
protected Singleton() { }
|
||||
~Singleton() { singleton = null; }
|
||||
private static T singleton;
|
||||
private static readonly object locker = new object();
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (singleton == null)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (singleton == null)
|
||||
singleton = new T();
|
||||
Debug.Log($"Create {typeof(T)}");
|
||||
}
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user