You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

47 lines
959 B

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);
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 { }
}
}