using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
|
|
{
|
|
|
|
public static T Instance { get; private set; }
|
|
|
|
[Header("Singleton Settings")]
|
|
public bool SetSingletonOnAwake;
|
|
public bool isPersistant;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (SetSingletonOnAwake)
|
|
SetAsSingleton();
|
|
}
|
|
|
|
private void SetAsSingleton()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
Destroy(this.gameObject);
|
|
else
|
|
Instance = (T)this;
|
|
|
|
if (isPersistant)
|
|
DontDestroyOnLoad(transform.root);
|
|
}
|
|
|
|
}
|
|
|
|
public class Singleton<T> where T : Singleton<T>, new()
|
|
{
|
|
private static T instance;
|
|
public static T Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
instance = new T();
|
|
return instance;
|
|
}
|
|
private set { }
|
|
}
|
|
|
|
}
|